所以我有一个看起来像这样的表格。
<form action="thanks.php" method="post" class="niceform" name="myform">
<table>
<tr>
<td><label for="company_name">Company Name</label></td>
<td><input type="text" name="company" value="" size="38" /></td>
</tr>
// and so on
我通过 javascript 进行了验证。但问题是,当我从 localhost/mysite/form/thanks.php 直接转到 thanks.php 时,我在查看 phpmyadmin 时插入了一个空行。 thanks.php 看起来像这样。
<?php
// open the connection
$conn = mysql_connect("localhost", "root", "password");
// pick the database to use
mysql_select_db("company_register",$conn);
$sql = "INSERT INTO `tblsignups` VALUES ('NULL', '$_POST[company]', '$_POST[email]', '$_POST[phone]', '$_POST[address]', '$_POST[comments]', '$_POST[contact_person]')";
$result = mysql_query($sql, $conn) or die(mysql_error());
mysql_close($conn);
?>
// And I have a thank you msg I display
我如何检查是否有人应该直接去 thanks.php 我告诉他们先去填写表格并且不要在数据库中放任何东西
最佳答案
您需要检查表单是否已提交。你可能想要这样的东西:
<?php
if ($_POST)
{
// open the connection
$conn = mysql_connect("localhost", "root", "password");
// pick the database to use
mysql_select_db("company_register",$conn);
$sql = "INSERT INTO `tblsignups` VALUES ('NULL', '$_POST[company]', '$_POST[email]', '$_POST[phone]', '$_POST[address]', '$_POST[comments]', '$_POST[contact_person]')";
$result = mysql_query($sql, $conn) or die(mysql_error());
mysql_close($conn);
}
?>
关于php - 如何阻止 PHP 向 MYSQL 数据库添加空白行/字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7482566/