Search the Community
Showing results for tags 'update form php mysql'.
-
I have been trying to use some code to update a simple table on multiple columns. For some reason the code below will present the data but if I alter the details the data is not updated, it just refreshes in the view with the old data. The original code came from here, I've made some minor adjustments to make it 'work': http://www.phpeasystep.com/mysql/10.html Thank you for any help Gary <?php $host="xxxx"; // Host name $username="xxxx"; // Mysql username $password="xxxx"; // Mysql password $db_name="xxxx"; // Database name $tbl_name="xxxx"; // 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"> <?php $id[]=$rows['id']; 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]."'"; $result1=mysql_query($sql1); } } if($result1){ header("location:update_multiple.php"); } mysql_close();