2014-01-19 20 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

相关问题