hance2105 Posted June 30, 2013 Share Posted June 30, 2013 hello guys, i have the below code which echoes records from tblproduct. a field is displayed with an add button against each retrieved record. how can i have only one add button to add all records at once to the table? <?php session_start(); include('db_connect.php'); $sql = mysql_query("select * from tblproduct"); $num = mysql_num_rows($sql); if($num>0){ echo "<center><table bgcolor='grey' width='80%' border=0>"; echo "<tr bgcolor='#CCCCCC'>";echo "<td><b><center>Product</td>";echo "<td><b><center>Name</td>";echo "<td><b><center>Brand</td>";echo "<td><b><center>Price</td>";echo "</tr>"; while($row = mysql_fetch_assoc($sql)){ extract($row); echo "<tr>"; echo '<td style="text-align:center;"><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'"/></td>'; echo "<td style='text-align: center;'>".$row['prod_name']."</td>"; echo "<td style='text-align: center;'>".$row['prod_brand']."</td>"; echo "<td>"."<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">". "<input type=\"hidden\" name=\"prod_id\" value=".$row['prod_id']." />". "<input type=\"text\" name=\"prod_price\" />". "<input type=\"submit\" value=\"Add\" /></form>"."</td>"; }echo "</table>";echo "<center><a href ='retailer_home.php' >Home</a>";} else{ echo "No products found.";} ?> Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted July 1, 2013 Share Posted July 1, 2013 You are making this much more difficult than needs to be... But you cant if you are going to have a form on each page. A way I would do it is after you enter in a price have javascript update that one . But everyone has their own ways of doing things. From the html5 working draft: 4.10.3 The form element Content model: Flow content, but with no form element descendants. Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 1, 2013 Share Posted July 1, 2013 (edited) First you would build your form different, using arrays: <?php session_start(); include('db_connect.php'); $sql = mysql_query("select * from tblproduct"); $num = mysql_num_rows($sql); if($num>0) { echo "<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">" ."<center><table bgcolor='grey' width='80%' border=0>" . "<tr bgcolor='#CCCCCC'>" . "<td><b><center>Product</td>" . "<td><b><center>Name</td>" . "<td><b><center>Brand</td>" . "<td><b><center>Price</td>" . "</tr>"; while($row = mysql_fetch_assoc($sql)){ echo "<tr>" .'<td style="text-align:center;"><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'"/></td>' . "<td style='text-align: center;'>".$row['prod_name']."</td>" . "<td style='text-align: center;'>".$row['prod_brand']."</td>" . "<td>" ."<input type=\"hidden\" name=\"prod_id[]\" value=".$row['prod_id']." />" ."<input type=\"text\" name=\"prod_price[]\" />" ."</td>"; } echo "</table>"."<input type=\"submit\" value=\"Add\" /></form>" ."<center><a href ='retailer_home.php' >Home</a>"; } else{ echo "No products found."; } ?> Then on: ret_addprod.php You would have a processing portion like: <?php include('db_connect.php'); if($_SERVER['REQUEST_METHOD'] == 'POST') { foreach($_POST['prod_id'] as $index => $id) { $sql = sprintf("UPDATE tblproduct SET prod_price = %f WHERE prod_id = %d", $_POST['prod_price'][$index], $id); if(mysql_query($sql)) continue; exit('There was an error in your syntax<br />' . mysql_error()); } } ?> I don't really like running queries in loops. The only way to make it work without looping the queries, would require a huge case statement. If there is a problem with overhead (shouldn't be, unless your server is remote from the db server) perhaps we could write that out. Edit: I'm an idiot, mixed the syntax for sprintf and mysqli->prepare Edited July 1, 2013 by jcbones Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.