bhavin_85 Posted March 10, 2007 Share Posted March 10, 2007 hi guys im having a bit of trouble with my form, its for an invoice system I have 2 tables: Invoices - invoice_id*, cust_id and date invoice_items - invoice_item_id*, invoices_invoice_id(links to invoice_id in invoices table) item_item_id(links to item_id in item table) description, weight, price I am trying to create the form for the page so that all the information can be entered at once, but i dont think its possible, because an invoice must be created first then invoice_items added using that invoice id. how can i get my form to do this? i need it to insert the invoice into the database then take that invoice_id and add it to the item details on the page? this is what ive written so far but now im stuck on the invoice_id bit ??? <? session_start(); if ( empty($_SESSION['username'])){ header("location:default.php"); exit; } include('../../config.php'); $sql="SELECT * FROM item"; $query=mysql_query($sql) or die(mysql_error()); ?> <html> <head> <title>Administrator - Add Sales Invoice</title> </head> <body> <form name="add" id="add" method="post" action="add.php"> <table width="650" align="center"> <tr> <td> <? include('../menu.php'); ?></td></tr> <tr> <td align="center"><b> Add Sales Invoice</b></td> </tr> </table> <table align="center" cols="5"> <tr> <td colspan="5"><b>Invoice Details:</b></br></br> Customer ID: <input name="cust_id" id="cust_id" type="text"></input></br></br> Date: (YYYY-MM-DD)<input name="date" id="date" type="text"></input></br></br> </td> </tr> <tr> <td> Items:</td></tr> <tr> <td colspan="1">Invoice ID</td> <td colspan="1">Item</td> <td colspan="1">Description</td> <td colspan="1">Weight</td> <td colspan="1">Price</td> </tr> <tr> <td colspan="1"> <input id="invoice_id" name="invoice_id" type="text"></input></td> <td colspan="1"> <select type="text" name="item_id"> <? while ($row = mysql_fetch_array($query)) {?> <option value="<? printf($row["item_id"]); ?>"><? printf($row["item_name"]);?></option> <? } ?> </select> </td> <td colspan="1"> <input id="description" name="description" type="text"></input></td> <td colspan="1"> <input name="weight" id="weight" type="text"></input></td> <td colspan="1"> <input name="price" name="price" type="text"></input></td> </tr> <tr><td> <input id="submit" type="submit" value="Submit"></input> </td> </tr> <tr> <td> </td></tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/42108-multiple-forms-in-1-page/ Share on other sites More sharing options...
aniesh82 Posted March 10, 2007 Share Posted March 10, 2007 Hello I think ,invoice_id is an auto increment field. So you need not use a text field for getting the invoces id from user. You can execute the first query to insert cust_id and date . Using mysql_insert_id, you will get the inserted id.. Use this id in the second table for making a relationship between the table.. Otherwise if you want to see the invoices_id, call a function to retrieve the largest invoices_id from table before loading your form. Add one to this retrieved id. Displays that id in your text field. I think my first suggestion is good. Regards Aniesh Joseph Link to comment https://forums.phpfreaks.com/topic/42108-multiple-forms-in-1-page/#findComment-204230 Share on other sites More sharing options...
bhavin_85 Posted March 10, 2007 Author Share Posted March 10, 2007 i like the first idea, that can work the only problem i can think of though is there are multiple items to an invoice right, so i guess ill need to create some sort of button to add another item, but how can i get the form to remember what information was already in the form? Link to comment https://forums.phpfreaks.com/topic/42108-multiple-forms-in-1-page/#findComment-204239 Share on other sites More sharing options...
aniesh82 Posted March 10, 2007 Share Posted March 10, 2007 Hello So you need to add many records which has same invoice id ? If you want to do so, submit the form once then ... In the action page you please put a link to this form with the invoice id, ...You can repeat this any number of times. So second time you do this, have invoice id, so donot need to take the id from mysql_insert_id... Otherwise you can register it in session and after taht clear the session.. Third method is add a innerhtml to your main form. So you can add any number of items .. All this can do it in a single submit. Regards Aniesh Joseph Link to comment https://forums.phpfreaks.com/topic/42108-multiple-forms-in-1-page/#findComment-204260 Share on other sites More sharing options...
bhavin_85 Posted March 10, 2007 Author Share Posted March 10, 2007 Hello So you need to add many records which has same invoice id ? In the action page you please put a link to this form with the invoice id, ...You can repeat this any number of times. So second time you do this, have invoice id, so donot need to take the id from mysql_insert_id... Hi Yes theres multiple invoice_items associated with 1 invoice how do u make that link? sorry im not a great php coder ??? so far i have written this as the action page <? $cust_id=$_POST['cust_id']; $date=$_POST['date']; $item_item_id=$_POST['item_item_id']; $description=$_POST['description']; $weight=$_POST['weight']; $price=$_POST['price']; $points=($price*0.1); include('../../config.php'); echo "ID:$item_item_id"; $sql="INSERT INTO invoices (cust_id, date) VALUES ('$cust_id','$date')"; $result = mysql_query($sql) or die(mysql_error()); $lastid=mysql_insert_id(); $sql1="INSERT INTO invoice_items (invoices_invoice_id, item_item_id, description, weight, price, points) VALUES ('$lastid', '$item_item_id', '$description', '$weight', '$price', '$points')"; $result1 = mysql_query($sql1) or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/42108-multiple-forms-in-1-page/#findComment-204277 Share on other sites More sharing options...
aniesh82 Posted March 10, 2007 Share Posted March 10, 2007 Hello $lastid has the inserted id ie invoice id... After the end of your code add this : <a href="page_name.php?invoice_id=<?=$lastid?>">Add More Items</a> Please note above code is a html .. So will write outside of the php tags.. In your main page with forms.. Change the invoice text filed like this : <input id="invoice_id" name="invoice_id" type="text" value="<? echo $_GET['invoice_id']; ?>></input></td> This add more link will displays your form with the invoice id.. In add.php, make the following changes.... Write a function to check wether a record is already available with the invoice_id, If so we skip the code to insert the new record . $invoice_id = $_POST['invoice_id']; if(function_check($invoice_id)) { $lastid = $invoice_id ; } else { $sql="INSERT INTO invoices (cust_id, date) VALUES ('$cust_id','$date')"; $result = mysql_query($sql) or die(mysql_error()); $lastid=mysql_insert_id(); } Regards Aniesh Joseph Link to comment https://forums.phpfreaks.com/topic/42108-multiple-forms-in-1-page/#findComment-204280 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.