2014-01-13 63 views
0

我對代碼保存3文本框在一個領域DATABSE空場到mysql使用PHP

當我進入3文本框,但是當我填寫1個文本框,然後按OK 保存在數據庫中的另一個文本框爲沒有問題空白

我只想把文本框被溢滿,並忽略文本框空

這是我的代碼

<?php 

include("connect.php"); 


$expert_name = trim($_POST['expert_name']); 
$expert_name2 = trim($_POST['expert_name2']); 
$expert_name3 = trim($_POST['expert_name3']); 


// this is for arabic language. 
mysql_query("SET NAMES utf8"); 

// Insert data into mysql 
$sql="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name')"; 
$sql2="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name2')"; 
$sql3="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name3')"; 
$result=mysql_query($sql); 
$result2=mysql_query($sql2); 
$result3=mysql_query($sql3); 
// if successfully insert data into database, displays message "Successful". 
if($result){ 
echo "Successful"; 
echo "<BR>"; 
// echo "<a href='formadd.php'>Back to main page</a>"; 
} 
else { 
echo "ERROR"; 
echo "<br>"; 
// this for print error in insert process 
echo mysql_error(); 
echo "<a href='expert_add.php'><br>Please try again </a>"; 
} 
//mysql_close($con); 
?> 
<a href="<?php echo $_POST['sys']; ?>">back to form add</a> 
+0

的代碼示例是危險的,因爲它使SQL注入。 –

回答

2

執行您sql查詢僅變量值不等於空返回更迭。 試試這個,

 $expert_name = trim($_POST['expert_name']); 
     $expert_name2 = trim($_POST['expert_name2']); 
     $expert_name3 = trim($_POST['expert_name3']); 


     // this is for arabic language. 
     mysql_query("SET NAMES utf8"); 

     // Insert data into mysql 
     if ($expert_name != "") { 
      $sql = "INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name')"; 
      $result = mysql_query($sql); 
     } 

     if ($expert_name2 != "") { 
      $sql2 = "INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name2')"; 
      $result2 = mysql_query($sql2); 
     } 
     if ($expert_name != "") { 
      $sql3 = "INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name3')"; 
      $result3 = mysql_query($sql3); 
     } 



     // if successfully insert data into database, displays message "Successful". 
     if ($result || $result2 || $result3) { 
      echo "Successful"; 
      echo "<BR>"; 
      // echo "<a href='formadd.php'>Back to main page</a>"; 
     } else { 
      echo "ERROR"; 
      echo "<br>"; 
      // this for print error in insert process 
      echo mysql_error(); 
      echo "<a href='expert_add.php'><br>Please try again </a>"; 
     } 
     //mysql_close($con); 
     ?> 
     <a href="<?php echo $_POST['sys']; ?>">back to form add</a> 

您也應該檢查$result2$result3。我補充說,在這個答案中

+0

許多許多thaaaaaanks – nasr25

+0

謹防SQL注入! –

0
if(!empty($textbox1_value)) { 

    //DO SQL 

} 

但是,您可以對多個框重複此操作,但空操作符會檢查它是否爲空,因此如果它不爲空,則「// DO SQL」區域將會運行。

1

試試這個

if (!empty($_POST['expert_name'])){ 
    $sql="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name')"; 
    $result=mysql_query($sql); 
} 

if (!empty($_POST['expert_name2'])){ 
    $sql2="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name2')"; 
    $result2=mysql_query($sql2); 
} 

if (!empty($_POST['expert_name3'])){ 
    $sql3 ="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name3')"; 
    $result3 =mysql_query($sql3); 
} 
1

那麼你可能要檢查如果變量是空的()。

<?php 

include("connect.php"); 


$expert_name = trim($_POST['expert_name']); 
$expert_name2 = trim($_POST['expert_name2']); 
$expert_name3 = trim($_POST['expert_name3']); 


// this is for arabic language. 
mysql_query("SET NAMES utf8"); 

// Insert data into mysql 
if(!empty($expert_name)) { 
$sql="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name')"; 
$result=mysql_query($sql); 
}  
if(!empty($expert_name2)) { 
$sql2="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name2')"; 
$result2=mysql_query($sql2); 
} 
if(!empty($expert_name3)) { 
$sql3="INSERT INTO experts(id,expert_name) VALUES(NULL, '$expert_name3')"; 
$result3=mysql_query($sql3); 
} 
// if successfully insert data into database, displays message "Successful". 
if($result){ 
echo "Successful"; 
echo "<BR>"; 
// echo "<a href='formadd.php'>Back to main page</a>"; 
} 
else { 
echo "ERROR"; 
echo "<br>"; 
// this for print error in insert process 
echo mysql_error(); 
echo "<a href='expert_add.php'><br>Please try again </a>"; 
} 

另請注意:您只檢查$ result是否可以。如果您只填充文本框2並將1留空,則會插入2的值,但會顯示錯誤。

0

我說你的代碼需要全面審查,但因爲它是現在,你將不得不做這樣的事情每個查詢:

if (!empty($expert_name2){ 
    $result2=mysql_query($sql2) 
} 

但是你應該嘗試循環您的查詢中的foreach,而不是手動編寫每個查詢。順便說一句:

if($result){ 
echo "Successful"; 
echo "<BR>"; 
// echo "<a href='formadd.php'>Back to main page</a>"; 
} 

此代碼僅在第一次wuery成功,因爲你使用$result這是在第一次查詢中只設置

0

ID可能是NOT NULL AUTO_INCREMENT,所以不會接受NULL作爲值。 嘗試發送空白值,如:

$sql="INSERT INTO experts(id,expert_name) VALUES ('', '$expert_name')"; 

此外,建立批量插入,而不是多個。 我將解釋爲什麼當插入單個插入到數據庫中時,插入值,然後數據庫引擎會刷新索引(它們寫入磁盤),除非您在my.cnf中設置了delay_key_write = ALL。索引刷新直接影響你的數據庫性能。

請檢查重做的代碼。代碼調整爲批量插入,出於安全目的的sql字符串轉義以及針對post鍵存在的額外驗證。

<?php 
include("connect.php"); 

// this is for arabic language. 
mysql_query("SET NAMES utf8"); 
$values = array(); 
$skipInsert = true; 
$fields = array('expert_name', 'expert_name2', 'expert_name3'); 
$insert = "INSERT INTO experts(id,expert_name) VALUES "; 
// Loop through predefined fields, and prepare values. 
foreach($fields AS $field) { 
    if(isset($_POST[$field]) && !empty($_POST[$field])) { 
     $values[] = "('', '".mysql_real_escape_string(trim($_POST[$field]))."')"; 
    } 
} 

if(0 < sizeof($values)) { 
    $skipInsert = false; 
    $values = implode(',', $values); 
    $insert .= $values; 
} 

if(false === $skipInsert) { 
    mysql_query($insert); 
} 
// if successfully insert data into database, displays message "Successful". 
if($result){ 
    echo "Successful","<BR>"; 
    // echo "<a href='formadd.php'>Back to main page</a>"; 
} else { 
    echo "ERROR","<br>",mysql_error(),"<a href='expert_add.php'><br>Please try again </a>"; 
} 

貼HTH, VR