What is wrong with my SQL syntax here?
I'm trying to create a IT asset database with a web front end.
I've gathered some data from forms using POST as well as one variable that had already written to a cookie.
This is the first time I have tried to enter the data into the database.
Here is the code:
<?php
//get data
$id = $_POST['id'];
$company = $_POST['company'];
$location = $_POST['location'];
$purchase_date = $_POST['purchase_date'];
$purchase_order = $_POST['purchase_order'];
$value = $_POST['value'];
$type = $_COOKIE["type"];
$notes = $_POST['notes'];
$manufacturer = $_POST['manufacturer'];
$model = $_POST['model'];
$warranty = $_POST['warranty'];
//set cookies
setcookie('id', $id);
setcookie('company', $company);
setcookie('location', $location);
setcookie('purchase_date', $purchase_date);
setcookie('purchase_order', $purchase_order);
setcookie('value', $value);
setcookie('type', $type);
setcookie('notes', $notes);
setcookie(开发者_如何学Python'manufacturer', $manufacturer);
setcookie('model', $model);
setcookie('warranty', $warranty);
//checkdata
//start database interactions
// connect to mysql server and database "asset_db"
mysql_connect("localhost", "asset_db", "asset_db") or die(mysql_error());
mysql_select_db("asset_db") or die(mysql_error());
// Insert a row of information into the table "asset"
mysql_query("INSERT INTO asset
(id, company, location, purchase_date, purchase_order, value, type, notes) VALUES('$id', '$company', '$location', '$purchase_date', $purchase_order', '$value', '$type', '$notes') ")
or die(mysql_error());
echo "Asset Added";
// Insert a row of information into the table "server"
mysql_query("INSERT INTO server
(id, manufacturer, model, warranty) VALUES('$id', '$manufacturer', '$model', '$warranty') ")
or die(mysql_error());
echo "Server Added";
//destination url
//header("Location: verify_submit_server.php");
?>
The error I get is: 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 '', '678 ', 'Server', '789')' at line 2
That data is just test data I was trying to throw in there, but it looks to be the at the $value, $type, $notes.
Here are the table create statements if they help:
<?php
// connect to mysql server and database "asset_db"
mysql_connect("localhost", "asset_db", "asset_db") or die(mysql_error());
mysql_select_db("asset_db") or die(mysql_error());
// create asset table
mysql_query("CREATE TABLE asset(
id VARCHAR(50) PRIMARY KEY,
company VARCHAR(50),
location VARCHAR(50),
purchase_date VARCHAR(50),
purchase_order VARCHAR(50),
value VARCHAR(50),
type VARCHAR(50),
notes VARCHAR(200))")
or die(mysql_error());
echo "Asset Table Created.</br />";
// create software table
mysql_query("CREATE TABLE software(
id VARCHAR(50) PRIMARY KEY,
software VARCHAR(50),
license VARCHAR(50))")
or die(mysql_error());
echo "Software Table Created.</br />";
// create laptop table
mysql_query("CREATE TABLE laptop(
id VARCHAR(50) PRIMARY KEY,
manufacturer VARCHAR(50),
model VARCHAR(50),
serial_number VARCHAR(50),
esc VARCHAR(50),
user VARCHAR(50),
prev_user VARCHAR(50),
warranty VARCHAR(50))")
or die(mysql_error());
echo "Laptop Table Created.</br />";
// create desktop table
mysql_query("CREATE TABLE desktop(
id VARCHAR(50) PRIMARY KEY,
manufacturer VARCHAR(50),
model VARCHAR(50),
serial_number VARCHAR(50),
esc VARCHAR(50),
user VARCHAR(50),
prev_user VARCHAR(50),
warranty VARCHAR(50))")
or die(mysql_error());
echo "Desktop Table Created.</br />";
// create server table
mysql_query("CREATE TABLE server(
id VARCHAR(50) PRIMARY KEY,
manufacturer VARCHAR(50),
model VARCHAR(50),
warranty VARCHAR(50))")
or die(mysql_error());
echo "Server Table Created.</br />";
?>
Running a standard LAMP stack on Ubuntu 10.04.
Thank you.
you need a beginning quote ' before $purchase_order
You're missing a '
before $purchase_order.
What Galen said. Plus your code is vulnerable to SQL Injection - and it will generally have problems with any user-input data with embedded single-quotes.
you are missing a opening quote, below is the corrected section:
mysql_query("INSERT INTO asset
(id, company, location, purchase_date, purchase_order, value, type, notes) VALUES('$id', '$company', '$location', '$purchase_date', '$purchase_order', '$value', '$type', '$notes') ")
or die(mysql_error());
echo "Asset Added";
精彩评论