Ajax POST成功,但是sql查询未运行

时间:2019-04-04 07:44:16

标签: php jquery mysql ajax post

我有一个可正常使用的ajax jQuery <svg> .... .... <foreignObject> <div> your content</div> </foreignObject> </svg> 。或者至少,我认为可以(我收到成功消息,并在浏览器中看到POST数据),但是用于更新数据库的SQL查询无法正常工作。

-----------------带有POST请求的功能-----------------------

post

------------------- updatecheckbox.php文件------------------------ -

var  examenid=examenid;
var checked=checked;

$.ajax
({
  type:'post',
   url:'updatecheckbox.php',
   data:{examenid: examenid,
         checked: checked
      },
  success:function(msg)
  {
      //if i set a message here, the message is displayed.
  }

我希望更新查询运行并将数据库中的数字设置为0或1。但是现在什么也没发生。

谢谢。

2 个答案:

答案 0 :(得分:0)

PHP代码不会将任何值返回/回传给ajax回调,因此success函数可能正在被欺骗-使用PHP中的运算结果逻辑来确定发送回什么。

下面如何通过对ajax回调...未响应的响应消息实现stored procedures的示例

$.ajax({
    type:'post',
    url:'updatecheckbox.php',
    data:{
        examenid: examenid,
        checked: checked
    },
    success:function(msg){
        //if i set a message here, the message is displayed.
        console.info(msg)
    },
    error:function(err){
        console.warn(err);
    }
});



<?php

        $result = false;
        $checked = true;


        $link = new mysqli("127.0.0.1","root","","sprint");
        $examenid = filter_input( INPUT_POST, 'examenid', FILTER_SANITIZE_NUMBER_INT );
        $checked = filter_input( INPUT_POST, 'checked', FILTER_SANITIZE_NUMBER_INT );

        $checkDigitaal = $checked==1 ? 1 : 0;



        $sql='update `examen` set digitaal=? where examenid=?';
        $stmt=$link->prepare( $sql );

        if( $stmt && $examenid ){
            $stmt->bind_param( 'ii', $checkDigitaal, $examenid );
            $result = $stmt->execute();
            $stmt->free_result();
            $stmt->close();
        }

        $link->close();

        /* send appropriate message based upon query result */
        exit( $result ? 'happy' : 'sad' );
?>

答案 1 :(得分:-1)

我修改了几行代码,请检查是否有效

-----------------带有POST请求的功能-----------------------

var  examenid=examenid;
var checked=checked;

$.ajax({
        type:'post',
        url:'updatecheckbox.php',
        data:{"examenid": examenid, "checked": checked},
         success:function(msg){
          //if i set a message here, the message is displayed.     
     });

------------------- updatecheckbox.php文件------------------------ -

<?php
        // set defaults 
        $checkDigitaal;
        $checked = true;
        $examenid = 0;

        //open connection to database
        $link = new mysqli("127.0.0.1","root","","sprint");
        if (!$link) 
        {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
            exit;
        }

    //check if the POST value is set
           if(isset($_POST['examenid'])){ $examenid = $_POST['examenid'];}
           if(isset($_POST['checked'])) {$checked = $_POST['checked'];}

        if($checked){$checkDigitaal = 0;}
        else{$checkDigitaal = 1;}

        //update query database
        $query="update examen set digitaal=$checkDigitaal where examenid=$examenid";  //your query

        if ($result = $link->query($query)) 
        {
                /* free result set */
            $result->free();
        }   

        mysqli_close($link);
 ?>