首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我希望将单个表单中的数据插入多个表中,这两个表都包含关系。

问我希望将单个表单中的数据插入多个表中,这两个表都包含关系。
EN

Stack Overflow用户
提问于 2014-01-14 03:50:50
回答 2查看 83关注 0票数 0

我是新的php,并试图学习。我还试图制作一个网络应用程序“学校管理系统”。当我从相同的表格中插入学生和家长表中的记录时,我遇到了问题,表单是可以的,它的插入记录在学生表中,而不是在家长表中。在父表和学生表中,student_id是常见的,在学生表中,student_id包含主键,在parents表中包含外键。我的代码如下:

代码语言:javascript
复制
<?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');
         }
         ?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-14 05:48:52

就像乔·李在上面的片段中提到的,

代码语言:javascript
复制
$generated_key=mysql_insert_id();

//这一行代码实际上返回自动生成的id,当然,我猜您需要父表的学生表中的自动递增值。

票数 0
EN

Stack Overflow用户

发布于 2014-01-14 04:03:26

查找最后插入的id u,使用mysql_insert_id()

代码语言:javascript
复制
<?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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21105790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档