Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery PHP POST错误处理_Javascript_Php_Jquery_Mysql - Fatal编程技术网

Javascript jQuery PHP POST错误处理

Javascript jQuery PHP POST错误处理,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我正在通过jQuery发送POST请求: if ( isset( $_POST['newOwner'], $_POST['uID'] ) ) : $function->updateTransport( $_POST['newOwner'], $_POST['uID'] ); endif; $.post("", { newOwner: $("#newOwner").val(), uID: uid }) 例如,当MySQL无法更新时,我如何登录jQuery?我尝试了

我正在通过jQuery发送POST请求:

if ( isset( $_POST['newOwner'], $_POST['uID'] ) ) :
    $function->updateTransport( $_POST['newOwner'], $_POST['uID'] );
endif;

$.post("", {
    newOwner: $("#newOwner").val(),
    uID: uid
})
例如,当MySQL无法更新时,我如何登录jQuery?我尝试了
.fail
,但jQuery总是在请求后显示成功

public function updateTransport( $newOwner, $uID )
{
    try
    {
        $stmt = $this->db->prepare( "UPDATE cars SET owner= ? WHERE id = ?" );
        $stmt->execute( array( $newOwner, $uID ) );
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
}

您需要发送有关执行的查询的成功和失败的特定数据。 试试这个代码

    public function updateTransport( $newOwner, $uID ){
        try
        {
            $stmt = $this->db->prepare( "UPDATE cars SET owner= ? WHERE id = ?" );
            $status = $stmt->execute( array( $newOwner, $uID ) );
            if($status){
                $ret = array(
                    'status' => 'success',
                    'message' => 'Success'
                    );
                echo json_encode($ret);
            }else{
                $ret = array(
                    'status' => 'failed',
                    'message' => 'Failed'
                    );
                echo json_encode($ret);
            }
        }
        catch(PDOException $e)
        {
            $ret = array(
                'status' => 'failed',
                'message' => $e->getMessage()
                );
            echo json_encode($ret);
        }
   }
    $.ajax({
        'url' : 'yourURL',
        data : { 'key' : value },
        success : function(data, textStatus, jqXHR){
            var dat = JSON.parse(data);
            if(dat.status == 'success'){

            }else{

            }
        },
        error : function(jqXHR, textStatus, errorThrown){
            var dat = JSON.parse(data);
            if(dat.status == 'success'){

            }else{

            }
        }
    });