我还是php的新手(昨天才刚开始),当我想删除我的数据时,它会显示
Notice: Undefined index: bookname in C:\wamp64\www\HelpingClass\delete.php on line 13
Notice: Undefined index: quantity in C:\wamp64\www\HelpingClass\delete.php on line 14
Notice: Undefined index: price in C:\wamp64\www\HelpingClass\delete.php on line 15
这是我在第13,14,15行的代码
$bookname = $_POST['bookname'];
$quantity = $_POST['quantity'];
$price = $_POST['price'];
我什至尝试将这些13,14,15行更改为此
if(isset($_POST['bookname']) && isset($_POST['bookname'])){
echo $_POST['bookname'];
}
if(isset($_POST['quantity']) && isset($_POST['quantity'])){
echo $_POST['quantity'];
}
if(isset($_POST['price']) && isset($_POST['price'])){
echo $_POST['price'];}
通知未定义索引不见了,但我仍然无法删除数据:(
这是我的delete.php完整代码
<?php
include ('connection.php');
session_start();
if (!isset($_SESSION ['userid'])&& empty($_SESSION['userid'])){
header ("location:login.php");
exit;
}
else {
$userid = $_SESSION ['userid'];
}
$bookid = $_GET['bookid'];
$bookname = $_POST['bookname'];
$quantity = $_POST['quantity'];
$price = $_POST['price'];
$query = mysqli_query ($conn, "DELETE FROM book SET WHERE Book_Id='$bookid';");
if ($query == TRUE)
{
echo "<script language='javascript'>";
echo "alert('Deleted.');";
echo "window.location.href='action.php';";
echo "</script>";
}
else
{
echo "<script language='javascript'>";
echo "alert('Delete Failed.');";
//echo "window.location.href='action.php';";
echo "</script>";
}
?>
非常感谢您的帮助
答案 0 :(得分:3)
您的代码中有很多问题。
参数化版本的示例可能是:
// prepare the query statement
$stmt = $mysqli_prepare($conn, "DELETE FROM book
WHERE Book_Id = ?");
// bind the parameters
$stmt->bind_param("i", $bookid);
// execute the query
$stmt->execute();
//fetching result would go here (in case of SELECT queries)
$stmt->close();
isset
函数检查变量是否存在。您可以在旁边使用三元运算符。 例如:
$bookid = isset($_GET['bookid']) ? $_GET['bookid'] : 0;
$bookname = isset($_POST['bookname']) ? $_POST['bookname'] : '';
$quantity = isset($_POST['quantity']) ? $_POST['quantity'] : 0;
$price = isset($_POST['price']) ? $_POST['price'] : 0;
DELETE
查询语句语法错误。您不能使用*
和SET
。另外,在(int)
上进行$bookid
类型转换(假设此处为整数)。使用;
函数时,查询字符串中也不需要分号(mysqli_query
)。应如下所示:
$query = mysqli_query ($conn, "DELETE FROM book
WHERE Book_Id = '" . (int)$bookid . "'");
在MySQL Documentation中,DELETE
语句的语法如下:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]