2011-09-20 29 views
0

所以我有一個看起來如此的表單。如何阻止PHP向MYSQL數據庫添加空白行/字段

<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我告訴他們先去填寫表格,不要把任何數據庫

+3

麪點,你需要開始嘗試將它們插入到數據庫之前,您消毒POST變量。 –

+0

詳細說明一下,你的數據庫受到了所謂的「SQL注入」的限制。由於構建查詢字符串的方式,有人可以很容易地擦除整個數據庫。相反,請嘗試使用準備好的聲明:http://www.php.net/manual/en/class.mysqli-stmt.php –

+0

謝謝JonStirling。但更重要的是@SkyKelsey我很欣賞這個解釋。我將搜索衛生Mysql/Php的最佳實踐並相應地修改我的代碼! – Anele

回答

1

這裏是接收POST數據後的草圖例如

你必須進行檢查,並在情況下提高錯誤標誌
的一些錯誤顯示回來的形式而不是保存它

<? 
if ($_SERVER['REQUEST_METHOD']=='POST') { 

    $err = array(); 
    //performing all validations and raising corresponding errors 
    if (empty($_POST['name']) $err[] = "Username field is required"; 
    if (empty($_POST['text']) $err[] = "Comments field is required"; 

    if (!$err) { 
    // if no errors - saving data 
    // and then redirect: 
    header("Location: ".$_SERVER['PHP_SELF']); 
    exit; 
    } else { 
    // all field values should be escaped according to HTML standard 
    foreach ($_POST as $key => $val) { 
     $form[$key] = htmlspecialchars($val); 
    } 
} else { 
    $form['name'] = $form['comments'] = ''; 
} 
include 'form.tpl.php'; 
?> 

並修改您的表單,使其能夠顯示錯誤

<? if ($err): ?> 
    <? foreach($err as $e): ?> 
<div class="err"><?=$e?></div> 
    <? endforeach ?> 
<? endif ?> 
<form> 
    <input type="text" name="name" value="<?=$form['name']?>"> 
    <textarea name="comments"><?=$form['comments']?></textarea> 
    <input type="submit"> 
</form> 
2

您需要檢查形式已提交。你可能想是這樣的:

<?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); 
} 

?> 
0

您可以檢查所有的

if(empty($_POST['company'])) 
{ 
//redirect or show error msg that its required 
} 
else 
{ 
//do what you want 
} 

,並相應地對所有。

如果您發佈了表單$ _POST將始終爲真,因此當您必須檢查空值時,您必須像這樣檢查所有數據。

if(empty($_POST['company']) || empty($_POST['email']) || empty($_POST['user'])) 

等等。

-1

只需檢查是否存在所有必需的表單域。如果它們不存在,請重定向到您的表單頁面。

if(!array_key_exists("company", $_POST){ 
    header('Location: http://your.form.page.html'); 
} 
+0

真的嗎?........ –

1

檢查公司名稱是否已在請求中提供且不爲空。如果沒有公司名稱,可能會出現錯誤消息,將用戶重定向到表單。

形式:

<form action="thanks.php" method="post" class="niceform" name="myform"> 
<?php if($_GET['error']): ?> 
<span class="error">Please fill the required fields!</span> 
<?php endif; ?> 
<table> 
<tr> 
<td><label for="company_name">Company Name</label></td> 
<td><input type="text" name="company" value="" size="38" /></td> 
</tr> 

Thanks.php:

<?php 
// Is company specified? 
if(!isset($_POST['company']) || $_POST['company'] == '') { 
    header('Location: form.php?error=1'); 
    exit(); 
} 

// 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); 
?> 
1

檢查是否有$ _ POST是不夠的。在帖子後重定向 - 否則如果添加一行並刷新頁面,則最終會在數據庫中創建2條記錄。所以發佈到process.php,並從process.php重定向到thanks.php。此外,在PHP中驗證,而不是在JavaScript中

0
if(count($_POST)>0){ 
    // your code... 
} 
else{ 
    // else code... 
} 
0

首先和最重要的是:不要在表格內使用表格。 (我很抱歉無法抗拒,但這傷害了我的眼睛)

關於主題:永遠記得在你的前端和後端進行驗證。

因此,在這種情況下,一些驗證添加到您的PHP文件,如:

<?php 

if(empty($_POST['company'])) 
{ 
// show validation message 
} else { 

// 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); 
} 
?> 
1

只需選中下面的代碼

<?php 
// open the connection 
$conn = mysql_connect("localhost", "root", "password"); 
// pick the database to use 
mysql_select_db("company_register",$conn); 

if($_POST[company]!="" or $_POST[email]!="" or $_POST[phone]!="" or $_POST[address]!="") { 
    $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()); 
} else { 
    echo "Please fill all fields "; 
} 

mysql_close($conn);