我想将单个表单中的数据插入到多个表中,两个表都包含关系

时间:2014-01-14 03:50:51

标签: php mysql sql

我是php的新手并且正在努力学习。我也试图建立一个网络应用程序“学校管理系统”。 当我从同一个表单中插入学生和父母表中的记录时,我遇到了问题,表格是好的,它在学生表中插入记录但不在父表中。 在parent和student表中,student_id是常见的,在student表中,student_id包含主键,在parent表中包含外键。 我的代码如下:


<?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');
         }
         ?>

2 个答案:

答案 0 :(得分:0)

find id u最后插入,使用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());
?>

答案 1 :(得分:0)

就像JOE LEE在上面的片段中提到的那样,

$generated_key=mysql_insert_id();

//这行代码实际上返回了自动生成的id,当然我猜你想要父表的student表中的自动递增值。