0

I'm having problem on duplication of data when updating in row of mysql. I am changing bed of a user where I can get at table1 the field fname, which is the names of user. Then send to table2, then already get the fname using rfname='$fname', This is my html getting a pop up that get the list of room by $roommedical. Iwant To not duplicate the entry. or how can i delete first the past row value before updating it to other row., The value on roommedical is default the only i want to change is rfname.

<label class="selectDrop" >Check This For Room:</label>
<input class="selectDrop" type="checkbox" name="statmed" value="active" id="statmed" />
<label class="selectDrop11" >Medical Ward:</label>
<input class="selectDrop11" type=text id=medbed name=roommedical value="<?php echo $roommedical?>" placeholder="Select Medical Bed">
<!--BUTTON--><input class="selectDrop11" type="button" name="choice" onClick="selectValue('medbed')" value="Check">

And This is my submit form php

$roommedical = clean($_POST['roommedical']);
$statmed = clean($_POST['statmed']);
mysql_query("UPDATE room SET rfname='$fname',statmed='$statmed' Where  roommedical='$roommedical'");

When updating the patient name is duplicating the the red box line is the example of duplicating of my entry This is the picture

enter image description here

Thank you. I am bad at this, I know. Please understand me.

Charles
  • 21
  • 3

1 Answers1

0

If the patient is moving to a different bed, you need to clear out the old bed before updating the new one. So do:

mysql_query("UPDATE room SET rfname='',statmed='None' Where  rfname='$fname'");
mysql_query("UPDATE room SET rfname='$fname',statmed='$statmed' Where  roommedical='$roommedical'");

BTW, in general it's preferable to use the id from the first table as the foreign key in the second table, not the name.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • @Charles If that answers the question you should accept the answer. http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work Also depending on what `clean` does you may be open to SQL injections. I'd recommend updating to parameterized queries. – chris85 Jan 10 '17 at 01:34