newsuper Posted August 7, 2008 Share Posted August 7, 2008 Hi all, I trying to update multiple rows in the same table with this code, but it doesn't seem to work. Nothing seems to happen when I clcik the 'Update' button. Any help is appreciated. Thanks <? /// Check if submit button is clicked if (isset($_POST['update'])) { ?><font color="black"><? echo("Status has been updated"); $con = mysql_connect("localhost","user","pass"); mysql_select_db("portfolio"); $num_rows = count($_POST['id']); // start a loop in order to update each record if(isset($_POST['update'])){ for($i=0;$i<$num_rows;$i++){ $id = $_POST['id']; $action = $_POST['action']; $sql = "UPDATE portfolio.stocks SET action = '".$action."' WHERE stocks.id = '".$id."'"; $result = mysql_query($sql); } } } /// Display the table $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("portfolio"); $sql_events = mysql_query("SELECT * FROM abndata inner join stocks on abndata.ABcode = stocks.Code WHERE abndata.ABCode = stocks.Code ORDER BY ABCode ") or die (mysql_error()); ?> <form method="post" action="index2.php"> <input type="submit" value="Update Status" name="update"/> <? while ($row = mysql_fetch_array($sql_events)) { ?> <tr bgcolor=#C0C0C0><SPACER height="1" type="block"> <td><b><? echo $row['ABCode']; ?></b></td> <td><b><? echo $row['ABName']; ?></b></td> <td><b><? echo number_format(round($row['PE FY08'],2),2); ?></b></td> <td><b><? echo number_format(round($row['FY08 Div Yield']*100,2),2); ?>%</b></td> <td><b><? echo number_format(round($row['ROE 08']*100,2),2); ?>%</b></td> <td><b><? echo number_format(round($row['Gearing 08']*100,2),2); ?>%</b></td> <td><b><? echo number_format($row['Market Cap']/1000,2); ?></b></td> <td><b><? echo $row['GICS Sector']; ?></b></td> <td><b> <? echo "<select name=action[]>"; echo "<option value=".$row['action'].">".$row['action']."</option><br>"; echo "<option value=Buy>Buy</option>"; echo "<option value=Hold>Hold</option>"; echo"</select>"; echo "<input type=hidden name=id[] value=".$row['id']." /><br>"; </td></tr> } echo "</form>"; ?> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/118568-update-multiple-rows-with-one-form/ Share on other sites More sharing options...
newsuper Posted August 7, 2008 Author Share Posted August 7, 2008 I see I have two if (isset($_POST['update'])) so I removed the second one, where it is meant to determine how many rows there are being submitted and loop through them. But still no luck updating the table. Link to comment https://forums.phpfreaks.com/topic/118568-update-multiple-rows-with-one-form/#findComment-610447 Share on other sites More sharing options...
fenway Posted August 7, 2008 Share Posted August 7, 2008 I'm not sure what you mean... Are you sure the queries executing without error? Where any rows affected? Link to comment https://forums.phpfreaks.com/topic/118568-update-multiple-rows-with-one-form/#findComment-610746 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.