[英]How to properly Insert javascript variable to mysql database using ajax
Im trying to implement the solution posted in this question Best approach to add records into DB using php/ajax/mysql? 我正在尝试实现此问题中发布的解决方案使用php / ajax / mysql将记录添加到数据库的最佳方法?
My code so far is like this 到目前为止,我的代码是这样的
JS 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 的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();
I am getting this error. 我收到此错误。 (line 21 refers to $stmt = $dbh-> ) (第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 />
I can't figure out what is wrong with my code. 我不知道我的代码出了什么问题。 I checked pairing of open/close parenthesis and it is properly paired 我检查了开/闭括号的配对,并且配对正确
What am I missing? 我想念什么?
Your forgot to prepare your query :) 您忘了准备查询:)
replace : 替换:
$stmt = $dbh->('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus)
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');
With this : 有了这个 :
$stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus)
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');
You missed prepare()
and $data should be an array of place holders. 您错过了prepare()
并且$ data应该是一个占位符数组。
$data = array(
':theStream'=>$_POST['theStream'],
':theLabel'=>$_POST['theLabel'],
':theLocation'=>$_POST['theLocation'],
':theStatus'=>$_POST['theStatus']
);
$stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus)
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');
$stmt->execute($data);
For ajax: 对于ajax:
var postData = {
theStream: m1,
theLabel: m2,
theLocation: m4,
theStatus: m3
};
$(".form").submit(function(){
$.ajax({
type:'post',
url:'target.php',
data: postData,
success:function(data){
//code to run after success
}
})
})
Total code: 总代码:
<?php
include 'PDODB.php';
if(isset($_POST['submit'])){
$data = array(
':theStream'=>$_POST['theStream'],
':theLabel'=>$_POST['theLabel'],
':theLocation'=>$_POST['theLocation'],
':theStatus'=>$_POST['theStatus']
);
$stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus)
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');
$stmt->execute($data);
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="form">Form</button>
<script>
var postData = {
theStream: 'qq',
theLabel: 'ww',
theLocation: 'ee',
theStatus: 'rr',
submit: 'submit'
};
$(".form").click(function(){
$.ajax({
type:'post',
url:this.url,
data: postData,
success:function(data){
//code to run after success
}
})
})
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.