【发布时间】:2015-01-29 23:13:45
【问题描述】:
我想通过 PHP 将一些数据从 jQueryMobil.listwidget 发送到 mysql 数据库。
我像这样获取并发布我的列表项:
function getItems()
{
var listview_array = new Array();
$( "#itemList li" ).each(function(index) {
listview_el = new Object();
listview_el.id = index;
listview_el.name=$(this).text();
listview_el.owner="owner";
listview_array.push(listview_el);
});
var stringifyObject = JSON.stringify(listview_array);
//alert(stringifyObject);
$.ajax({
type: "POST",
dataType: 'json',
url: "insert.php",
data: { mydata: stringifyObject },
});
//showItems();
}
我想将我的 json 对象添加到存在的 mysql 数据库/表中。根据我的请求,我的数据已发送,但 if(prepStmnt) 从未成功。
<?php
$con = new mysqli($servername, $username, $password, $dbname);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
echo "preps";
if($preparedStatement = $con->prepare('INSERT INTO Einkaufsliste (item, owner) VALUES (:name, :owner)')){
$preparedStatement->execute(json_decode($_POST["mydata"], true));
$preparedStatement->close();
echo "done";
};
$con->close();
?>
您能告诉我为什么没有数据存储在我的数据库中吗?
【问题讨论】:
-
您没有绑定变量
:name和:owner,因此没有插入它们 -
但是,如果它们是固定输入,则不会提交任何数据
-
if($preparedStatement = $con->prepare('INSERT INTO Einkaufsliste (item, owner) VALUES ("milk", "me")')){
-
在您的 MySQLi 语句中添加一些错误检查或查看错误日志,您可能会找到答案。
-
检查:php.net/manual/es/mysqli-stmt.execute.php ...
execute()不能包含任何参数,您可能需要在执行准备好的语句之前绑定一些参数
标签: php mysql json html jquery-mobile