Hi. I have a table listing products' data that is retrieved from the database. Each row corresponds to a product, and at the end of each row, there is a "buy" hyperlink. Each row is generated through a "for" loop like this: Code: $connection=mysql_connect('localhost','root','); if(!$connection){die(' ' . mysql_error());} $bd=mysql_select_db('PAT',$connection); if(!$bd){ die(' ' . mysql_error()); } $query=mysql_query("SELECT * FROM products",$connection); $regists=mysql_num_rows($query); for($i=1;$i<=$regists;$i++){ $data = mysql_fetch_assoc($query); $query_image=mysql_query("SELECT * FROM images",$connection); $image = mysql_fetch_assoc($query_image); if($data['reserved']!=1){ echo "<tr align=center> <td>".$data['name']."</td>"; echo "<td>".$data['label']."</td>"; echo "<td>".$data['price']." €</td>"; echo "<td>".$data['stock_quantity']."</td>"; echo "<td><img scr='".$image['path']."'></td>"; echo "<td align=right><a href='product_reservation.php' name='link'>Buy</a></td>"; echo "</tr>";} PHP: What I wanted it to do when the user clicked on the buy hyperlink was to generate a prompt so that the user could input the desired quantity of the product in the same row as the hyperlink. Then, the product_id, the quantity, the price*quantity and the user_id would all be stored in a relational table called "Orders" with foreign keys from the products' table and the users' table. All of that would be in the file "product_reservation.php" that is referenced by the hyperlink. I thought of something like setting the name of each hyperlink to the current value of the $i variable (see in the for statement) and then relate it to the number of the product in its table, as this is listed in order. Such as this: 1st product in table - "Garments" -> $i=1 hyperlink name="1" And then relate them in some way. Is something like this possible? Please, please give suggestions and help.
Create a form around the table Then the link will actually submit the form. Each product is actually part of an array so you might have <input type='text' name='qty[123]' value='0'> Code (markup): and that would tell me the client wants 0 of product #123 however if they update that to 3 that will be the value in the $qty array for that product
Thanks for the reply. But what code should I input specifically to act after the form is submitted? I've tried something like this to associate the product_id with the respective submit button: <form action="product_reservation.php" method="post"> <table align="center" width="800"> <tr border="2" bgcolor="grey"> <th>Name</th> <th>Label</th> <th>Price</th> <th>Quantity</th> <th>Quantity to buy</th> <th>Image</th> </tr>$query=mysql_query("SELECT * FROM products",$connection); $regists=mysql_num_rows($query); for($i=0;$i<$regists;$i++){ $data = mysql_fetch_assoc($query); $query_image=mysql_query("SELECT * FROM images WHERE product=".$data['id_product]."",$connection); $image = mysql_fetch_assoc($query_image); echo "<tr align=center> <td>".$data['name']."</td>"; echo "<td>".$data['label']."</td>"; echo "<td>".$data['price']." €</td>"; echo "<td>".$data['quantity_stock']."</td>"; echo "<td background ='".$imagem['path']."'></td>"; echo "<td><input type='text' size='1' name='text_".$data['id_product']."'></td>"; echo "<td align=right><input type='submit' name='button_".$data['id_product']."' value='Make reservation'> echo "</tr>";} PHP: What i did basically was to associate the button name with the number of the row the product is in. I was aiming for something like this: id_product - 2 submit name - button_2 Is this correct? If it is, what can I do from here? I'm kind of a newbie to this, so could you help with simple, specific code? Thanks
First I need to know following to help you quickly: 1. What exactly you need in script when it is submitted ? I mean what pieces of information ? regards
I need to save the product_id, the quantity that was typed by the user in the text box and the price*quantity into the database. Also, I need to substract the quantity that was typed in the quantity_available and sum it in the quantity_reserved fields in the product's table in database. Please say if you need more info to help, and thanks for the reply.
sorry I misunderstood, is each product has separate form, and when button in front of product is hit, it submits only one product ? if yes it is too easy. Tell me so I provide you code. regards
Well yes to everything, expect for one thing - The whole table is part of one form. The output will be different depending on what button is pressed, and that's where my difficulty lays. But do you think I should generate a form for each row?
Both are possible. Which one you like? tell me quick I need to hit bed soon, believe me am here to reply you becuase I comitted . regards
Tell me the one that i stated first - one form for the whole table. Anyway I take it you're from Europe if you're going to bed now? Are you here tomorrow morning? I need to hand this project over tomorrow and i just could really use some help. Thanks a lot Vooler.
Try this <tr align=center> <td>$data[name]</td> <td>$data[label]</td> <td>$data[price] €</td> <td>$data[quantity_stock]</td> <td> <form> <input type='text' size='1' name='product_qty'> <input type=hidden name=product_id value='$data[id_product]'> <input type=hidden name=product_price value='$data[price]'> <input type='submit' value='Make reservation'> </form> </td> <td background ='$imagem[path]'></td> </tr> HTML: What you receive is the fields for each form: You might have to rearrange the <th> tags. I hope it helps regards
I wish you best of luck boy. BTW, am somewhere in asia , I work in night, that is why I need to head to bed. regards
Ok Vooler, stay nice. I'll try to reach you if i need more help, i ask you to reply when available. But thanks a lot really, it is really important.