我需要一个更新数据的解决方案。使用当前代码我得到$ product_name,$ product_des,$ product_price未定义变量错误。怎么解决这个?由于我无法从第一个if条件之外获取那些变量值,那么我该如何运行更新查询?
echo '<form action="" method="post">ID Number:<input type="number" name="id_number" value="number"><br><br><input type="submit" value="Search Product" name="id_number_submit">';
if (isset($_POST['id_number_submit'])) {
$id_number = $_POST['id_number'];
$q = mysqli_query($conn, "SELECT * FROM product WHERE id='$id_number'");
$row = mysqli_fetch_array($q);
if (empty($row)) {
echo "Error: Invalid product id";
}
else{
$product_name = $row['product_name'];
$product_des = $row['product_des'];
$product_price = $row['product_price'];
echo '<br><br><input type="number" name="product_price" value="'.$product_price.'">';
echo '<br><br><input type="text" name="product_name" value="'.$product_name.'">';
echo '<br><br><textarea rows="8" cols="100" name="product_des">'.$product_des.'</textarea><br>';
echo '<input type="submit" value="Save Changes" name="save_changes_submit">';
}
}
if (isset($_POST['save_changes_submit'])) {
$c_product_price = $_POST['product_price'];
$c_product_name = $_POST['product_name'];
$c_product_des = $_POST['product_des'];
$q2 = mysqli_query($conn,"UPDATE product SET product_name='$c_product_name', product_price='$c_product_price', product_des='$c_product_des' WHERE product_name='$product_name', product_price='$product_price', product_des='$product_des'");
if ($q2>0) {
echo "Successfully updated!";
}else{echo "Error: Failed to update";}
}
echo '</form>';