将数据插入MySql DB [英] Insert data into MySql DB
问题描述
我正在创建一个类,并且有一个函数,我想使用该函数从一些输入中向表中插入一些数据.如果我检查表,但仍然出现错误"prepare中的参数数量与bind_result中的arg数量不匹配",它会起作用.我也不知道我的方法是否正确..
I'm creating a class and I have a function with which I want to insert some data into an table from some inputs. It works if I check the table but I keep getting the error "number of arguments in prepare doesn't match the no of arg in bind_result". Also I don't know if my method is correct ..
private function insertData($foldName,$foldClass,$foldLink) {
$sql = "INSERT INTO folders (folder_name,folder_class,folder_link) VALUES ('$foldName','$foldClass','$foldLink')";
if($stmt = $this->connect->prepare($sql)) {
$stmt->execute();
$stmt->bind_result($foldName,$foldClass,$foldLink);
$stmt->close();
$error = false;
$message['error'] = false;
$message['message'] = "Data Successfuly Inserted";
return json_encode($message);
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "Data Failed To Insert";
return json_encode($message);
}
}
推荐答案
在插入数据而不选择任何数据时,您根本不需要bind_result
.
You don't need bind_result
at all as you are inserting data and not selecting any.
但是您应该使用准备好的语句的核心功能.那就是:安全地将变量传递给语句对象,而不是将其原始"值插入SQL字符串中:
But you should use the core features of your prepared statement. That is: safely passing the variables to the statement object instead of inserting their "raw" values into the SQL string:
$sql = "INSERT INTO folders (folder_name,folder_class,folder_link) VALUES (?,?,?)";
$stmt = $this->connect->prepare($sql);
$stmt->bind_param("sss", $foldName, $foldClass, $foldLink);
$stmt->execute();
(我还没有测试过.)
这篇关于将数据插入MySql DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!