SQL使用PHP插入数据库

时间:2014-05-18 10:51:43

标签: php sql post sql-insert

嘿伙计们我第一次练习将数据插入我的数据库,而且我的代码出现了一些问题。

所以我的代码所做的是它从表单中获取信息并将其简单地插入到我连接的数据库中。我得到了两种类型的错误,一种是我的POSTS上未定义的索引,我也得到了SQL语法错误。我一直试着环顾四周,没有帮助我解决这个问题。

(是的,我在发布时忘记添加消息)编辑:

未定义的索引:第57行的customerid

查询问题您的SQL语法有错误; '','','','','','& #39)'在第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>

1 个答案:

答案 0 :(得分:1)

<?php 
if(isset($_POST['customerid'])){
   $customeridstr = $_POST['customerid']; 
   $customerfnamestr = $_POST['customerfname']; 
   $customerlnamestr = $_POST['customerlname']; 
   $customeraddressstr = $_POST['customeraddress'];  
   $suburbstr = $_POST['suburb'];  
   $statestr = $_POST['state']; 
   $postcodestr = $_POST['postcode'];  

   $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);  
   if($rs){
       echo 'Records Inserted';
   }else{
       die ('Problem with query' . mysql_error());
   }
}else{
?>    

<!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="" 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>
</body>
</html>
<?php
}
?>

现在,试试上面的解决方案,这应该可行。

为了进一步学习,请尝试以下方法:

  • 使用mysqli代替我的mysql库,现在已弃用。尝试清理数据(您从$ _POST或$ _GET获取的数据)。

  • 为数据库连接设置一个单独的文件,并将其包含在需要的位置,但在一个php脚本/页面中只包含一次。

  • 将值发布在同一页面上也不是一个好习惯,将其发布在其他只是处理提交数据的页面上。

  • 此外,我们只检查一个设置与否的变量,并且还没有验证它是否为空,因此请尝试检查要检查的每个必需变量和条件。

干杯:)