【发布时间】:2015-08-16 13:19:03
【问题描述】:
我正在尝试实施此问题中发布的解决方案 Best approach to add records into DB using php/ajax/mysql?
到目前为止我的代码是这样的
JS
function FromFlash(m1, m2, m3, m4){
var postData = {
theStream: m1,
theLabel: m2,
theLocation: m4,
theStatus: m3
};
$.post('add_stream.php', postData)
.done(function(response) {
alert("Data Loaded: " + response);
});
}
PHP
//Connect to DB
...
// INSERT DATA
$data = validate($_POST);
$stmt = $dbh->('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus)
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');
$stmt->execute($data);
if ($conn->query($sql) === TRUE) {
echo "New stream added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
我收到此错误。 (第 21 行指的是 $stmt = $dbh->)
Data Loaded: <br />
<b>Parse error</b>: syntax error, unexpected '(', expecting T_STRING or T_VARIABLE or '{' or '$' in <b>add_stream.php</b> on line <b>21</b><br />
我无法弄清楚我的代码有什么问题。我检查了开/关括号的配对,它是正确配对的
我错过了什么?
【问题讨论】:
-
我想你该戴眼镜了。应该是
$stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus) VALUES (:theStream, :theLabel, :theLocation, :theStatus)');。你忘记的是命令prepare。 -
顺便说一句。我没有使用 PDO 连接到数据库。我使用 MySQLi 程序,如此处所述w3schools.com/php/php_mysql_insert.asp
-
那么你在脚本中做错了。您使用的是 MySQLi OOP,而不是程序。还要使用 php.net 手册,而不是 w3schools.com。
标签: javascript php mysql ajax