我只是在学习使用准备好的语句并卡在这里。正常方法没有问题。没有显示任何错误,但数据未存储在数据库中,尽管它显示“已输入数据”。
$db = new mysqli("localhost", "root","","learndb");
if ($db->connect_error) {
die("Connection failed this is the error: " . $db->connect_error);
}
$stmt = $db->prepare("INSERT INTO studentrecords (Name, email, Phone, school,dob,father,feereceived,due,image) VALUES (?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("ssisssiib",$first,$email,$phone,$school,$dob,$father,$feereceived,$due,$image);
$stmt->execute();
if($stmt)
{
echo"data entered";
}
更新
数据已存储,但不是所需的类型。我应该在用户输入中指定所有类型吗? html 格式的模式也不起作用。
最佳答案
我建议您将整个 bind_param 包装起来并使用 if 条件执行,因为即使有一个小问题,语句也将无法准备。在这种情况下,我猜测可能是每个变量/字段的类型在某些时候是错误的——可能是 image
/b
部分。
您可以使用 gettype
回显每个类型,这可能有助于追踪它:
echo gettype($first), gettype($email), gettype($phone),
gettype($school), gettype($dob), gettype($father),
gettype($feereceived), gettype($due), gettype($image);
$db = new mysqli("localhost", "root","","learndb");
if ($db->connect_error) {
die("Connection failed this is the error: " . $db->connect_error);
}
$stmt = $db->prepare("INSERT INTO studentrecords (`Name`, `email`, `Phone`, `school`,`dob`,`father`,`feereceived`,`due`,`image`) VALUES (?,?,?,?,?,?,?,?,?)");
if($stmt) {
$stmt->bind_param("ssisssiib",$first,$email,$phone,$school,$dob,$father,$feereceived,$due,$image);
$stmt->execute();
} else {
echo 'Failed to prepare the sql statement';
}
关于php - 未使用准备好的语句存储的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35171530/