尽管POST vars正确传递,但PDO更新失败并使用Ajax

时间:2018-05-01 21:47:38

标签: php ajax pdo

表单提交和数据库更新在独立时按预期执行。当使用ajax时,它提交了正确的POST变量,但我最终得到了一个PDO异常我不明白如何解决,因为它在没有制作ajax帖子时工作正常。 PDO和Ajax都是新手。

    <form id="privacy" action="privacy.php" method="post">
        <input type="hidden" name="id" class="id" value="<?php echo $user->id; ?>">
        <label>
            <input type="radio" name="name" class="name" value="private" />
            <img src="img/lock.png">
        </label>
        <label>
            <input type="radio" name="name" class="name" value="friends"/>
            <img src="img/friends.png">
        </label>
        <label>
            <input type="radio" name="name" class="name" value="fof"/>
            <img src="img/friends-of-friends.png">
        </label>
    </form>

    <script>
        $(function() {
        $("#privacy").change(function() {
            var name = $(".name:checked").val();
            var id = $(".id").val();
            $.ajax({
                type: "POST", 
                url: "privacy.php",  
                data: {"name":name, "id":id},  
                success: function() { 
                    alert("form submitted: "+ id +" name:" + name);
                    }
                });
             });
         });
    </script>

更新功能

    public function Privacy($id,$name)
{

        // Building INSERT QUERY
        $sql = "UPDATE user_privacy";

        // keep only wanted fields (change this) to 'field' => value
        $data = array_filter($_POST);

        $id = $_POST['id'];
        $cols = array('id' => 'id','name' => 'name');

        $count = 0;
        $i = 0;

        foreach ($data as $key => $value) {
            if(array_key_exists($key, $cols) && $cols[$key] != 'id'){
                $sql .= ' SET ' . $cols[$key] . ' = :'. $cols[$key] . ' WHERE parent_id = :id';
                unset($data[$key]);
                $count = count($data);

            } else { 
                if($count !== 0){
                   $i++;
                   $sql .= " $key = '$value' ";
                   $sql .= $i == $count ? " " : " AND ";
                }

            }
        }
        $query = $this->db->prepare($sql);
        $query->bindParam("id", $id, PDO::PARAM_STR);
        $query->bindParam(':'.$key, $value, PDO::PARAM_STR);
        $query->execute();
}

   and the exception

致命错误:未捕获PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中出错;检查与您的MariaDB服务器版本对应的手册,以便在C:\ xampp2 \ htdocs \ platform \ library \ library.php的第1行'id ='1''附近使用正确的语法:151 堆栈跟踪: 0 C:\ xampp2 \ htdocs \ platform \ library \ library.php(151):PDO-&gt; prepare('UPDATE user_pri ...') 1 C:\ xampp2 \ htdocs \ platform \ privacy.php(24):DemoLib-&gt;隐私('1','私人',NULL) 2 {主}   在 151

C C:\ xampp2 \ htdocs \ platform \ library \ library.php 中抛出

那么如果我在添加ajax post submit之前正确更新,我怎么能有错误呢?我可以看到ajax传递的是post数据('1','private',NULll)。太困惑了!

1 个答案:

答案 0 :(得分:0)

经过几天的努力,我终于明白了。仍然不知道问题是什么,但现在这是工作代码。如前所述,除了“id”字段之外总是只有一个字段,但另一个字段是动态的...当我开发更多技能时,我肯定会进一步修改它以清理它并正确地构造它。

    $(function() {
        $("#privacy").change(function() {
             var uname = $(".name:checked").val();
             var uid = $(".id").val();
        $.ajax({
            type: "POST", 
            url: "privacy.php",  
            data: {"name":uname, "uid":uid},  
            success: function() { 
                }
            });
       });
    });

处理......

    $uid = $_POST['uid'];
    $sql = "UPDATE user_privacy";
        $data = array_filter($_POST);
        foreach ($data as $key => $value) {
            if($key != 'uid'){  
                $field = $key;
                $fieldValue = $value;
                $sql .= ' SET '. $key.' = :'.$key;
            }
        }
        $sql .= ' WHERE parentid = :uid';

        $query = $db->prepare($sql);
        $query->bindParam(':uid', $uid, PDO::PARAM_STR);
        $query->bindParam(':'.$field, $fieldValue, PDO::PARAM_STR);
        $query->execute();