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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.