title: Update multiple rows in mysql with php
body: <p>Here is my code below. The problem is when I try to update info it instead clears all records and does not update. How can I get this script to update and not clear. Also, I have used this before and it worked fine but all the sudden it doesn't.. I might have removed something important.</p>
  
  <pre><code>&lt;strong&gt;Update multiple rows in mysql&lt;/strong&gt;&lt;br&gt; 
  
  &lt;?php
  $mysql_host = "mysql.com";
  $mysql_user = "username";
  $mysql_pass = "password";
  $mysql_database = "dbname";
  $tbl_name="test_mysql"; // Table name
  
  // Connect to server and select databse.
  mysql_connect("$mysql_host", "$mysql_user", "$mysql_pass")or die("cannot connect"); 
  mysql_select_db("$mysql_database")or die("cannot select DB");
  
  $sql="SELECT * FROM $tbl_name";
  $result=mysql_query($sql);
  
  // Count table rows 
  $count=mysql_num_rows($result);
  $id = array(); 
  ?&gt;
  &lt;table width="500" border="0" cellspacing="1" cellpadding="0"&gt;
  &lt;form name="form1" method="post" action=""&gt;
  &lt;tr&gt; 
  &lt;td&gt;
  &lt;table width="500" border="0" cellspacing="1" cellpadding="0"&gt;
  
  
  &lt;tr&gt;
  &lt;td align="center"&gt;&lt;strong&gt;Id&lt;/strong&gt;&lt;/td&gt;
  &lt;td align="center"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
  &lt;td align="center"&gt;&lt;strong&gt;Lastname&lt;/strong&gt;&lt;/td&gt;
  &lt;td align="center"&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php
  while($rows=mysql_fetch_array($result)){
  ?&gt;
  &lt;tr&gt;
  &lt;td align="center"&gt;&lt;? $id[]=$rows['id']; ?&gt;&lt;? echo $rows['id']; ?&gt;&lt;/td&gt;
  &lt;td align="center"&gt;&lt;input name="name[]" type="text" id="name" value="&lt;? echo     $rows['name']; ?&gt;"&gt;&lt;/td&gt;
  &lt;td align="center"&gt;&lt;input name="lastname[]" type="text" id="lastname" value="&lt;? echo $rows['lastname']; ?&gt;"&gt;&lt;/td&gt;
  &lt;td align="center"&gt;&lt;input name="email[]" type="text" id="email" value="&lt;? echo    $rows['email']; ?&gt;"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php
  }
  ?&gt;
  &lt;tr&gt;
  &lt;td colspan="4" align="center"&gt;&lt;input type="submit" name="Submit" value="Submit"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/form&gt;
  &lt;/table&gt;
  &lt;?php
  // Check if button name "Submit" is active, do this 
  if(isset($_POST['Submit'])){
  for($i=0;$i&lt;$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){
  echo "Good";
  ////header("location:update_multiple.php");
  }
  mysql_close();
  ?&gt;
  </code></pre>