php - 未使用准备好的语句存储的数据

标签 php mysql

我只是在学习使用准备好的语句并卡在这里。正常方法没有问题。没有显示任何错误,但数据未存储在数据库中,尽管它显示“已输入数据”。

$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/

相关文章:

php - 使用 composer、gulp 和 Travis 的 PHP 网站的目录结构

php - 在 Laravel 5.6 中创建搜索表单

php - 使用ajax php mysql的用户通知/警报系统

PHP 登录验证器 + 存储用户 ID

mysql - 删除 ID 最低的条目

java - Hibernate: native SQL 查询重复列结果

python - 更新 mysql 字段未读结果发现

PHP 循环,单独的结果取决于 group_id

PHP 动态求和回显结果(困难)

php - 使用codeigniter上传图片错误