我使用 [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> </p>
<h1 class="auto-style2"><strong>New Hosting Customer </strong></h1>
<p> </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"> </th>
<td> </td>
</tr>
<tr>
<th scope="row"><input type="submit" name="Submit" id="Submit" value="Submit" /></th>
<td> </td>
</tr>
</table>
<p> </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";
?>
最佳答案
$_POST
是 superglobal并且必须为大写。
您的 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/