Jump to content

Update multiple rows with one form


newsuper

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.