Russia Posted January 29, 2011 Share Posted January 29, 2011 Hello, I have this simple php form that updates the table, and also shows what the information in it is. Here it is: <strong>Update multiple rows in mysql</strong><br> <?php $host="localhost"; // Host name $username=""; // 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>Month</strong> <strong>Date</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="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>">- <input name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>"> </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 (isset($_POST['Submit'])) { for($i=0;$i<$count;$i++){ $month = $_POST['month']; $date = $_POST['date']; $lastname = $_POST['lastname']; $email = $_POST['email']; $name = $month."<br>".$date; $sql1="UPDATE $tbl_name SET name='$name[$i]', month='$month[$i]', date='$date[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } if($result1){ header("location:update2.php"); } ?> <?php $result = mysql_query("SELECT * FROM test_mysql") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Name</th> <th>Age</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['name']; echo "</td><td>"; echo $row['lastname']; echo "</td></tr>"; } echo "</table>"; ?> What it does is this, it gets the row from the database, echos it and I am able to edit it and then save. The thing is, it works with the updating but im trying to combine 2 variables into 1 and update that one also. Im trying to combine month and date textboxes into one variable in the database that is name. It for some reason updates the column name to A instead of what I had in the 2 fields month and date. Here is where I combine both fields into one. $name = $month."<br>".$date; And here is where $month and $date is set. $month = $_POST['month']; $date = $_POST['date']; Here is a picture. Link to comment https://forums.phpfreaks.com/topic/226018-mysql-update-and-fetch-not-working-as-planned/ Share on other sites More sharing options...
dragon_sa Posted January 29, 2011 Share Posted January 29, 2011 $name = $month[$i]."<br>".$date[$i]; $sql1="UPDATE $tbl_name SET name='$name', month='$month[$i]', date='$date[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'"; The value you are getting was part of the word ARRAY Link to comment https://forums.phpfreaks.com/topic/226018-mysql-update-and-fetch-not-working-as-planned/#findComment-1166819 Share on other sites More sharing options...
Russia Posted January 29, 2011 Author Share Posted January 29, 2011 So what part did you edit in those 2 lines? Link to comment https://forums.phpfreaks.com/topic/226018-mysql-update-and-fetch-not-working-as-planned/#findComment-1166820 Share on other sites More sharing options...
Russia Posted January 29, 2011 Author Share Posted January 29, 2011 Okay now it says 'Array<br>' in the database instead of JAN<br>01 Why's that? Link to comment https://forums.phpfreaks.com/topic/226018-mysql-update-and-fetch-not-working-as-planned/#findComment-1166821 Share on other sites More sharing options...
dragon_sa Posted January 29, 2011 Share Posted January 29, 2011 $name = $month."<br>".$date; became $name = $month[$i]."<br>".$date[$i]; and name='$name[$i]' became name='$name' Link to comment https://forums.phpfreaks.com/topic/226018-mysql-update-and-fetch-not-working-as-planned/#findComment-1166822 Share on other sites More sharing options...
Russia Posted January 29, 2011 Author Share Posted January 29, 2011 Oh great Thanks!! Also, what does the [$i] exactly for? Link to comment https://forums.phpfreaks.com/topic/226018-mysql-update-and-fetch-not-working-as-planned/#findComment-1166823 Share on other sites More sharing options...
dragon_sa Posted January 29, 2011 Share Posted January 29, 2011 In this case $i stands for the index value of the array you submitted from your forms so on the first loop $i=0 which is the first value in the array second loop $i=1 which is the second value in the array and so on until $i is greater than the number counted in the array Link to comment https://forums.phpfreaks.com/topic/226018-mysql-update-and-fetch-not-working-as-planned/#findComment-1166826 Share on other sites More sharing options...
Russia Posted January 29, 2011 Author Share Posted January 29, 2011 Okay thanks again alot! Link to comment https://forums.phpfreaks.com/topic/226018-mysql-update-and-fetch-not-working-as-planned/#findComment-1166828 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.