Jump to content

[SOLVED] Sending data to my database


Schlo_50

Recommended Posts

I have ms access database (im know mysql is better but please bare with me) and within it  is a product list. That product list is called and displayed on my product page. After the price of an item has been echo'ed out i have a small text field where the user of the system is required to type the product id ($ProductId) on all the items they wish to select and then proceed to click submit.

 

Once the submit button has been pressed i want to send all of the data typed into the little text fields into a single cell separated by commas in the database. I have some basic code that doesn't correctly do what i want it to. If more than 3 products are listed on a page and i type the product id for any items not in an order E.g.

 

Product 1  <----i want this one

Product 2

Product 3

Product 4

Product 5 <-----I wantthis one

 

I just get ',,,' sent tothe database however if i type product id like this:

 

Product 1  <----i want this one

Product 2  <----I want this one as well

Product 3  <----i want this one too

Product 4

Product 5

 

I get 1,2,3 sent into the database.

 

here is my code:

 

<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>
<?php
if ($_POST[submit] == "Submit") 
{
//Collect form data

$codea = $_POST['code'][2];
$code = $_POST['code'][1];
$codeb = $_POST['code'][0];
$code = "$code,$codeb,$codea";

echo "$code";
//SQL Statement

$conn = odbc_connect('CentralProduct', '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);
}
?>

 

Any help would be appreciated, and if you don't understand what im trying to do please ask as im really stuck!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/79240-solved-sending-data-to-my-database/
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.