简体   繁体   English

使用Ajax更新MySQL数据库

[英]Updating MySQL Database with ajax

I'm trying to send a JS array to a .php file on the click of a button, and run the php file's code on the click, using the array that was sent. 我试图通过单击按钮将JS数组发送到.php文件,并使用发送的数组在单击时运行php文件的代码。

I have a button: 我有一个按钮:

<button id="submit" class="button1" >Submit<span></span></button>

Which I try to run this JS ajax with: 我尝试使用以下命令运行此JS ajax:

$('#submit').click(function() {
  $.ajax({
    method: "POST",
    url: "submit.php",
    data: selectedImageArray
  }).done(function( msg ) {
    alert( "Data Saved: " + msg );
  });
});

In my submit.php file, I connect to the database, then have this: 在我的Submit.php文件中,我连接到数据库,然后执行以下操作:

    $array = $_POST['selectedImageArray'];

    $sql = $dbh->prepare("INSERT INTO pending_trades (steam_id, trade_items, is_giving, is_receiving, has_started) VALUES (:steamid, :itemlist, '1', '0', '0')");

    $sql->bindParam(':steamid', $steamprofile['steamid']);
    $sql->bindParam(':itemlist', $array);

    $sql->execute();


}

When I click the "submit" button, I get the message saying "Data saved", but nothing is happening with my database. 当我单击“提交”按钮时,出现消息“数据已保存”,但是数据库没有任何反应。 Should clicking the button run the submit.php code? 是否应该单击按钮运行Submit.php代码?

You have a syntax error with your JS. 您的JS语法错误。 Also, since #submit is an ID, qualifying it with button is unnecessary. 另外,由于#submit是ID,因此不需要使用button限定它。

Your code should read: 您的代码应为:

$('#submit').click(function() {
  $.ajax({
    method: "POST",
    url: "submit.php",
    data: selectedImageArray
  }).done(function( msg ) {
    alert( "Data Saved: " + msg );
  });
});

Edit 1: 编辑1:

I see that you have incorporated the above code into your JS. 我看到您已经将上述代码合并到了JS中。 Now on to your problem on the server side (PHP/SQL). 现在在服务器端解决您的问题(PHP / SQL)。

Firstly, why are you decoding $_POST[selectedImageArray] then re-encoding it? 首先,为什么要解码$_POST[selectedImageArray]然后重新编码? Doesn't make sense to me. 对我来说没有意义。

Secondly, what is the value of $steamprofile['steamid'] ? 其次, $steamprofile['steamid']的值是多少?

Thirdly, what is the schema of your SQL table? 第三,您的SQL表的架构是什么? Are you sure you're not violating any unique / not null / fk constraint? 您确定您没有违反任何唯一/不为空/ fk约束吗?

Do yourself a favour, and use a debugging/logging solution on the server-side. 帮自己一个忙,并在服务器端使用调试/日志记录解决方案。 It can be as simple as printing debug lines into a log file so you can inspect later on to find out what is happening. 就像将调试行打印到日志文件中一样简单,因此您以后可以检查以了解发生了什么。

What I'd do: 我会做什么:

  • check value of $_POST[selectedImageArray] . 检查$_POST[selectedImageArray] It should be a string. 应该是一个字符串。
  • check the value for $array . 检查$array的值。 It should be an array. 它应该是一个数组。
  • read the error logs to see what else it says. 阅读错误日志,以了解其他内容。
  • $sql->execute(); returns a bool (TRUE on success). 返回bool (成功bool TRUE)。 Use it in your script to return either success or failure message. 在脚本中使用它可以返回成功或失败消息。
  • Log down the value of $sql->errorCode(); 记下$sql->errorCode();的值$sql->errorCode(); on failure. 失败。

With all the above, you should have enough to troubleshoot what went wrong there. 有了以上所有内容,您就应该有足够的能力来解决哪里出了问题。

$('#submit').click(function(){
  $.ajax({
      method: "POST",
      url: "submit.php",
      data: {selectedImageArray: selectedImageArray}, // this is supposed to be JSON
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
  });
});

And

$_POST['selectedImageArray'] // see the quotes

I suppose that steam_id is a primary key with auto increment, so if you are trying to insert a row to database with the same value for steam_id , then it is a possibility that your data cannot be added. 我想steam_id是具有自动增量功能的主键,因此,如果您尝试将具有相同steam_id值的行插入数据库,则可能无法添加数据。

To debug in a better way, try to generate the query in the server and echo it. 若要以更好的方式进行调试,请尝试在服务器中生成查询并回显它。 Then try to run the same query in your MySQL console to find the response. 然后尝试在MySQL控制台中运行相同的查询以找到响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM