我有一個表單將提交將數據插入數據庫(公司詳細信息)的信息。提交工作正常,但我也發現進入包含表單的頁面會在數據庫中插入一個空行。任何想法,爲什麼這可能是?代碼如下:在PHP - 數據庫在載入頁面時產生空白行
if(!empty($_POST)){
...
}
包裝所有的底碼,因此這種方式只有在有後它會做插件:
<?php
echo "<form action='addnew.php' method='post'>";
echo "<input type='text' name='categoryAdd' value='Travel'/><br/>";
echo "<input type='text' name='EstablishmentNameAdd' value='EstablishmentName'/><br/>";
echo "<input type='text' name='Address1Add' value='Address1'/><br/>";
echo "<input type='text' name='Address2Add' value='Address2'/><br/>";
echo "<input type='text' name='Address3Add' value='Address3'/><br/>";
echo "<input type='text' name='Address4Add' value='Address4'/><br/>";
echo "<input type='text' name='PostcodeAdd' value='Postcode'/><br/>";
echo "<input type='text' name='NearestStationAdd' value='NearestStation'/><br/>";
echo "<input type='text' name='TelAdd' value='Tel'/><br/>";
echo "<input type='text' name='FaxAdd' value='Fax'/><br/>";
echo "<input type='text' name='EmailAdd' value='Email'/><br/>";
echo "<input type='text' name='WebsiteAdd' value='Website'/><br/>";
echo "<input type='text' name='DescriptionAdd' value='Description'/><br/>";
echo "<input type='submit' value='test'/>";
echo "</form>";
$EstablishmentNameAdd = mysql_real_escape_string($_POST['EstablishmentNameAdd']);
$CategoryAdd = $_POST['categoryAdd'];
$Address1Add = mysql_real_escape_string($_POST['Address1Add']);
$Address2Add = mysql_real_escape_string($_POST['Address2Add']);
$Address3Add = mysql_real_escape_string($_POST['Address3Add']);
$Address4Add = mysql_real_escape_string($_POST['Address4Add']);
$PostcodeAdd = $_POST['PostcodeAdd'];
$NearestStationAdd = mysql_real_escape_string($_POST['NearestStationAdd']);
$TelAdd = $_POST['TelAdd'];
$FaxAdd = $_POST['FaxAdd'];
$EmailAdd = mysql_real_escape_string($_POST['EmailAdd']);
$WebsiteAdd = mysql_real_escape_string($_POST['WebsiteAdd']);
$DescriptionAdd = mysql_real_escape_string($_POST['DescriptionAdd']);
$result1 = mysql_query("INSERT into establishment_id (EstablishmentName) values ('$EstablishmentNameAdd')");
$result2 = mysql_query("SELECT * from establishment_id where EstablishmentName = '$EstablishmentNameAdd'");
while($row = mysql_fetch_array($result2))
{
$ID = $row['EstablishmentID'];
$NAME = $row['EstablishmentName'];
};
$result3 = mysql_query("INSERT into establishmentdetails (EstablishmentID, EstablishmentName, category, Address1, Address2, Address3, Address4, Postcode, NearestStation, Tel, Fax,
Email, Website, Description) values('$ID', '$EstablishmentNameAdd', '$CategoryAdd', '$Address1Add', '$Address2Add', '$Address3Add', '$Address4Add', '$PostcodeAdd', '$NearestStationAdd', '$TelAdd', '$FaxAdd', '$EmailAdd',
'$WebsiteAdd', '$DescriptionAdd')");
?>
這是一個好辦法做到這一點。 'if($ _SERVER ['REQUEST_METHOD'] =='POST')'保證工作。如果提交內容合法爲空,則您的失敗。 – 2011-05-19 20:03:37
@MarcB,是的,但是OP不希望插入一個空的帖子,那就是問題所在! – Neal 2011-05-19 20:04:30
實際上,他正在GET上插入,這是整個問題。簡單地說「不要空POST」不會停止空白字段。他將不得不添加更多的服務器端驗證。 – 2011-05-19 20:06:43