我一直在想为什么我在准备好的陈述中会出现这个错误,但我无法解决这个问题。我得到一个错误:
Notice: Undefined index: tipimage in C:\xampp\htdocs\Matt\addtip.php on line 68
ERROR: Could not execute query: INSERT INTO tips (tiptitle, tiptext, tiplink, tipimage) VALUES (?, ?, ?, ?). Column 'tipimage' cannot be null
我的数据库中的所有值都设置为文本类型,id除外。
....
else {
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$mysqli = new mysqli("localhost", "paul", "pass", "yourcomp");
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
// Prepare an insert statement
$sql = "INSERT INTO tips (tiptitle, tiptext, tiplink, tipimage) VALUES (?, ?, ?, ?)";
if($stmt = $mysqli->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("ssss", $tiptitle, $tiptext, $tiplink, $tipimage);
// Set parameters
$tiptitle = $_REQUEST['tiptitle'];
$tiptext = $_REQUEST['tiptext'];
$tiplink = $_REQUEST['tiplink'];
$tipimage = $_REQUEST['tipimage'];
// Attempt to execute the prepared statement
if($stmt->execute()){
$successmsg = "<div class='alert alert-success'>Tip Added Successfully!</div>";
} else{
echo "ERROR: Could not execute query: $sql. " . $mysqli->error;
}
} else{
echo "ERROR: Could not prepare query: $sql. " . $mysqli->error;
}
1条答案
按热度按时间acruukt91#
您正在查找未随请求一起发送的post变量。这就是未定义索引通知试图告诉您的内容。在将它传递到数据库之前,不检查它是否存在,因此它最终被传递为null。只要给一个空字符串的默认值(这里使用null coalesce操作符),它就可以正常工作了。