【发布时间】:2015-04-22 04:54:39
【问题描述】:
我有三个表:users、courses 和 grads。
- 用户表(id、用户名、pass、course_id)。
- 课程表(id、课程名称、描述)。
- grads 表(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->create 之后添加了这段代码,所以它看起来像那样
$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()
【问题讨论】:
-
请出示您的代码并说明问题所在,以便我们帮助您解决问题。
-
我认为您正在寻找的是 MySQL
LAST_INSERT_ID()函数。这包含分配给刚刚插入的行的 auto_increment ID,您可以在使用外键插入表时使用它。 -
thanx,,,我把所有的东西都包装在了类中,,, 所以很难解释...但是如果您对我写的示例进行更多解释会有所帮助...。 . last_insert_id 为最后添加的user_id,,,,,,,, course_id 呢????
-
为什么
grads表中有course_id列?如果与users中的course_id相同,则无需重复。