2011-05-19 75 views
0

我有一個表單將提交將數據插入數據庫(公司詳細信息)的信息。提交工作正常,但我也發現進入包含表單的頁面會在數據庫中插入一個空行。任何想法,爲什麼這可能是?代碼如下:在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')"); 

?> 

回答

1

所有的回聲後添加此。

+0

這是一個好辦法做到這一點。 'if($ _SERVER ['REQUEST_METHOD'] =='POST')'保證工作。如果提交內容合法爲空,則您的失敗。 – 2011-05-19 20:03:37

+1

@MarcB,是的,但是OP不希望插入一個空的帖子,那就是問題所在! – Neal 2011-05-19 20:04:30

+0

實際上,他正在GET上插入,這是整個問題。簡單地說「不要空POST」不會停止空白字段。他將不得不添加更多的服務器端驗證。 – 2011-05-19 20:06:43

0

發生這種情況是因爲您正在回顯表單,然後立即將其插入到您的數據庫中,但尚不存在。

你可以試試這個:

if(count($_POST) > 0 && in_array('EstablishmentNameAdd',$_POST)) { 
$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')"); 
} 
+0

:P對不起,這是一個錯字..我拉了!謝謝 – 2011-05-19 20:06:29

+0

你爲什麼選擇'EstablishmentNameAdd'作爲該帖子的警告? – Neal 2011-05-19 20:07:00

+0

只是一個例子..我選擇了其他人,因爲它似乎最有可能是必要的。例如,你可以有一個企業,但沒有一個網站。或者,也許你正在讓你的機構成立..所以你還沒有電話號碼。但是,所有的機構都會有一個名稱。在任何時候,與該機構有關的每個人都會知道該名稱。 – 2011-05-19 20:11:11