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

标签 php mysql forms mysqli

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

这是我的索引页:

<!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";

?>

最佳答案

$_POSTsuperglobal并且必须为大写。

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

这些也是超全局变量:

  • $GLOBALS
  • $_SERVER
  • $_GET
  • $_POST
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_REQUEST
  • $_ENV

要了解有关超全局变量的更多信息,请访问 PHP.net 网站:

<小时/>

警告

您当前的代码已开放给 SQL injection 。使用prepared statements ,或PDO

关于php - 尽管通过表单发布了数据,但空行仍被插入到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22924279/

相关文章:

mysql - 将删除的属性返回到实时站点

mysql - 来自连接的 MySQL 表的最近的不同记录

php - 如果表单未在我自己的网站上提交,如何显示错误?

javascript - 如果变量不为空则显示内容

javascript - 将滚动条放到 div 上,并将获取的数据放入其中

php - Mysql在特定日期之间计数和分组

php - 所有城市的名称

mySQL 未按预期运行

javascript - 单击以表现得像返回一样

javascript - 在以多阶段形式进入下一步之前,我如何验证 field-yield 是否已填充?