-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.

  • Which line do you get the error in? – BlueEel Aug 21 '13 at 21:41
  • product_img ='$productimg',product_price ='$price' WHERE product_id = '$product_id'"; – The Fat Fish Aug 21 '13 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?](http://stackoverflow.com/q/12769982/367456) – hakre Aug 21 '13 at 21:43
  • I meant it as "why is it undefined" rather than what does it mean. – The Fat Fish Aug 21 '13 at 21:45

3 Answers3

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?

mpriscella
  • 361
  • 3
  • 12
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

Rahul
  • 1,181
  • 1
  • 11
  • 20
  • Excellent thank you. It still has a problem with the ; at the end of "die('Query failed: (' . mysqli_errno($con) . ') ' . mysqli_error();" – The Fat Fish Aug 23 '13 at 17:30
  • its probably because it should be mysqli_error($con) as it need an argument. reference : http://www.w3schools.com/php/func_mysqli_error.asp – Rahul Aug 23 '13 at 17:34
  • Still does not like it. Really confused now. Very sorry. – The Fat Fish Aug 23 '13 at 17:46
0

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

user1050544
  • 437
  • 5
  • 24