简体   繁体   English

更新和在mysql中插入查询时出错

[英]Error in update and insert query in php mysql

I am trying to update seen(a bool in the database) if the seen button is clicked and delete a row if the delete button is clicked. 我正在尝试更新seeed(数据库中的布尔值),如果单击了seeed按钮,并且如果单击了delete按钮,则删除一行。 But it doesn't seem to work nor do I get any data(mysql_error) back. 但是它似乎没有用,也没有得到任何数据(mysql_error)。 But the last else part works and I get the data. 但是最后的另一部分有效,我得到了数据。

This is the php code : 这是php代码:

if(isset($_POST["seen"])){
    $fid=$_POST["id"];
    $sql="UPDATE feedback SET seen=1 WHERE ID=$fid";
    mysql_query($sql,$con);
    echo mysql_error($con);
} else if(isset($_POST["delete"])){
    $fid=$_POST["id"];
    $sql="DELETE from feedback WHERE ID=$fid";
    mysql_query($sql,$con);
    echo mysql_error($con);
} else{
    $sql="SELECT * FROM feedback";
    $result=mysql_query($sql,$con);
    while($row=mysql_fetch_array($result)){
        $jsonData[]=$row;
    }
    echo json_encode($jsonData);
}

and js code: 和js代码:

$("#seen").click(function(){
    $.post("main_php.php",{},function(data){
        alert(data);
    }); 
});

$("#delete").click(function(){
    var fid = $('#ID').val();
    $.post("main_php.php",{id:fid},function(data){
        console.log(data);
    }) 
});

Please tell me what may be wrong with it!!!! 请告诉我它可能有什么问题!

seen and delete are the names of buttons btw 看到和删除按钮按钮的名称

Your jQuery POST is not setting the "seen" or "delete". 您的jQuery POST未设置“已看到”或“删除”。 So when the PHP executes, it is just falling through to that last else. 因此,当PHP执行时,它就落到了最后。

In the jQuery, set "seen" or "delete" to true (or anything else, but they need to be set) and you should be good. 在jQuery中,将“ seen”或“ delete”设置为true(或其他任何设置,但需要设置它们),您应该会很好。

In Ajax submit, we have to explicitly submit the elements you want. 在Ajax提交中,我们必须显式提交所需的元素。 To fix it, edit your code like below 要解决此问题,请按如下所示编辑代码

$("#seen").click(function(){
    $.post("main_php.php",{seen:1},function(data){
        alert(data);
    }); 
});

$("#delete").click(function(){
    var fid = $('#ID').val();
    $.post("main_php.php",{id:fid,delete:1},function(data){
        console.log(data);
    }) 
});

Set the 'seen' / 'delete' in jQuery to true as they are not set. 将jQuery中的'seen'/'delete'设置为true,因为未设置它们。 That might solve the problem. 那可能会解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM