Link to home
Create AccountLog in
Avatar of koochy
koochyFlag 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();
?>
Avatar of Michael701
Michael701
Flag of United States of America image

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
Avatar of liveaspankaj
liveaspankaj
Flag of Nepal image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of koochy

ASKER

works like a charm !!!! what did you change I see on new statement "extract($_POST, EXTR_SKIP);" what does this do?
FYI: You do NOT want id to be a text box that the client can modify. It should be a hidden field.