我正在尝试使基本的编辑功能正常工作,并提出了以下建议。
我有一个第一页:
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>";
除了我尝试在下一页上获取 Edit 的 id 之外,这一切都正常工作。
下一页:
<?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> </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> </p>
</div>
<div id='footer'>
");
}
?>
出于某种原因,我不断收到 product_id 变量的未定义错误。任何想法为什么?应该是使用“isset ($_GET['id'])) 从上一页获取它。谢谢。