【问题标题】:Issue with posting via Ajax to MySql通过 Ajax 发布到 MySql 的问题
【发布时间】:2016-10-08 22:55:03
【问题描述】:

尽管我很确定我的代码很好(进行了几次测试),但数据库没有更新。所以肯定有问题(异步等)。我需要帮助找出问题所在。

这是我的 Ajax 调用(通过按下保存按钮,它已经过测试并且可以很好地触发):

$.ajax({  
url: "../../../../admin/includes/classes/class.article_front_Post.php",     
type: "POST",
data: {
        'articleid': $articleid,
        'contenu': $contenu,
        'name': $name 
      }

});

这里是 ...front_Post.php 文件的内容:

include_once('../../../../init.php');

$articleid  = $_GET['articleid'];
$contenu  = $_GET['contenu'];
$name  = $_GET['name'];

// $name  = 'special1';            
// $contenu  = '<p>test</p>';
// $articleid  = '17';
// above to test the update (it works)

mysql_query("

UPDATE al_articles SET $name='$contenu'
WHERE (ArticleID='$articleid')

") or die(mysql_error());

【问题讨论】:

  • 什么是错误??
  • 没有错误 - 感谢它现在已解决。见下文。
  • 请注意:mysql_* 函数已被弃用,并已从 PHP 7 中删除,已替换为 myslqi_*PDO 函数,这些函数在使用准备好的语句和绑定参数时更加安全。
  • 感谢您的建议。

标签: javascript php mysql asynchronous


【解决方案1】:

您的 ajax 正在使用“POST”:

$.ajax({  
url: "../../../../admin/includes/classes/class.article_front_Post.php",     
type: "POST",    //<====================================================!!!!
data: {
        'articleid': $articleid,
        'contenu': $contenu,
        'name': $name 
      }
});

但是您的 PHP 使用的是“GET”,所以,将“GET”替换为“POST”:

include_once('../../../../init.php');

$articleid  = $_POST['articleid']; //<===============================
$contenu  = $_POST['contenu'];     //<===============================
$name  = $_POST['name'];           //<===============================

// $name  = 'special1';            
// $contenu  = '<p>test</p>';
// $articleid  = '17';
// above to test the update (it works)

mysql_query("

UPDATE al_articles SET $name='$contenu'
WHERE (ArticleID='$articleid')

") or die(mysql_error());

或者在您的 ajax 中将“POST”替换为“GET”。反正在URL上可以看到GET参数,所以推荐POST。

【讨论】:

  • 是的!谢谢!现在工作! (我会做功课来理解 The Post and Get 的东西!。
  • 刚刚做了,再次感谢! - 有一个超时可以防止过早进行。
猜你喜欢
  • 2015-09-13
  • 2013-01-10
  • 2010-10-17
  • 2012-04-07
  • 2022-01-18
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
相关资源
最近更新 更多