Jump to content

Web form dropdown and mysql


skoobi

Recommended Posts

Hi im trying to create an interactive web form where it calls the dropdown list from a mysql database. There are 3 different dropdown lists which are picking up seperate data from within the database.

The problem i have is that ive managed to call the data from the database into the dropdown and i can select the items but i cant get it to send the data back to the database as an order.

Heres the code i have at the moment.

 

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Order Form</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
// CALL FOR NECESSARY FILES
require_once 'includes/connect.php';

//TITLE
echo '<p class="title">Order Form</p>';

// START FORM

echo "<form action=\"includes/process.php\" method=\"post\" name=\"order\" id=\"order\">";

// START OF STARTERS SECTION
echo "<p class=\"body\">Please choose a Starter:<br/>";

// CALL THE ORDER FOR REVIEW FROM DATABASE
mysql_select_db($db, $con);
$result_starter = mysql_query("SELECT id, starter FROM menu_starters");
$options=""; 
while($row = mysql_fetch_array($result_starter))
          {
		 $id=$row["id"];
  			 $main=$row["starter"];
			 $options.="<OPTION VALUE=\"$id\">".$main; 
          }
echo '<SELECT NAME=main><OPTION VALUE=0>Choose Order' . $options . '</SELECT>';
echo "<br/><a href=\"starters.php\">add/update</a></p>";
// END OF STARTERS

// START MAIN COURSE SECTION
echo "<p class=\"body\">Please choose a Main Course:</a><br/>";

// CALL THE MAIN COURSE ORDER FOR REVIEW FROM DATABASE
mysql_select_db($db, $con);
$result_main = mysql_query("SELECT id, main_course FROM menu_main_course");
$options=""; 
while($row = mysql_fetch_array($result_main))
          {
		 $id=$row["id"];
  			 $main=$row["main_course"];
			 $options.="<OPTION VALUE=\"$id\">".$main; 
          }
echo '<SELECT NAME=main><OPTION VALUE=0>Choose Order' . $options . '</SELECT>';
echo "<br/><a href=\"main_course.php\">add/update</a></p>";			 
// END OF MAIN COURSE



//  START DESERT SECTION
echo "<p class=\"body\">Please choose a Desert</a><br/>";

// CALL THE DESERT ORDER FOR REVIEW FROM DATABASE
mysql_select_db($db, $con);
$result_desert = mysql_query("SELECT id, deserts FROM menu_deserts");
$options=""; 
while($row = mysql_fetch_array($result_desert))
          {
		 $id=$row["id"];
  			 $main=$row["deserts"];
			 $options.="<OPTION VALUE=\"$id\">".$main;  
          }
echo '<SELECT NAME=main><OPTION VALUE=0>Choose Order' . $options . '</SELECT>';
echo "<br/><a href=\"deserts.php\">add/update</a></p>";
mysql_close($con);
// END OF DESERTS
echo '<input name="Submit" type="submit" class="submit" value="Send Order!"/></form>';
?>
</body>
</html>

 

and this is the process.php file:

<?php
require_once 'connect.php'; 

mysql_select_db($db, $con);

// INSERT: if we have a name to add...
$sql="INSERT INTO table_orders ('id', 'starter', 'starterQty', 'mainCourse', 'mainCourseQty', 'dersert', 'desertQty') 
VALUES ('','$_POST[starter]','$_POST[starterQty]','$_POST[mainCourse]','$_POST[mainCourseQty]','$_POST[dersert]','$_POST[dersertQty]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Order Succesfull! <br/><br/> <a href=\"../index.php\">Make Another Order</a>";

mysql_close($con);
?>

 

Any ideas... The error im getting is:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id', 'starter', 'starterQty', 'mainCourse', 'mainCourseQty', 'dersert', 'desert' at line 1

 

But im not sure why...

Any help or information would be greatful.

 

Cheers

~Chris

Link to comment
Share on other sites

dont put field names in quotes.

 

$sql="INSERT INTO table_orders (id, starter, starterQty, mainCourse, mainCourseQty, dersert, desertQty)

VALUES ('','$_POST[starter]','$_POST[starterQty]','$_POST[mainCourse]','$_POST[mainCourseQty]','$_POST[dersert]','$_POST[dersertQty]')";

Link to comment
Share on other sites

dont put field names in quotes.

 

$sql="INSERT INTO table_orders (id, starter, starterQty, mainCourse, mainCourseQty, dersert, desertQty)

VALUES ('','$_POST[starter]','$_POST[starterQty]','$_POST[mainCourse]','$_POST[mainCourseQty]','$_POST[dersert]','$_POST[dersertQty]')";

@jl5501

if he dont put the '' for the field names then how will he insert the text fields in the database. doesnot that give an error..

Link to comment
Share on other sites

the data goes in quotes, the field names must not.

 

You can use backticks  `fieldname` for a field name, but only needed if there is a space in the field name or the field name is a reserverd word, and generally it is better not to use backticks and not to use reserved words as field names or have spaces in them

Link to comment
Share on other sites

$_POST[starter] should be like $_POST['starter'] and others too similarly. you assign these POST values to variables and use that variables inside query. you will have to use single quotes but only for  those table fields which are non numaric.

 

yes i think this should resolve the issue

Link to comment
Share on other sites

the data goes in quotes, the field names must not.

 

You can use backticks  `fieldname` for a field name, but only needed if there is a space in the field name or the field name is a reserverd word, and generally it is better not to use backticks and not to use reserved words as field names or have spaces in them

 

so is it that if i write my code some thing like this will it work..

VALUES

('',$_POST['starter'],$_POST['starterQty'],$_POST['mainCourse'],$_POST['mainCourseQty'],$_POST['dersert'],$_POST['dersertQty'])";

Link to comment
Share on other sites

Wiked ive got one step further thank you for your help... Its posting the result to the database but im still stuck on how it sends the dropdown option.

i.e. one of the dropdown options is 'Soup of the day' but if i select that now and submit order it adds the order to the database but nothing is actually inserter into the starters table... If that makes sence.

 

Cheers

Thank you for your help so far...

Link to comment
Share on other sites

If you put print_r($_POST); at the top of your process script I'm sure you'll see the problem.

 

This is the code you posted for the "starters".....

 

 

echo '';

 

So it's no surprise to me that there is no $_POST['starter'].

Link to comment
Share on other sites

Well im getting a little further... Im getting this now.

 

Array ( [starter] => Soup of the Day [starterQty] => 3 [main_course] => No Order [mainCourseQty] => [deserts] => No Order [desertQty] => [submit] => Send Order! ) Order Succesfull! 

 

Here is the code for the starters option list:

 

//  START DESERT SECTION
echo "<p class=\"body\">Please choose a Desert</a><br/>";

// CALL THE DESERT ORDER FOR REVIEW FROM DATABASE
mysql_select_db($db, $con);
$result_desert = mysql_query("SELECT id, deserts FROM menu_deserts");
$options=""; 
while($row = mysql_fetch_array($result_desert))
          {
		 $id=$row["deserts"];
  			 $main=$row["deserts"];
			 $options.="<OPTION VALUE=\"$id\">".$main;  
          }
echo '<select name="deserts"><option value="No Order">Choose Order' . $options . '</SELECT>';
echo '<input name="desertQty" type="text" size="2" maxlength="2" />';
echo "<br/><a href=\"deserts.php\">add/update</a></p>";

mysql_close($con);
// END OF DESERTS

 

And here is the process form as it stands:

<?php
require_once 'connect.php'; 

mysql_select_db($db, $con);
print_r($_POST);
// INSERT: if we have a name to add...
$sql="
INSERT INTO table_orders (id, starter, starterQty, mainCourse, mainCourseQty, desert, desertQty) 
VALUES (
'', 
'$_POST[$starter]', 
'$_POST[$starterQty]',
'$_POST[$mainCourse]',
'$_POST[mainCourseQty]',
'$_POST[$dersert]',
'$_POST[dersertQty]'
)";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
echo "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "<span class=\"body\">Order Succesfull! <br/><br/> <a href=\"../index.php\">Make Another Order</a></span>";


?>

 

But for some reason it still is not entering the data into the database...

Any ideas im coming to my wits end... Its going to be a simple thing but i just cant see or find what is causing it ...

Link to comment
Share on other sites

Ok solved the problem... I went and had a coffee and came back a little more refreshed...

 

Was:

<?php
require_once 'connect.php'; 

mysql_select_db($db, $con);
print_r($_POST);
// INSERT: if we have a name to add...
$sql="
INSERT INTO table_orders (id, starter, starterQty, mainCourse, mainCourseQty, desert, desertQty) 
VALUES (
'', 
'$_POST[$starter]', 
'$_POST[$starterQty]',
'$_POST[$mainCourse]',
'$_POST[mainCourseQty]',
'$_POST[$dersert]',
'$_POST[dersertQty]'
)";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
echo "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "<span class=\"body\">Order Succesfull! <br/><br/> <a href=\"../index.php\">Make Another Order</a></span>";


?>

 

Now:

<link href="../style.css" rel="stylesheet" type="text/css" />
<?php
require_once 'connect.php'; 

mysql_select_db($db, $con);
print_r($_POST);
// INSERT: if we have a name to add...
$sql="
INSERT INTO table_orders (id, starter, starterQty, mainCourse, mainCourseQty, desert, desertQty) 
VALUES (
'', 
'$_POST[starter]', 
'$_POST[starterQty]',
'$_POST[mainCourse]',
'$_POST[mainCourseQty]',
'$_POST[dersert]',
'$_POST[dersertQty]'
)";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
echo "<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "<span class=\"body\">Order Succesfull! <br/><br/> <a href=\"../index.php\">Make Another Order</a></span>";


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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