Schlo_50 Posted November 13, 2007 Share Posted November 13, 2007 Hello guys, I have posted a question similar to this before but it had no response so i will try to explain a little better. I have a MS Access database which holds my products. On my products.php page i use PHP to access the products and output them in a list on the page. As well as displaying the results i echo out a text field after each record displayed called 'code', which is for the user to enter the product code of the item they wish to buy into. Because text field will appear after every record in the database i have called it 'code' in order for me to then have some php code submit all of the fields which the user has filled out and place them in a single field within my database and have each order nuber separated by a comma. E.g (page view) Item# 12 [text field]<------User types 12 if they want this item Item# 17 [text field]<------User types 17 if they want this item Item# 46 [text field]<------User types 46 if they want this item [submit button] E.g (database view once the form is submitted) |ProductNotes| |12,17,46 | The code i am using so far in my attempt to add all user information into my database is as follows: <?php if ($_POST[submit] == "Submit") { //Collect form data $code = $_POST['code'][0]; $codeb = $_POST['code'][0]; $code = "$code,$codeb"; //SQL Statement $conn = odbc_connect('DB', 'root', '') or die('Could not Connect to ODBC Database!'); $sql = "INSERT INTO Order1 " . "(ProductNotes) VALUES ('$code')"; //Execute SQL Statement and store results as a recordset $rs = @odbc_exec($conn,$sql); if (!$rs) { echo "An error has occured. Please try again"; } else { echo "The record was successfully inserted."; } odbc_close($conn); } ?> Before you ask, there are no php generated errors. This is a question soley for someone really godlike to tell me how to collect all the users data and place it in my database as i would like. At the moment i can collect the information but doesn't display in my db properly. I get '1,3' instead of 12,13,17 for example. Thanks so much Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 13, 2007 Share Posted November 13, 2007 can you give code of your form too Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 13, 2007 Author Share Posted November 13, 2007 <form name="the_form" id="the_form" method="post" action="<?php $_SERVER[php_SELF]; ?>"> <?php $sql2 = odbc_exec($odbc, "SELECT * FROM Product1 ORDER BY CategoryName") or die (odbc_errormsg()); $prevCat=''; while($row = odbc_fetch_array($sql2)) { $Category = $row["CategoryName"]; $ProductId = $row["ProductId"]; $ProductName = $row["ProductName"]; $Price = $row["Price"]; // has category changed // if so, print it if ($Category != $prevCat) { echo "<h2>$Category</h2>"; } echo 'Item Code: ', $ProductId, '<br/>Item Name: ', $ProductName, '<br/><br/>Price: £', $Price, '<input name="code[]" type="text" size="2" /><br/><br/>'; $prevCat = $Category; } ?> <hr /> (Order Notes- If you have any comments to make about any of the products you are ordering please state them below.)<br /> <textarea name="ProductNotes" cols="50" rows="5"></textarea><br /><br /> <input name="submit" type="Submit" value="Submit" /> </p> </form> Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 13, 2007 Author Share Posted November 13, 2007 bump Quote Link to comment Share on other sites More sharing options...
gin Posted November 14, 2007 Share Posted November 14, 2007 First you should check if the form is correcting info correctly. Do an echo $code before writing to your db, see if it shows what you expect. If not, start doing an echo every step back to see what went wrong. 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.