dqroc48068 2016-09-29 20:36
浏览 76
已采纳

PDO中的beginTransaction

I've recently started learning about PDO. My question is how can i execute more than 1 prepared statement. In my example i'm trying to add a new student to the database. The first part of the code i'm adding the student into the 'students' table. The second part of the code i'm trying to add all of his classes (from array e.g an array(PHP,JAVA,ANGULAR)) into student_class table (which contain 2 columns - student_id and class_id). Here's a snippet of what i've tried:

function addStudent($name, $phone, $email, $classes){
        global $conn;
        //first part
        $stat = $conn->prepare("INSERT INTO students (sName, phone, email) VALUES(:name, :phone, :email)");
        $stat->bindValue("name",$name,PDO::PARAM_STR);
        $stat->bindValue("phone",$phone,PDO::PARAM_STR);
        $stat->bindValue("email",$email,PDO::PARAM_STR);
        $stat->execute();
        //second part
        //insert classes into student_class
        $lastId = $conn->lastInsertId();
        $conn->beginTransaction();
        $len = count($classes);
        for ($i=0; $i < $len; $i++) {
            $cid = getClassByName($classes[$i]);//returns the class id
            $cl = $conn->prepare("INSERT INTO student_class (student_id,class_id) VALUES(:sid, :cid)");
            $cl->bindValue("sid",$lastId,PDO::PARAM_INT);
            $cl->bindValue("cid",$cid,PDO::PARAM_INT);
            $cl->execute();
        }
        $conn->commit();
    }

try{
     addStudent($params['name'], $params['phone'], $params['email'], $params['classes']);
   }
catch(PDOException $e){
       echo $e->getMessage();
       $conn->rollback();
   }

The result of this is: the user gets added to the 'students' table but the classes remain untouched (i'm getting no error), so i guess i'm doing something wrong with the second part. I hope you can shed some light on this matter.

  • 写回答

1条回答 默认 最新

  • dragon201401 2016-09-29 22:58
    关注

    If these are prepared statements then you only "create" them once, and can execute them multiple times. Also edited your code to print error information, use it to debug.

    function addStudent($name, $phone, $email, $classes){
            global $conn;
            //first part
            $stat = $conn->prepare("INSERT INTO students (sName, phone, email) VALUES(:name, :phone, :email)");
            $stat->bindValue("name",$name,PDO::PARAM_STR);
            $stat->bindValue("phone",$phone,PDO::PARAM_STR);
            $stat->bindValue("email",$email,PDO::PARAM_STR);
            $stat->execute();
    
            //second part
            //insert classes into student_class
            $lastId = $conn->lastInsertId();
            $conn->beginTransaction();
            $len = count($classes);
    
            $cl = $conn->prepare("INSERT INTO student_class (student_id,class_id) VALUES(:sid, :cid)");
            if (!$cl) {
                echo "
    PDO::errorInfo():
    ";
                print_r($conn->errorInfo());
            }
            for ($i=0; $i < $len; $i++) {
                $cid = getClassByName($classes[$i]);//returns the class id
                $cl->bindValue("sid",$lastId,PDO::PARAM_INT);
                $cl->bindValue("cid",$cid,PDO::PARAM_INT);
                $cl->execute();
    
                echo "
    PDOStatement::errorInfo():
    ";
                $arr = $cl->errorInfo();
                print_r($arr);
            }
            $conn->commit();
        }
    
    try{
         addStudent($params['name'], $params['phone'], $params['email'], $params['classes']);
       }
    catch(PDOException $e){
           echo $e->getMessage();
           $conn->rollback();
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?