未使用准备好的语句存储数据 [英] Data not stored using prepared statements

查看:107
本文介绍了未使用准备好的语句存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习使用准备好的语句,并停留在这里.普通方法没有问题.虽然没有显示任何错误,但是尽管数据显示为输入的数据",但数据未存储在数据库中.

I am just learning to use prepared statements and stuck here. there is no problem with normal method. there is nothing error shown but the data is not stored in database although it displays "data entered".

$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格式的模式也不起作用.

Data is stored but not the type required. should i specify all types in user input? Also the pattern in html form not working.

推荐答案

我建议您包装整个bind_param&使用if条件执行,因为即使有很小的问题,该语句也无法准备.在这种情况下,我想可能是每个变量/字段的类型在某些时候是错误的-可能是image/b部分.

I'd suggest that you wrap the entire bind_param & execute with an if condition as the statement will fail to be prepared if there is even a minor issue. In this case I would guess it could be that the types for each variable/field is wrong at some point - probably the image / b part.

您可以使用gettype回显每个类型,这可能有助于对其进行跟踪:

You can echo the type of each using gettype which might help track it down:

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';
}

这篇关于未使用准备好的语句存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆