在xampp服务器phpadmin数据库中插入重复的值而不是合成键

时间:2019-04-06 08:27:25

标签: sql xampp mariasql

我首先用一个主键创建了表,然后使用以下命令将另外一列转换为主键:

ALTER TABLE students DROP PRIMARY KEY, ADD PRIMARY KEY(student_id, email);

复合密钥创建成功,但是复合密钥无法正常工作。

在插入相同值的电子邮件但具有不同ID数据库的情况下,成功插入该值不会给我catch语句

我尝试了以下代码:

 try{
        $dbhandler = new PDO('mysql:host=127.0.0.1;dbname=project_db','root','');

        $dbhandler->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        $sql="insert into students (student_id,faculty,password,cpi,first_name,last_name,birthdate,email,contact_no,participate,leader,allocated) values (?,?,?,?,?,?,?,?,?,?,0,0)";    //   leader is 0 means not decided
        $query=$dbhandler->prepare($sql);
        $p=0;
        if(isset($_POST['s_participate']))
        {
            $p=1;
        }
        $r=$query->execute(array($_POST['s_id'],$_SESSION['faculty'],$_POST['s_id'],$_POST['s_cpi'],$_POST['s_f_name'],$_POST['s_l_name'],$_POST['s_birthday'],$_POST['s_email'],$_POST['s_contact'],$p));
}
catch(PDOException $e){
            $msg='student with this id is already exist';
            $temp=1;
            header("location:./insert_student.php?msg=".$msg);
    }
if($r){
        header("location:./insert_student.php?msg=Student ".$_POST['s_f_name']." with id ".$_POST['s_id']." is successfully added");
    }

0 个答案:

没有答案