Avatar of koochy
koochy
Flag for United States of America asked on

update mutiple record at one time

I have a need to an end user to be able to update multiple records from 1 form

I found a multiple update through a search and It pulls the data from the table ok but when I change any of the fields and click submit  it redisplays the orginal information and doesn't update the table. I am probably missing 1 little thing but banging my head on the wall isn't helping .. so I am asking you "the experts" what I am missing!!  thanks in advance ... here is the code I am using

<strong>Update multiple rows in mysql</strong><br>

<?php
$db_host = 'localhost';
$db_user = 'butch';
$db_pass = 'brewser';
$db_name = 'iwe';
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$db_host", "$db_user", "$db_pass")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
echo $sql1;
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:dgdedit.php");
}
mysql_close();
?>
PHP

Avatar of undefined
Last Comment
Michael701

8/22/2022 - Mon
Michael701

you never stored the id as a field, this isn't quite right

<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>

maybe you mean this

<td align="center"><input type="hidden" name="id[]" value="<?php echo $rows['id']; ?>"><?php echo $rows['id']; ?></td>
ASKER CERTIFIED SOLUTION
liveaspankaj

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
koochy

ASKER
works like a charm !!!! what did you change I see on new statement "extract($_POST, EXTR_SKIP);" what does this do?
Michael701

FYI: You do NOT want id to be a text box that the client can modify. It should be a hidden field.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23