2013-10-03 59 views
1

我只是編程中的新手,我不知道我的代碼中存在什麼問題。我在tblins中輸入的數據被記錄下來,但tbluser中的數據不會出現在我的數據庫中。但是當我嘗試刪除我的tblins的插入查詢時,我想要在tbluser中輸入的數據可以記錄在我的數據庫中。我應該怎麼做才能讓我的兩個表格可以記錄我在頁面中點擊提交後所輸入的所有數據?謝謝。php sql插入表

$usr="INSERT INTO tbluser(username,password,type) VALUES('".$_POST['txtuname']."','".$_POST['txtpass']."','".$_POST['type']."')"; 
$ins="INSERT INTO tblins(insLN,insFM,insMN,insadd,insCN,insemail,insbdate) VALUES('".$_POST['txtLN']."','".$_POST['txtFN']."','".$_POST['txtMN']."','".$_POST['txtadd']."','".$_POST['txtCN']."','".$_POST['txtemail']."','".$bdate."')"; 

感謝所有的建議先生。 :D我目前準備好什麼SQL注入。希望瞭解更多。 :D

這是我的完整代碼。

<?php 
include("connect.php"); 
if(isset($_POST['txtpass']) && isset($_POST['txtrepass'])){ 

        $password1=mysql_real_escape_string($_POST['txtpass']); 
        $password2=mysql_real_escape_string($_POST['txtrepass']); 

        if($password1==$password2){ 



           $typeopt=$_POST['type']; 

           $bdate=$_POST['year']."-".$_POST['month']."-".$_POST['day']; 

           switch($typeopt){ 
             case 'ins': 


             $usr=mysql_query("INSERT INTO tbluser(username,password,type) VALUES('".$_POST['txtuname']."','".$_POST['txtpass']."','".$_POST['type']."')"); 

             $ins=mysql_query("INSERT INTO tblins(insLN,insFM,insMN,insadd,insCN,insemail,insbdate) VALUES('".$_POST['txtLN']."','".$_POST['txtFN']."','".$_POST['txtMN']."','".$_POST['txtadd']."','".$_POST['txtCN']."','".$_POST['txtemail']."','".$bdate."')"); 


             if(mysqli_query($con,$ins)) { 
              echo"success"; 
             } 
             else{ 
              echo"fail to register"; 
             } 

             break; 

             case 'student': 
             $std="INSERT INTO tblstudent(studLN,studFN,studMN,studBDate,studemail,studadd,studCN)"; 
             $usr="INSERT INTO tbluser(username,password,type)"; 
             $usr=$usr."VALUES('".$_POST['txtuname']."',"; 
             $usr=$usr."'".$_POST['txtpass']."',"; 
             $usr=$usr."'".$_POST['type']."')"; 

             $std=$std."VALUES('".$_POST['txtLN']."',"; 
             $std=$std."'".$_POST['txtFN']."',"; 
             $std=$std."'".$_POST['txtMN']."',"; 
             $std=$std."'".$bdate."',"; 
             $std=$std."'".$_POST['txtemail']."',"; 
             $std=$std."'".$_POST['txtadd']."',"; 
             $std=$std."'".$_POST['txtCN']."')"; 

             if(mysqli_query($con,$std)) { 
              echo"success"; 
             } 
             else{ 
              echo"fail to register"; 
             } 

           } 

        } 
        else{ 
         echo"<form>"; 
         echo "Password doesn't match. Try registering again."; 
         echo "<input type=submit formaction=register.php value=back>"; 
         echo"</form>"; 
        } 
       } 

?>

+3

你可以在你試圖將這些代碼寫入數據庫的地方添加代碼嗎? – andrewsi

+1

你應該檢查任何PHP錯誤或MySQL錯誤。有可能有一個錯誤信息告訴你*確切的*有什麼問題。另外,你的代碼對SQL注入攻擊是全面的。你需要閱讀和理解這個:http://php.net/manual/en/security.database.sql-injection.php – David

+1

這應該是你的數據庫連接錯誤。在這裏粘貼你的完整代碼 –

回答

3

首先,你在建立查詢的方式是很容易出現錯誤和SQL注入。

怎麼樣的東西多一點清潔劑,如:

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password'); 
$stmt = $db->prepare("INSERT INTO table(field1,field2,field3,field4,field5) VALUES(:field1,:field2,:field3,:field4,:field5)"); 
$stmt->execute(array(':field1' => $field1, ':field2' => $field2, ':field3' => $field3, ':field4' => $field4, ':field5' => $field5)); 

,並確保檢查錯誤消息。

+0

PDO是什麼意思先生? –

+1

@benarylagadan - http://php.net/manual/en/book.pdo.php – andrewsi

+0

@benarylagadan閱讀以下內容:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers –