0

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. But it doesn't seem to work nor do I get any data(mysql_error) back. But the last else part works and I get the data.

This is the php code :

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:

$("#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

2
  • Stop using deprecated mysql and start using mysqli or pdo Commented Jan 16, 2016 at 4:06
  • In the name of all that is good and beautiful in this world, just stop writing new PHP code that uses the deprecated mysql_ interface functions. It's 2016 already. Use PDO or mysqli. And prepared statements with bind placeholders are not that hard. Really. And I'm so going to pass in the value '1 OR 1=1 --' for id. Commented Jan 16, 2016 at 4:40

3 Answers 3

2

Your jQuery POST is not setting the "seen" or "delete". So when the PHP executes, it is just falling through to that last else.

In the jQuery, set "seen" or "delete" to true (or anything else, but they need to be set) and you should be good.

Sign up to request clarification or add additional context in comments.

1 Comment

seen and delete are buttons.Are they not set when clicked upon?
0

Set the 'seen' / 'delete' in jQuery to true as they are not set. That might solve the problem.

1 Comment

seen and delete are buttons.Are they not set when clicked upon?
0

In Ajax submit, we have to explicitly submit the elements you want. 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);
    }) 
});

5 Comments

seen and delete are buttons.Are they not set when clicked upon?
Those buttons will be submitted when the form submission happens. But what you are doing is a Ajax submit in which we have explicitly submit the elements you want
can you help me with one more question pls
just search 'Display dynamic data using jquery chart' and check the first post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.