将相同表单中的第三个表的外键提交到两个不同的表中

时间:2015-02-20 13:32:09

标签: php mysql forms pdo

我有三个表:用户,课程和毕业生。

  • 用户表(id,username,pass,course_id)。
  • 课程表(id,coursename,description)。
  • grads table(user_id,course_id,grade)。

我有一个表单来添加一个新用户,其中包含一个下拉列表,该列表显示值为" id"的可用课程。从课程表。

当课程被添加到数据库时,下拉列表将被更新。

现在,我尝试做的是:

当我使用表单添加新用户并从下拉列表中选择课程时,我希望选择发送到成绩表(course_id)的课程和将添加的(user_id)的相同值同样的提交行动。

我使用Wrapper类php和PDO,只需用普通的PDO解释。

<?php
require_once 'core/init.php';
include 'includes/header.php';
include 'includes/headeradmin.php';



if(Input::exists()) {
    if(Token::check(Input::get('token'))) {   ### Security check

   $validate = new Validate();
   $validation = $validate->check($_POST, array(

    'email' => array(
        'required' => true,
        'min'      => 2,
        'max'      => 20,
        'unique'   => 'users'
    ),

    'password' => array(
        'required'  => true,
        'min'       => 6
    ),

    'password_again' => array(
        'required'  => true,
        'matches'   => 'password'
    ),

    'fullname' => array(
        'required'  => true,
        'min'       => 2,
        'max'       => 20
    ),

    'group_id' => array(

        'min'       => 1,
        'max'       => 20
    ),

    'class_id' => array(

        'min'       => 1,
        'max'       => 20
    ),

    'course_id' => array(

        'min'       => 1,
        'max'       => 20
    )

));



if($validation->passed()) {
            $user = new User();
            $salt = Hash::salt(32);

            try {

            $user->create(array(
                'email' => Input::get('email'),
                'password' => Hash::make(Input::get('password'), $salt),
                'fullname' => Input::get('fullname'),
                'address'  => Input::get('address'),
                'telnumber'=> Input::get('telnumber'),
                'salt'     => $salt,
                'group_id' => Input::get('group_id'),
                'class_id' => Input::get('class_id'),
                'course_id'=> Input::get('course_id'),

                ));


                    echo "En ny användaren Har skapat";
                #Session::flash('home', 'you are registerd successfully');
                #Redirect::to('index.php');
            } catch (Exception $e) {
                die ($e->getMessage());
            }

        #Session::flash('success', 'You registered successfully');
        #header('Location: index.php');
   } else {
      foreach($validation->errors() as $error) {
        echo $error,'<br>';

      }
    }
}  
}

?>



<form  id ="regform" action="" method="post">
    <div id="register">
    <table>
        <tr>
        <td style="width:130px; text-align: right"><label for="email">Email:</label></td>
        <td style="width: 200px"><input type="email" name="email" id="email" value="<?php echo escape(Input::get('email')); ?>" autocomplete="off" width="200px"></td>

    </tr>

   <tr>
        <td style="width:130px; text-align: right"><label for="password">Password:</label>
        <td style="width: 200px"><input type="password" name="password" id="password" width="200px"></td>
    </tr>

    <tr>
        <td style="width:130px; text-align: right"><label for="password_again">Re Enter your password:</label></td>
        <td style="width: 200px"><input type="password" name="password_again" id="password_again" width="200px"></td>
    </tr>

   <tr>
        <td style="width:130px; text-align: right"><label for="fullname">Full Name:</label></td>
        <td style="width: 200px"><input type="text" name="fullname" id="fullnmae" value="<?php echo escape(Input::get('fullname')); ?>" autocomplete="off" width="200px"></td>
    </tr>

    <tr>
        <td style="width:130px; text-align: right"><label for="address">Address:</label></td>
        <td style="width: 200px"><input type="text" name="address" id="address" width="200px"></td>
    </tr>

    <tr>
        <td style="width:130px; text-align: right"><label for="telnumber">Tel. Number:
        </label></td>
        <td style="width: 200px"><input type="text" name="telnumber" id="telnumber" width="200px"></td>
    </tr>

   <tr>
        <td style="width:130px; text-align: right"><label for="group_id">Permission:</label></td>
        <td style="width: 200px; text-align: center"><select name="group_id" width="200px">
        <option value="0">Select a permission</option>
        <option value="1">Student</option>
        <option value="2">Admin</option>
        <option value="3">Teacher</option>
        </select></td>
 </tr>

    <tr>
        <td style="width:130px; text-align: right"><label for="class_id">Class:</label></td>
        <td style="width: 200px; text-align: center"><select name="class_id" width="200px">
        <option value="0">Select a Class</option>
        <?php 
            $class = DB::getInstance()->query("SELECT * FROM classes");
                foreach ($class->results() as $class) {
        echo '<option value="'. $class->id .'">' . $class->classname . '</option>';
        }
        ?>
        </select></td>

    </tr>

    <tr>
        <td style="width:130px; text-align: right"><label for="course_id">Course:</label></td>
        <td style="width: 200px; text-align: center"><select name="course_id" width="200px">
        <option value="0">Select a Course</option>

        <?php 
            $course = DB::getInstance()->query("SELECT * FROM courses");
                foreach ($course->results() as $course) {
        echo '<option value="'. $course->id .'">' . $course->coursename . '</option>';
        }
        ?>

        </select></td>
    </tr>
    <tr>
    <td style="width:130px; text-align: right"></td>
    <td style="width: 200px"><input type="hidden" name="token" value="<?php echo Token::generate(); ?>"></td>
    </tr>
    <tr>
       <td style="width:130px; text-align: right"></td>
        <td style="width: 200px; text-align: center"> <input type="submit"  value="Register"></td>
    </tr>
    </table>

我在$ user-&gt;创建之后添加了此代码,因此它看起来像那样

$user->create(array(
                'email' => Input::get('email'),
                'password' => Hash::make(Input::get('password'), $salt),
                'fullname' => Input::get('fullname'),
                'address'  => Input::get('address'),
                'telnumber'=> Input::get('telnumber'),
                'salt'     => $salt,
                'group_id' => Input::get('group_id'),
                'class_id' => Input::get('class_id'),
                'course_id'=> Input::get('course_id'),

                ));

                $grad = DB::getInstance()->insert('grads', array(
                        'user_id' => Input::get(LAST_INSERT_ID()),
                        'course_id' => Input::get('course_id'),
                    ));

但是我收到了这个错误: 致命错误:在第77行的C:\ xampp \ htdocs \ Jensenonline1 \ register.php中调用未定义的函数LAST_INSERT_ID()

1 个答案:

答案 0 :(得分:0)

使用MySQL中的LAST_INSERT_ID()函数获取分配给刚刚创建的用户的ID。使用如下查询创建新用户:

$stmt1 = $pdo->prepare("INSERT INTO users (username, pass, course_id) 
                        VALUES (:username, :pass, :course_id)");
$stmt1->execute(array(':username' => $_POST['username'],
                      ':pass' => $_POST['password'],
                      ':course_id' => $_POST['course_id']));

然后要添加到grades,请使用:

$stmt2 = $pdo->prepare("INSERT INTO grades (user_id, course_id)
                        VALUES (LAST_INSERT_ID(), :course_id)");
$stmt2->execute(array(':course_id' => $_POST['course_id']));

由于在执行两个查询时使用相同的$_POST['course_id'],因此相同的值将放在两个表中。