2014-01-14 43 views
0

我是新來的PHP和試圖學習。我也試圖製作一個網絡應用程序「學校管理系統」。 我遇到問題,而我插入記錄在學生和家長表格從相同的形式,形式是好的,它的插入記錄在學生表,但不是在父母表。家長和學生表中的 student_id是常見的,學生表中的student_id包含主鍵,父母表中包含外鍵。 我的代碼如下:我想從單個表單插入數據到多個表中這兩個表都包含關係


<?php> 
    if(isset($_POST['submit'])){ 
$reg = $_POST['reg_no']; 
$s_name = $_POST['student_name']; 
$s_father = $_POST['student_father']; 
$p_last_name = $_POST['parent_lastname']; 
$s_birth = $_POST['student_birth']; 
$p_phone = $_POST['parent_phone']; 
$p_address = $_POST['parent_address']; 
$school_name = $_POST['school_name']; 
$batch = $_POST['session_batch']; 
//filtering variables 
    $reg_no = mysql_real_escape_string($reg); 
$student_name = mysql_real_escape_string($s_name); 
$student_father = mysql_real_escape_string($s_father); 
$parent_last_name= mysql_real_escape_string($p_last_name); 
$student_birth = mysql_real_escape_string($s_birth); 
$parent_phone = mysql_real_escape_string($p_phone); 
$parent_address = mysql_real_escape_string($p_address); 
$school = mysql_real_escape_string($school_name); 
$batch = mysql_real_escape_string($batch); 
    //connecting to db by including db file 
    include_once('include/dbconnect.php'); 

     $db_select = mysql_select_db($server_db_name,$db_connect); 
     if ($db_connect) 
     { 
$student_query = "INSERT INTO students (school_id, session_id, student_name, 
    student_father, student_birthdate, registration_no) VALUES 
    ('$school','$batch','$student_name','$student_father','$student_birth','$reg_no')"; 
    $s_query = mysql_query($student_query) or DIE ("error. while inserting records in 
    student"); 
    /* here im trying to select student_id which is inserted above to insert data in 
     parents table*/ 
    $id_query = mysql_query("SELECT * FROM students WHERE student_id = $student_name 
    LIMIT 1") or DIE ("Could complete the id query"); 
    while ($id_result = mysql_fetch_array($id_query)) 
    { $s_id = $id_result['student_id']; 
      $parent_query = "INSERT INTO parents (school_id, student_id, parent_name, 
      parent_lastname, parent_phone, parent_address) 
      VALUES('$school','$s_id','$student_father','$parent_last_name', 
      '$parent_phone','$parent_address')"; 
     $p_query = mysql_query($parent_query); 
      if (!$parent_query) { echo "error. while inserting records in student"; } 
       } 
      mysql_close($db_connect); 
    header('location:admin.php?student'); 
    } 
    else { 
    echo "Error While Connecting to server"; 
    } 
       }else { 
      header('location:admin.php?error'); 
     } 
     ?> 
+0

我在做這個以正確的方式嗎? –

+0

什麼是錯誤 –

+0

記錄沒有插入父母表 –

回答

0

像李喬在上面的代碼中已經提到,

$generated_key=mysql_insert_id(); 

//這段代碼實際上會返回自動生成的id,當然我猜你想要自動遞增的值在父表的學生表中。

0

查找用戶ü最後插入,使用mysql_insert_id()

<?php 
mysql_connect("localhost", "mysql_user", "mysql_password") or 
    die("Could not connect: " . mysql_error()); 
mysql_select_db("mydb"); 

mysql_query("INSERT INTO mytable (product) values ('kossu')"); 
printf ("Last inserted record has id %d\n", mysql_insert_id()); 
?> 
+0

補充撇號。 –

+0

mysqli?任何人? – user241221

相關問題