尽管通过表单发布了数据,但空行仍会插入到数据库中

时间:2014-04-07 22:29:49

标签: php mysql forms mysqli

我使用[post]方法创建了一个简单的HTML表单,并使用外部操作php页面。我面临的问题是,每次按下提交按钮时,都会在数据库中插入一个空行而不是输入数据。

这是我的索引页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Webediah Add new hosting customer</title>
<style type="text/css">
.auto-style1 {
    text-align: left;
}
.auto-style2 {
    text-align: center;
}
</style>
</head>

<body>
<form id="form1" name="form1" method="post" action="add.php">
  <p>&nbsp;</p>
  <h1 class="auto-style2"><strong>New Hosting Customer </strong></h1>
  <p>&nbsp;</p>
  <table width="43%" border="0" align="center">
    <tr>
      <td width="36%" scope="row" class="auto-style1">Customer name :</td>
      <td width="64%"><label for="cname"></label>
      <input type="text" name="cname" id="cname" style="width: 233px" /></td>
    </tr>
    <tr>
      <td scope="row" class="auto-style1">Customer Domain :</td>
      <td><label for="cdomain"></label>
      <input type="text" name="cdomain" id="cdomain" style="width: 234px" /></td>
    </tr>
    <tr>
      <td scope="row" class="auto-style1">Host start on :</td>
      <td><label for="hstart"></label>
      <input type="text" name="sdate" id="hstart" style="width: 236px" /></td>
    </tr>
    <tr>
      <td scope="row" class="auto-style1">Host ends on :</td>
      <td><label for="hends"></label>
      <input type="text" name="edate" id="hends" style="width: 236px" /></td>
    </tr>
    <tr>
      <td scope="row" class="auto-style1">Customer Email :</td>
      <td><label for="cemail"></label>
      <input type="text" name="cemail" id="cemail" style="width: 237px" /></td>
    </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th scope="row"><input type="submit" name="Submit" id="Submit" value="Submit" /></th>
      <td>&nbsp;</td>
    </tr>
  </table>
  <p>&nbsp;</p>
</form>


</body>
</html>

这是我的外部动作php页面:

<?php $con = mysqli_connect("localhost","root","111","awamah");


if (mysqli_connect_errno())
echo " error in mysql connection" . mysqli_connect_errno(); 


$sql = "INSERT INTO `customer` (`cname`,`cdomain`,`sdate`,`edate`,`cemail`) VALUES ('$_post[cname]','$_post[cdomain]','$_post[sdate]','$_post[edate]','$_post[cemail]')";

if(!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}
echo "1 record added";

?>

1 个答案:

答案 0 :(得分:2)

$_POSTsuperglobal,必须为大写。

您的VALUES查询中有$_post,请将它们全部更改为$_POST,这是主要解释。

这些也是超级全球:

  • $ GLOBALS
  • $ _ SERVER
  • $ _ GET
  • $ _ POST
  • $ _ FILES
  • $ _ COOKIE
  • $ _ SESSION
  • $ _ REQUEST
  • $ _ ENV

要了解有关superglobals的更多信息,请访问PHP.net网站:


警告

您目前的代码向SQL injection开放。使用prepared statementsPDO