Jump to content

Form Processing


Schlo_50

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/77185-form-processing/
Share on other sites

<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>

Link to comment
https://forums.phpfreaks.com/topic/77185-form-processing/#findComment-390803
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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