2014-01-19 26 views
0

有3張桌子「學生」,「部門」,「prev_edu」...表學生是家長,其'ID'是FK在其他...我插入記錄,但在其他兩個表(部,prev_edu)的SQL不工作....只是插入數據到表'學生'... ..有具體的代碼..(usnig php我的管理員)....應該有什麼?有3張桌子「學生」,「部門」,「prev_edu」... tbl學生是家長,其'ID'是其他人的FK

if($query=="insert_student"){ 
     $sql ="Insert Into student Values(' ','".$_POST["txtName"]."','".$_POST["txtfatherName"]."','".$_POST["txtdob"]."','".$_POST["txtgender"]."','".$_POST["txtcnic"]."','".$_POST ["txtMstatus"]."','".$_POST["txtemail"]."','".$_POST["txtaddress"]."','".$_POST["txtoccupation"]."','".$_POST["txtcommaddress"]."','".$_POST["txtnationality"]."','".$_POST["txtrstatus"]."','".$_POST["txtphoneno"]."','".$_POST["txtmob"]."')"; 

     if(mysql_query($sql,$link)){ 
    $sqlrs = "Insert Into dept Values('?','".$_POST["txtcampus"]."','".$_POST["txtprogram"]."','".$_POST["txtDegree"]."','')"; 
    if(mysql_query($sqlrs,$link)){ 


    $sqlrss = "INSERT INTO prev_edu VALUES ('?','".$_POST["txtClass1"]."','".$_POST["txtYear1"]."','".$_POST["txtAnnual1"]."','".$_POST["txtRollno1"]."','".$_POST["txtMarks1"]."','".$_POST ["txtResult1"]."','".$_POST["txtGroup1"]."','".$_POST["txtBoard1"]."','')"; 

      if(mysql_query($sqlrss,$link)){ 
     header("Location: Main.html"); 
    }else { 

     header("Location: Error.php?msg=Insertion Failed"); 
+0

在插入查詢之後使用'echo mysql_error();'來調試錯誤。 –

回答

0
if($query=="insert_student"){ 
     $sql ="INSERT INTO student VALUES('', 
             '".$_POST["txtName"]."', 
             '".$_POST["txtfatherName"]."', 
             '".$_POST["txtdob"]."', 
             '".$_POST["txtgender"]."', 
             '".$_POST["txtcnic"]."', 
             '".$_POST ["txtMstatus"]."', 
             '".$_POST["txtemail"]."', 
             '".$_POST["txtaddress"]."', 
             '".$_POST["txtoccupation"]."', 
             '".$_POST["txtcommaddress"]."', 
             '".$_POST["txtnationality"]."', 
             '".$_POST["txtrstatus"]."', 
             '".$_POST["txtphoneno"]."', 
             '".$_POST["txtmob"]."' 
             )"; 
     @mysql_query($sql, $link); 
     if(mysql_affected_rows($link) == 1){ 
      //get the last insert ID (SID) 
      $sid = mysql_insert_id($link); 
      $sqlrs = "INSERT INTO dept VALUES('', 
              '".$_POST["txtcampus"]."', 
              '".$_POST["txtprogram"]."', 
              '".$_POST["txtDegree"]."', 
              '".$sid."' 
              )"; 
      @mysql_query($sqlrs, $link); 
      if(mysql_affected_rows($link) == 1){ 
      $sqlrss = "INSERT INTO prev_edu VALUES ('', 
                '".$_POST["txtClass1"]."', 
                '".$_POST["txtYear1"]."', 
                '".$_POST["txtAnnual1"]."', 
                '".$_POST["txtRollno1"]."', 
                '".$_POST["txtMarks1"]."', 
                '".$_POST ["txtResult1"]."', 
                '".$_POST["txtGroup1"]."', 
                '".$_POST["txtBoard1"]."', 
                '".$sid."' 
                )"; 
      @mysql_query($sqlrss, $link); 

      if(mysql_affected_rows($link) == 1){ 
       @header("Location:Main.html"); 
       exit; 
      } 
      } 
    } 
    header("Location:Error.php?msg=Insertion Failed"); 
} 
+0

thnx for ur ans ...但它仍然是一樣的... – user3210071

+0

試圖在這裏發佈你的表格結構 – phpCore

+0

tble結構是 學生(sid(PK),姓名,價值,價值) dept(did(pk),campus,degree,prog,sid(FK)) prev_edu(pid(pk),價值,價值,sid(FK)) – user3210071

0

嘗試使用mysqliPDO,而不是MySQL的,因爲mysql_ *功能已被棄用。您還必須validate用戶正確輸入並嘗試使用prepared statements,因爲它們可以幫助您避免SQL注入攻擊。接下來,我假設在您的查詢中放入?,該字段是您的主鍵,並且具有自動增量功能。

你可以嘗試以下方法:

$mysqli = new mysqli("hostname", "username", "password", "database_name"); 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

//這裏....指示領域

$sql ="INSERT INTO student VALUES('',?,?....)"; 
$stmt = $mysqli->prepare($sql); 
$stmt->bind_param("ss...", $_POST["txtName"],$_POST["txtdob"],....); 

而且我猜測,您試圖其餘保存user_id後續的查詢。

我希望這可以幫助你。在準備好的語句上查看更多信息here

+0

thnx的建議....但我不在乎在這個級別的sql注入....只有problm是thr ..其他兩個quries($ sql&$ sqlrs)不工作....我knw那裏是FK約束概率一些whre – user3210071

+0

我猜你正試圖保存後續查詢中的學生ID,對不對? –

+0

yup ...你的權利 – user3210071

相關問題