【问题标题】:SQL insert into database using PHPSQL使用PHP插入数据库
【发布时间】: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


【解决方案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 脚本/页面中包含一次。

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

  • 此外,我们只检查一个变量是否已设置,也不能验证它是否为空,因此请尝试检查您要检查的每个必需变量和条件。

干杯:)

【讨论】:

  • 发布的代码易受 sql 注入攻击。也不保证设置了任何(明确检查的除外)的帖子字段。
  • 我读过它,但我怀疑大多数人会读得比解决问题所需的更远。但是,这不会以任何方式降低您的答案
  • @Rangad: 取决于教学风格.. 我不想让它变得复杂到初学者无法从中学习:)
  • 我在很多情况下完全支持这一点。然而,SQL 注入与安全相关,所以我会在这里有所作为。但我想没有什么好争论的,因为我们只是权重不同
猜你喜欢
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 2017-04-08
  • 2020-05-22
  • 1970-01-01
相关资源
最近更新 更多