-2

I am trying to get a basic edit function working and have come up with the following.

One the first page I have:

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><a href=\"product.php?id=". $row['product_id'] ."\">" . $row['product_name'] . "</a></td>";
  echo "<td>" . $row['product_price'] . "</td>";
  echo"<td><a href =\"deleteproduct.php?id=". $row['product_id'] ."\">Delete</a>";
  echo"<td><a href =\"editproduct.php?id=". $row['product_id'] ."\">Edit</a>";
  echo "</tr>";
  }
echo "</table>";

This all work correctly apart from when I try and get the id for Edit on the next page.

The next page:

<?php



if(isset($_POST['edit']))

  {
    $con=mysqli_connect("localhost","root","","db_test");

    // Check connection

    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    if (isset ($_GET['id'])) { $product_id = strip_tags($_GET['id']); }

    $productname   =strip_tags($_POST['nameAdd']); 
    $productdesc      =strip_tags($_POST['descAdd']);
    $productimg   =strip_tags($_POST['imageAdd']); 
    $price        =strip_tags($_POST['priceAdd']);


    $sql = "UPDATE tbl_products SET product_name ='$productname', product_description ='$productdesc',
         product_img ='$productimg',product_price ='$price' WHERE product_id = '$product_id'";
    if (!$insert = $con->prepare($sql))
        die('Query failed: (' . $con->errno . ') ' . $con->error);

    if (!$insert->bind_param('ssss', $productname, $productdesc, $productimg, $price ))
        die('Binding parameters failed: (' . $insert->errno . ') ' . $insert->error);

    if (!$insert->execute())
        die('Execute failed: (' . $insert->errno . ') ' . $insert->error);
    else
        echo "Edit Successful!";

    mysqli_close($con);

echo("      </div>
      <div id='right'><br><br><img src='Red Carpet Theatre Company/images/MayDayGroup.jpg' width='350' height='250' alt='Group Photo'/>
        <p>&nbsp;</p>
      </div>
      <div id='footer'>");
}
else
{
  echo("
<FORM action='".$_SERVER['PHP_SELF']."' METHOD=post>
<input type='hidden' name='edit' value='edit'>
 <table border='3'> 
     <tr>
     <td> Product Name :</td><td><input name=nameAdd type ='text' size'14'> </input></td>
     </tr>
     <tr>
     <td> Product Description :</td><td><input name='descAdd' type ='text' size'14'> </input></td>
     </tr>
     <tr>
     <td> Product Image URL :</td><td><input name='imageAdd' type ='text' size'14'> </input></td>
     </tr>
     <tr>
     <td> Product Price :</td><td><input name='priceAdd' type ='text' size'14'> </input></td> 
     </tr>
     <tr>
     <td><input type='submit' name='Submit' value='Submit'></td>
     </tr>
</table>
</FORM>
      </div>
      <div id='right'><br><br><img src='Red Carpet Theatre Company/images/MayDayGroup.jpg' width='350' height='250' alt='Group Photo'/>
        <p>&nbsp;</p>
      </div>
      <div id='footer'>
");
}
?>

For some reason I keep getting undefined errors for the product_id variable. Any ideas why? It should be getting it from the previous page using "isset ($_GET['id'])) Thank you.

4
  • Which line do you get the error in? Commented Aug 21, 2013 at 21:41
  • product_img ='$productimg',product_price ='$price' WHERE product_id = '$product_id'"; Commented Aug 21, 2013 at 21:42
  • "For some reason I keep getting undefined errors for the product_id variable. Any ideas why?" Sure, it's totally easy: Because somethings are undefined. That easy it is. You don't need to ask here about that, we did cover this already. If you wonder about a specific error message, there is a reference: Reference - What does this error mean in PHP? Commented Aug 21, 2013 at 21:43
  • I meant it as "why is it undefined" rather than what does it mean. Commented Aug 21, 2013 at 21:45

3 Answers 3

0

$_GET['id'] isn't defined because your form method is POST. How are you getting the product ID? Is it in the url structure?

Sign up to request clarification or add additional context in comments.

4 Comments

echo"<td><a href =\"editproduct.php?id=". $row['product_id'] ."\">Edit</a>"; This is on the first page which gets the id from the database which works fine.
What happens if you try printing out the value of $_GET['id']?
I get another undefined variable error.
Okay there's your problem. Take a look at this and see if that helps you out at all.
0

as you are using this using MYSQLI_ and Not PDO

you should not use query like:

if (!$insert = $con->prepare($sql))
    die('Query failed: (' . $con->errno . ') ' . $con->error);

if (!$insert->bind_param('ssss', $productname, $productdesc, $productimg, $price ))
    die('Binding parameters failed: (' . $insert->errno . ') ' . $insert->error);

if (!$insert->execute())
    die('Execute failed: (' . $insert->errno . ') ' . $insert->error);
else
    echo "Edit Successful!";

instead this u shall use it like this:

if (!$insert = mysqli_prepare($con,$sql))
    die('Query failed: (' . mysqli_errno($con) . ') ' . mysqli_error();

if (!mysqli_stmt_bind_param($insert,'ssss', $productname, $productdesc, $productimg, $price ))
    die('Binding parameters failed: (' . mysqli_errno($insert). ') ' . mysqli_error($insert);

if (!mysqli_stmt_execute($insert))
    die('Execute failed: (' . mysqli_errno($insert) . ') ' . mysqli_error($insert);
else
    echo "Edit Successful!";

Now you code will work fine (Its just mysqli_* way of doing it

3 Comments

Excellent thank you. It still has a problem with the ; at the end of "die('Query failed: (' . mysqli_errno($con) . ') ' . mysqli_error();"
its probably because it should be mysqli_error($con) as it need an argument. reference : w3schools.com/php/func_mysqli_error.asp
Still does not like it. Really confused now. Very sorry.
0

The href syntax looks off. What does the link look like in firebug.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.