未使用预准备语句存储数据

时间:2016-02-03 08:04:54

标签: 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格式的模式也不起作用。

1 个答案:

答案 0 :(得分:0)

我建议你包装整个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';
}