Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Loop not working 1

Status
Not open for further replies.

bazaroff

Technical User
Sep 12, 2006
10
US
This code looks like what I need to happen, but before I modified it to use in my web app. I tried it as is. It did not work but I can't figure out why.

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

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")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"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<? 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]'";
$result1=mysql_query($sql1);
}
}

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


In trying to fix it, I changed the " if($Submit)...." to "if($Submit !='false')..." which did seem to execute an update query, but it erased all the data from the table (or more accurately updated every field to "")

I then instead of running an update had the page display at the bottom each looped update query, and it showed the following...

UPDATE test_mysql SET name= '', lastname='', email='' WHERE id='1'
UPDATE test_mysql SET name= '', lastname='', email='' WHERE id='2'
UPDATE test_mysql SET name= '', lastname='', email='' WHERE id='3'
UPDATE test_mysql SET name= '', lastname='', email='' WHERE id='4'
UPDATE test_mysql SET name= '', lastname='', email='' WHERE id='5'...

Any thought or help on this would be greatly appreciated.
Thanks
 
I would set
Code:
echo "<form name="Something" method="POST" action="'.$_SERVER['PHP_SELF'].">";
Then the page which displays the form would do updates before drawing the form, then it would draw the appropriate information depending on which button was clicked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top