I can't seem to be able to update any records except the first one. I am not sure how to modify any of the displayed records.
<?php
if(isset($_POST["action"]) == "update")
{
$id = $_POST['m_id'][0];
$type = $_POST['type'][0];
// if I echo $id & $type, it only gives me the first record.**
mysql_query("
UPDATE membership_type
SET mt_type ='$type'
WHERE mt_id = '$id'"
);
}
?>
ALl of this is within the same php page.
<form name=form action='' method='post'>
<?php
$result=mysql_query("SELECT * FROM membership_type;");
while($rows=mysql_fetch_array($result))
{ ?>
<input size=35 class=textField type=text name='type[]' value='<?php echo $rows['mt_type']; ?>'>
<input type=hidden name='m_id[]' value="<?php echo $rows['mt_id']; ?>">
<input type=submit value="Update">
<?php
}
?>
How do I edit any of the displayed records by simply clicking Update button???
First: You should NEVER use the mysql_* functions as they are deprecated.
Second: Try this code:
Third: In order to add more security (this code is vulnerable), try mysqli_prepare
Replace it. Hope this works.
Try this :
Only the first record is updated on every form submission because you have set
$id = $_POST['m_id'][0]
, which contains the value of the firsttype[]
textbox. To update all the other records as well, loop through$_POST['m_id']
.You HTML was malformed and you were passing as an array but then only using the first element. Consider:
Then the server script:
There are other things you could be doing here, eg. prepared statements, but this should work.