【问题标题】:How to properly Insert javascript variable to mysql database using ajax如何使用 ajax 将 javascript 变量正确插入 mysql 数据库
【发布时间】: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-&gt;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


【解决方案1】:

您忘记准备查询了 :)

替换:

$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)');

【讨论】:

  • 谢谢。这似乎解决了上面的错误,但弹出了新的错误。 &lt;b&gt;Fatal error&lt;/b&gt;: Call to undefined function validate() in &lt;b&gt;add_stream.php&lt;/b&gt; on line &lt;b&gt;19&lt;/b&gt;&lt;br /&gt;
  • 您的 validate 函数似乎没有在您的 add_stream.php 文件中定义,请务必在您的文件中添加/包含它;),不要忘记接受并支持我的答案兄弟': )
  • 感谢您的回答。我给了你一个支持,让我找到了正确的解决方案。
【解决方案2】:

您错过了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>

【讨论】:

  • 我收到expecting ')' 错误。语法突出显示似乎不正确。 $_POST 在最后 3 个实例上是黑色的,但在第一个实例上是红色的。这个对吗?试图通过添加一些引号来纠正它,但我得到了同样的错误
  • 现在一切似乎都在代码级别上正常工作,但我的数据没有被插入到数据库中。
  • 谢谢。没有专门为我工作,但我从你的代码中得到了有用的提示,让我找到了这个api.jquery.com/jQuery.ajax 然后我删除了数组数据并使用与该 url 中的示例相同的数据。现在工作。
猜你喜欢
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多