1

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 29 when 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_store due to which SQL query gets null value. Is there any thing that i need to change?

5
  • first of all try to echo $_GET['id_store'] and check value is comming or not? also paste your url in your question. Commented May 27, 2015 at 0:18
  • The url is localhost/store/php/update.php?id_store=36 Commented May 27, 2015 at 0:27
  • does $conn->query() sanitize for you? Your using an untrusted $_GET variable in your SQL query, which could easily lead to an SQL injection.. Commented May 27, 2015 at 0:27
  • The problem is simple: $_GET['id_store'] is not set, thus your error.. Commented May 27, 2015 at 0:29
  • See my answer and do like that Commented May 27, 2015 at 0:31

2 Answers 2

2

Note:

  • Make sure that there is an id_store value in your URL.
  • Your first query is wrong. You are using ORDER BY like a WHERE.
  • You try to update and submit the page, but you didn't pass the id_store. Your form will go to update.php. Remove the URL attribute in your ACTION.

Revised select query code:

$sql ="SELECT store_name,heading FROM store WHERE id_store='$id_store' ORDER BY id_store";

Your form should be:

<form method="post"  action="">

So that when your from localhost/store/php/update.php?id_store=36, and you press the submit button, it will still go to localhost/store/php/update.php?id_store=36, instead of just localhost/store/php/update.php.

After you submit, the undefine error will be gone because you retain the id_store in your URL.

And inside your isset(), so that user can't refresh the page and re-submit the form, just put this before the closing bracket:

header("LOCATION:update.php?id_store=".$_GET["id_store"]);
Sign up to request clarification or add additional context in comments.

2 Comments

Wayen. Notice: Undefined index: id_store in C:\xampp\htdocs\store\php\update.php on line 29. yes you correct query but it will not help him
I just fixed query but problem is with $_GET['id_store']; when i submit the button
1

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

2 Comments

it worked for me after removing action. but now I have to refresh page twice to see the new values. Is there a way to refresh the page after sql query execution. thanks
for that you need to put your sql code in other page and give action to that page. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.