如何使用ajax将javascript变量正确插入mysql数据库
我正在尝试实现这个问题中提出的解决方案 到目前为止,我的代码是这样的 JS PHP如何使用ajax将javascript变量正确插入mysql数据库,javascript,php,mysql,ajax,Javascript,Php,Mysql,Ajax,我正在尝试实现这个问题中提出的解决方案 到目前为止,我的代码是这样的 JS PHP //连接到数据库 ... //插入数据 $data=验证($\u POST); $stmt=$dbh->('INSERT-INTO-Streams,theStream,theLabel,theLocation,theStatus) 值(:流、:标签、:位置、:状态)); $stmt->execute($data); if($conn->query($sql)==TRUE){ echo“新流添加成功”; }否则{
//连接到数据库
...
//插入数据
$data=验证($\u POST);
$stmt=$dbh->('INSERT-INTO-Streams,theStream,theLabel,theLocation,theStatus)
值(:流、:标签、:位置、:状态));
$stmt->execute($data);
if($conn->query($sql)==TRUE){
echo“新流添加成功”;
}否则{
echo“Error:”.$sql.“
”$conn->Error;
}
$conn->close();
我得到了这个错误。(第21行指的是$stmt=$dbh->)
加载的数据:分析错误:语法错误,意外的“(”,在第21行的add_stream.php中需要T_字符串或T_变量或“{”或“$”
我无法找出我的代码有什么问题。我检查了开/闭圆括号的配对,它已正确配对
我遗漏了什么?您忘记准备查询:) 替换:
$stmt = $dbh->('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus)
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');
为此:
$stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus)
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');
您错过了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);
对于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
}
})
})
<?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>
总代码:
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
}
})
})
<?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>
形式
var postData={
流:“qq”,
标签:“ww”,
位置:'ee',
状态:“rr”,
提交:“提交”
};
$(“.form”)。单击(函数(){
$.ajax({
类型:'post',
url:this.url,
数据:postData,
成功:功能(数据){
//成功后要运行的代码
}
})
})
我想你该戴眼镜了。应该是$stmt=$dbh->prepare('INSERT-INTO-Streams(theStream,theLabel,theLocation,theStatus)值(:theStream,:theLabel,:theLocation,:theStatus)'))
。您忘记的是命令prepare
。顺便说一句。我没有使用PDO连接到数据库。我使用的是MySQLi过程,正如这里所述,那么您在脚本中做的是错误的。您使用的是MySQLi OOP,而不是过程。另外,请使用php.net手册,而不是w3schools.com。谢谢。这似乎可以解决上面的错误,但新的错误是pops up.致命错误:调用未定义的函数validate()在第19行的add_stream.php
中,您的add_stream.php文件中似乎没有定义验证函数,请确保在您的文件中添加/包含它;),不要忘记接受并更新我的答案,兄弟:)谢谢您的回答。我为您找到正确的解决方案向上投了一票。我得到了expecting')
错误。语法突出显示似乎不正确。$\u POST在最后3个实例上是黑色的,但在第一个实例上是红色的。是否正确?尝试通过添加一些引号来更正它,但我得到了相同的错误现在在代码级别上一切似乎都正常,但我的数据没有插入到数据库中。谢谢。这对我来说不是特别有效,但我从y得到了有用的提示我们的代码让我找到了这个,然后我删除了数组数据,并按照url中的示例使用相同的数据。
<?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>