我正在尝试创建一个带有Web前端的IT资产数据库。
我已经通过POST从表单中收集了一些数据,以及一个已经写入cookie的变量。
这是我第一次尝试将数据输入到数据库中。
以下是代码:
<?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('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");
?>
我收到的错误信息是: 您的SQL语法存在错误;请检查与您的MySQL服务器版本相对应的手册,了解在第2行使用'','678','Server','789')的正确语法。
那些数据只是我试图插入的测试数据,但看起来问题出现在$value、$type和$notes这几个变量上。
如果有帮助,以下是表格创建语句:
<?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 />";
?>
在Ubuntu 10.04上运行标准的LAMP堆栈。
谢谢。
$_SESSION
代替setcookie()
,并转义您的输入数据,以防止未引用攻击。 - Delan Azabani