Monday, 27 August 2018

Update the MySQL row based on values ​​passed to the URL using PHP

I am trying to update a Mysql row based on value passed to url of a page. But i am getting an error Notice: Undefined index: id_store in C:\xampp\htdocs\store\php\update.php on line 29when i submit the button in html form. Here is my code:
<?php
require 'db.php';

if(isset($_GET['id_store'])){

    $id_store=$_GET['id_store'];
       $sql ="SELECT store_name,heading FROM store ORDER BY id_store='$id_store'";

       $result = $conn->query($sql);

       $row = $result->fetch_assoc();
       $store_name = $row['store_name'];
       $heading = $row['heading'];

}

if(isset($_POST['btn-update']))
{

    // variables for input data
    $store_name_ = $_POST['store_name'];
    $heading_ = $_POST['heading'];

    // variables for input data
 $id=$_GET['id_store'];
// sql query for update data into database
    $sql_query = "UPDATE store SET store_name='$store_name_',heading='$heading_' WHERE id_store='$id'";

    $conn->query($sql_query);
    // sql query for update data into database
}

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>

</head>
<body>
<center>

<div >

    <form method="post"  action="update.php">
    <table align="center">
    <tr>
    <td><input type="text" name="store_name" placeholder="Store Name" value="<?php echo $store_name; ?>" required /></td>
    </tr>
    <tr>
    <td><input type="text" name="heading" placeholder="Store Heading" value="<?php echo $heading; ?>" required /></td>
    </tr>
    <tr>
    <td>
    <button type="submit" name="btn-update"><strong>UPDATE</strong></button>
    </td>
    </tr>
    </table>
    </form>
    </div>

</center>
</body>
</html>

I am getting an error at line $id=$_GET['id_store'];
I think when I submit then button the form is directed to update.php without id_storedue to which SQL query gets null value. Is there any thing that i need to change?

Please check these errors:-
  1. Your all coding stuff along with form code is in one file then why you give action. Remove action attribute from your form.
  2. form method is POST but you are using $_GET change it to $_POST every where you use that.
  3. Also change query like this:-
    $sql ="SELECT store_name,heading FROM store WHERE id_store='$id_store' ORDER BY id_store";
Note:- check and do all this thing and tell what happen

0 comments:

Post a Comment