【问题标题】:Jquery ajax inserting and editingjQuery ajax 插入和编辑
【发布时间】:2016-07-20 17:08:39
【问题描述】:

在下面的函数中,我有两个发布请求。一种是在数据库中插入值(submit.php),另一种是根据businessid(edit.php)编辑数据库中的现有值。但是,该脚本仅执行第一部分。也就是说,当“keuze”选择并在提交时单击时,脚本很好地插入数据库。 But when something else than "keuze" is chosen and submit is clicked, the script does not edit the records in the database.我该怎么做才能解决这个问题?

 submitformfunction: function(){

     $(document).on('submit', '#form', function(){

        var data = $(this).serialize();
        var target = $('#busselect option:selected').val();
        var businessid = $('#busselect').children(":selected").attr("id");

        if(target == 'keuze'){

            $.ajax({

              type : 'POST',
              url  : 'submit.php',
              data : data,
              success :  function(data)
                   {
                        alert('Company added');
                   },
                error: function( jqXhr, textStatus, errorThrown ){
                    console.log( errorThrown );
                }
            });

        }
        else
        {

            $.ajax({

              type : 'POST',
              url  : 'edit.php',
              data : businessid+data,
              success :  function(data)
                   {
                        alert('Company modified');
                   },
                error: function( jqXhr, textStatus, errorThrown ){
                    console.log( errorThrown );
                }
            });
        }

    });

},

提交.php:

<?php

$mysqli = new mysqli("localhost", "root", "", "brandveiligheid");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

if($_POST )
{

     $naam = addslashes(htmlentities($_POST['uitgevoerd_door_naam']));


     $mysqli->query("INSERT INTO form (uitgevoerd_door_naam) 
     VALUES ('$naam')");

}

mysqli_close($mysqli);

?>

edit.php:

<?php

$mysqli = new mysqli("localhost", "root", "", "brandveiligheid");

if ($mysqli->connect_errno) {
     printf("Connect failed: %s\n", $mysqli->connect_error);
     exit();
}

if($_POST )
{

    $naam = addslashes(htmlentities($_POST['uitgevoerd_door_naam']));


    $mysqli->query("UPDATE form SET uitgevoerd_door_naam='$naam' WHERE id='$_POST[business_id]'");

}

mysqli_close($mysqli);

?>

【问题讨论】:

  • 你说过“不更新”。 edit.php 文件是否会触发?您是否使用浏览器的开发人员“网络”选项卡进行故障排除? AJAX 调用会出去吗?
  • 您很容易受到sql injection attaacks 的攻击,并且您的edit ajax 调用失败,因为您传入了test+businessidtest 似乎不存在于您的代码中,并且会生成一个 PHP 无法识别为 key=value 表单提交的数字/字符串,从而将 $_POST 留空。
  • 仅供参考,很好地格式化了你的代码——它看起来很不错!您可能会考虑三件事:双换行符只是创建开放空间,所以删除双换行符 - 使您的 if() { 格式一致 - 在某些地方它在同一行,在某些地方 { 在单独一行 - 最后,不要在 PHP 文件的末尾使用 ?&gt;。现在认为最好的做法是省略 php 文件末尾的那些。
  • 只是为了确认一下,您在 if 循环之前尝试过alert(target); 吗?当您选择 'keuze' 以外的选项时会发生什么?
  • 亲爱的 Shrikant,是的,我试过了,它产生了正确的值。此外,@cale_b tnx 提到了网络选项卡。 ajax 调用确实会输出到 edit.php。

标签: php jquery mysql dropdown


【解决方案1】:

查询中似乎存在问题,因为 '$_POST[business_id]' 将被假定为 varchar 而 id 的数据类型必须在数据库中为 int。下面试试;

"UPDATE form SET uitgevoerd_door_naam='$naam' WHERE id=addslashes(htmlentities($_POST[business_id]))"

还要始终确保您的查询安全;)。

【讨论】:

  • 感谢您对 addlashes 和 htmlentities 的注释 :)。但是,我尝试了您的解决方案,但仍然无法编辑。
  • addslashes(htmlentites(...)) 对此进行转义是完全不合适的。您应该使用准备好的语句,但更快的解决方法是:"UPDATE form SET uitgevoerd_door_naam='$naam' WHERE id='" . $mysqli-&gt;real_escape_string($_POST['business_id']) . "'" 或者,如果 id 是一个 int 列,请使用 intval 而不是 $mysqli-&gt;real_escape_string
【解决方案2】:

执行几个步骤,让我知道你得到了什么, 1.在edit.php中

if($_POST )
{

    $naam = addslashes(htmlentities($_POST['uitgevoerd_door_naam']));

echo $query = "UPDATE form SET uitgevoerd_door_naam='$naam' WHERE id='$_POST[business_id]'";

    $mysqli->query($query);

}
  1. 在 js 中: 别的 {

        $.ajax({
    
          type : 'POST',
          url  : 'edit.php',
          data : test+businessid,
          success :  function(test)
               {
                    alert(test);
                    alert('Company modified');
               },
            error: function( jqXhr, textStatus, errorThrown ){
                console.log( errorThrown );
            }
        });
    }
    

让我知道你在警报中得到了什么。

【讨论】:

  • 也许是一个非常愚蠢的问题,但是我需要做什么才能看到警报中的回显输出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 2014-09-26
相关资源
最近更新 更多