0

我首先创建了只有一个主键的表,然后使用以下方法将另外 1 列转换为主键:

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");
    }
4

0 回答 0