【发布时间】:2014-05-18 10:51:42
【问题描述】:
大家好,我只是第一次练习将数据插入到我的数据库中,我的代码有一些问题。
所以我的代码所做的是它从表单中获取信息并将其简单地插入到我连接的数据库中。我收到 2 种类型的错误,一种是我的 POSTS 中未定义的索引,我也收到了 SQL 语法错误。我试过环顾四周,但没有运气帮助我解决问题。
(是的,我发帖时忘记添加消息了)编辑:
未定义索引:第 57 行的 customerid
查询问题您的 SQL 语法有错误; '', '', '', '', '', '')' 在第 2 行
任何建议都会有所帮助:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Form</title>
</head>
<body>
<h1>Customer Information Collection <br /></h1>
<form method="post" action="task12.php" id="custinfo" >
<table>
<tr>
<td><label for="customerid">Customer ID (integer value): </label></td>
<td><input type="text" id="customerid" name="customerid" size=11/></td>
</tr>
<tr>
<td><label for="customerfname">Customer First Name: </label></td>
<td><input type="text" id="customerfname" name="customerfname" size=50/></td>
</tr>
<tr>
<td><label for="customerlname">Customer Last Name: </label></td>
<td><input type="text" id="customerlname" name="customerlname" size=50/></td>
</tr>
<tr>
<td><label for="customeraddress">Customer Address: </label></td>
<td><input type="text" id="customeraddress" name="customeraddress" size=65/></td>
</tr>
<tr>
<td><label for="suburb"> Suburb: </label></td>
<td><input type="text" id="suburb" name="suburb"/></td>
</tr>
<tr>
<td> State: </td>
<td> <select name="state" id="state">
<option value="--">--</option>
<option value="ACT">ACT</option>
<option value="NSW">NSW</option>
<option value="NT">NT</option>
<option value="QLD">QLD</option>
<option value="SA">SA</option>
<option value="TAS">TAS</option>
<option value="VIC">VIC</option>
<option value="WA">WA</option>
</select>
</td>
</tr>
<tr>
<td><label for="postcode"> Post Code (default "2000"): </label></td>
<td><input type="text" id="postcode" name="postcode" value="2000" size=4/></td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
<td> <input type="reset" value="Clear" /> </td>
</tr>
</table>
</form>
<?php
$customeridstr = $_POST['customerid'];
$customerfnamestr = $_POST['customerfname'];
$customerlnamestr = $_POST['customerlname'];
$customeraddressstr = $_POST['customeraddress'];
$suburbstr = $_POST['suburb'];
$statestr = $_POST['state'];
$postcodestr = $_POST['postcode'];
?>
<?php
$conn = mysql_connect("xxxxx", "xxxxx", "xxxxxx");
mysql_select_db("xxxxxxx", $conn)
or die ('Database not found ' . mysql_error() );
$sql = "INSERT INTO Customer (customerID, firstName, lastName, Address, suburb, state, postcode)
VALUES ( '$customeridstr', '$customerfnamestr’, '$customerlnamestr', '$customeraddressstr', '$suburbstr', '$statestr', '$postcodestr');";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
</body>
</html>
【问题讨论】:
-
你能发布 SQL 错误吗?
-
添加错误。请不要让我们逐行阅读您的代码以查找未定义的索引...了解错误消息的含义。他们在那里是有原因的。
-
抱歉,你发的时候我正抓着它们,因为我发的时候忘记加了
标签: php sql post sql-insert