Tell me why the database update does not work? Here is the code, when it is executed, for some reason, the data is deleted, and not updated, what could be the problem? Here is the code itself:

<?php include("bd.php"); if ($_POST['id']) { $id = $_POST['id']; $status = $_POST['status']; $id = mysql_escape_String($id); echo "update users set status='$status' where id='$id'"; mysql_query("update users set status='$status' where id='$id'"); } ?> 

UPD:

 $(function() { $("h4").click(function() { var titleid = $(this).attr("id"); var sid=titleid.split("title"); var id=sid[1]; var dataString = 'id='+ id ; var parent = $(this).parent(); $(this).hide(); $("#formbox"+id).show(); return false; }); $(".save").click(function() { var A=$(this).parent().parent(); var X=A.attr('id'); var d=X.split("formbox"); var id=d[1]; var Z=$("#"+X+" input.content").val(); var dataString = 'id='+ id +'$status='+Z ; $.ajax({ type: "POST", url: "status.php", cache: false, success: function(data) { A.hide(); $("#title"+id).html(Z); $("#title"+id).show(); } }); return false; }); $(".cancel").click(function() { var A=$(this).parent().parent(); var X= A.attr("id"); var d=X.split("formbox"); var id=d[1]; var parent = $(this).parent(); $("#title"+id).show(); A.hide(); return false; }); }); 

The code manipulates the div, and then ajax updates the data.

  • It seems to be correct, but what do you have in the database and what do you pass in the parameters? - Gorets
  • Offhand: if id is numeric, then quotes are not needed. And it is necessary not Muskl_escape_string, and intval () - Oleg Arkhipov
  • Integer values ​​are best escaped with (int)$_POST['id'] , string values ​​are more correctly escaped with mysql_real_escape_string() , and SQL service words are usually written in capital letters: UPDATE users SET status='$status' WHERE id=$id (numeric variables - without quotes, text variables - in quotes) - and you won’t get confused, and the code will be more readable - Metal Heart

2 answers 2

1. The problem with quotes is that the value of variables is not substituted into your query, so the query in the database looks for id = $ id - as a string, and of course it does not find it. The query should look like this

 "update users set status='".$status."' where id='".$id."'" 

2. Print the value of the variables before the query - do you have them?

    To begin with - in your case, it does not matter at all how you write down: either id = '". $ Id."' , Or id = '$ id' , or just id = $ id . The query will produce the same result.


    In order not to guess where you may have a problem, make a check with the issuance of both a positive result and a negative one.

     if(isset($_POST['id'])){ // ... } else { echo 'Параметра ID - ёк!'; } //.... $res = mysql_query(' .... '); if($res) { echo 'Прокатило;'; } else { echo 'Хьюстон! У нас проблемы!'.mysql_error(); } 

    Try to write the names of the functions correctly. Though mysql_escape_ S tring, but nevertheless everything is better with a small letter. But all this is a lyrical digression, and the following is alarming me in your question:

    when it is executed for some reason the data is deleted and not updated

    From this place, I would like more. What does it mean to delete? Is the record deleted from the database or is the data in this record deleted? $ _POST ['status'] - is it an int value or a string? Besides $ _POST ['status'] , is there any other data that updates the record in the table?

    • one
      The status is a string, when updating to ajax, the data is sent to the script that should put the data in the table, but it removes it on the contrary, no, only the status is updated. - OverLoader
    • @ raptor96, I repeat the question ... Is the status is a string? Because I have a suspicion that this is the case. When working there is a problem with the encoding. I think this is precisely because of this. By the way, why don't you screen data with status? - Deonis
    • one
      Your suspicions are correct about the fact that the status is a string, and how can you solve problems with the encoding during transmission? - OverLoader
    • one
      Documents (ideally) you should be UTF-8 encoded. If this is the case, then the first thing you can try is immediately after connecting to the database, add the line: mysql_query ("SET NAMES 'utf8'"); If the drug did not help, then try re-encoding the status data: $ status = iconv ('cp1251', 'utf-8', $ _ POST ['status']); In this case, it is possible that it will be necessary to remove the date mysql_query ("SET NAMES 'utf8'"); In general, the problem you have is not with quotes, as colleagues said above (put them quietly on the int values), but with porridge in the encoding. I am already sure. - Deonis
    • PS And install FireBug. If you had this plugin, then you would notice that the data you are transmitting has the form " % D0% 9F% D1% 80% D0% B8% D0% B2% D0% B5% D1% 82 " . This would immediately suggest an encoding. - Deonis