Zeno
Zeno

Reputation: 138

ID Column not updating while other columns are?

Good day.

This slight error has been bugging me for a bit and I can't seem to find what's the problem. There a 3 columns: id, last_name and first_name. The latter 2 update just fine but the id column keeps reverting back to its original value.

Ok, so here's some snippets of my code:

while($row = mysql_fetch_array($result))
{

    echo "<tr>";  
    echo "<td><input type='checkbox' name ='doc_list[]' value =". $row ."></input></td>
          <td><input type='text' maxlength='9' id='id' name ='id' value =". $row['id'] ."></input></td> 
          <td><input type='text' maxlength='32' id='last_name' name ='last_name' value =". $row['last_name'] ."></input></td>
          <td><input type='text' maxlength='32' id='first_name' name ='first_name' value =". $row['first_name'] ."></input></td>
          <td><input type='submit' value='Update'/></td>
          <td><div id='message'></div></td>";

}   

Then when the form submits:

//Extracts the information from the form
$id = clean($_POST['id']);
$first_name = clean($_POST['first_name']);
$last_name = clean($_POST['last_name']);

//Input validations
//include("../php/validate_insertion.php");


//Updates doctor info
$query1="UPDATE doctors 
     SET id= '$id', last_name='$last_name', first_name='$first_name'
     WHERE id='$id'";
$result = mysql_query($query1) or die(mysql_error());

Where's the error?

Upvotes: 0

Views: 187

Answers (2)

Abhinav Singh
Abhinav Singh

Reputation: 2651

Why are you changing id is my first question.

In any case if you really need to change id, you will have to get hold of original id for which this form was generated. One way of doing this is to put a hidden input field in the form which can give you the original id of the db row for which this form was generated.

PS: Also from security perspective it's not a great idea to accept id from a user form, depending upon which you are going to update the row. What if someone change that hidden field id to something else.

Upvotes: 1

jeroen
jeroen

Reputation: 91744

I don't really get the logic of changing the ID, but the problem is that you are not conserving the original id. You need that in the where condition to update the original record, not the record that happens to have the new ID as well.

If you really want to go this way (you'll run into trouble if there are duplicate ids...), you should add the original ID as a hidden field so that you know which record to update.

Upvotes: 2

Related Questions