我的数据库名称中有一个表是mdl_assigncourses..so,因为我有列作为id(主键),groupid,courseid,status ..
所以当我要向小组添加课程时,如果它已经存在于表格中,我想显示错误消息,例如'这个课程已经采取了' ..而且它也没有存储在表格中......
现在对我来说它显示了这个错误。您是否记得在调用get_records时让第一列成为独一无二的?重复值' 63'在列'id'中找到。但是这条记录仍然存储在db中......
这是我的插入代码:
if(isset($_POST['assigncourse'])) {
$courseid = $_POST['courseid'];
$courseid = required_param('courseid', PARAM_TEXT);
$groupid = required_param('groupid', PARAM_TEXT);
$insert_record = new stdClass();
$insert_record->courseid = $courseid;
$insert_record->groupid = $_SESSION['groupid'];
$insert_record->assigned_date = date('Y-m-d H:i:s');
if($result = $DB->insert_record('assigncourses', $insert_record) )
{
$_SESSION['successMessage'] = "record created successfully";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
//$stmt->bindParam("groupid", $_SESSION['groupid'],PDO::PARAM_STR) ;
}
任何人都可以帮助我..如何做到这一点..
提前感谢..
答案 0 :(得分:1)
if(isset($_POST['assigncourse'])) {
$groupid=$_SESSION['groupid']
$courseid = $_POST['courseid'];
$courseid = required_param('courseid', PARAM_TEXT);
$groupid = required_param('groupid', PARAM_TEXT);
//$con is your db connection
$sql=mysqli_query($con," SELECT * FROM mdl_assigncourses WHERE groupid='$groupid' AND courseid='$courseid' ");
$row=mysqli_num_rows($sql);
if($row>0){
$insert_record = new stdClass();
$insert_record->courseid = $courseid;
$insert_record->groupid = $_SESSION['groupid'];
$insert_record->assigned_date = date('Y-m-d H:i:s');
if($result = $DB->insert_record('assigncourses', $insert_record) )
{
$_SESSION['successMessage'] = "record created successfully";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
//$stmt->bindParam("groupid", $_SESSION['groupid'],PDO::PARAM_STR) ;
}
}else{
echo "<script>alert('groupid and courseid should be unique');window.location.href='form.php';</script>";
}