0

Below is the code I have. I'm trying to insert the variables into the table orders in the database called coffee. However when I submit the page the variables don't appear in the database.

$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$qty = $_POST['qty'];
$qty_T = $_POST['qty2'];
$option = $_POST['coffee'];
$option_T = $_POST['coffee2'];  
$query = "INSERT INTO orders(Name, Address, City, State, Zip, Quantity, Beans) VALUES ('$name', '$address', '$city', '$state', '$zip', '$qty', '$option')";
mysql_query($query);
mysql_close();

Thanks for your help

Ibu
  • 42,752
  • 13
  • 76
  • 103
  • 5
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – John Conde Apr 26 '13 at 00:22
  • 3
    FYI, you also wide open to [SQL injections](http://stackoverflow.com/q/60174) – John Conde Apr 26 '13 at 00:23
  • 1
    You need to use `mysql_error()` to see if any errors occurred. – John Conde Apr 26 '13 at 00:23
  • I'm not to worried about SQL injections. This is just a simple little project for a class. But okay I will try mysql_error() – Chris Yak Apr 26 '13 at 00:25
  • please post also your mysql connect statement – Saturnix Apr 26 '13 at 00:26
  • $link = mysql_connect('localhost', 'user', 'pass'); mysql_select_db('coffee', $link); – Chris Yak Apr 26 '13 at 00:29
  • I'm sorry its the connect is actually – Chris Yak Apr 26 '13 at 00:31
  • $link = mysql_connect('localhost', 'root', ''); – Chris Yak Apr 26 '13 at 00:31

2 Answers2

1

try using die(mysql_error()) or try to echo your variables to make sure it had a value in it. Also, try learning PDO much better with CodeIgniter. :)

Þaw
  • 2,047
  • 4
  • 22
  • 39
  • $link = mysql_connect('localhost', 'root', ''); mysql_select_db('coffee', $link); $query = "INSERT INTO orders(Name, Address, City, State, Zip, Quantity, Beans) VALUES ('$name', '$address', '$city', '$state', '$zip', '$qty', '$option')"; mysql_query($query); die(mysql_error()) – Chris Yak Apr 26 '13 at 00:36
  • like this `mysql_query($query) or die(mysql_error())` . have you `echo`ed your variables? did it have values? – Þaw Apr 26 '13 at 00:44
0

Are all your values strings? if zip is just an integer remove the quotes ('').

By the way, you dont need to parse all your POST-paramenters by hand, you can simply call extract($_POST); and all your POST parameters are available as variables.

try this:

extract($_POST);
mysql_query("INSERT INTO orders(Name, Address, City, State, Zip, Quantity, Beans) VALUES ('$name', '$address', '$city', '$state', '$zip', '$qty', '$option')") or die(mysql_error());

mysql_error() should output an error description for failed queries.

Marcel Dieterle
  • 335
  • 2
  • 6