【问题标题】:PHP MYSQL Insert into with switch statementPHP MYSQL Insert into with switch 语句
【发布时间】:2021-07-02 02:11:45
【问题描述】:

首先,我很抱歉,因为我还是个新手,想了解更多关于编码的知识。

我目前正在处理这个问题。 我的第一张、第二张和第三张桌子工作正常。我的问题是我的第四张桌子没有收到任何值。 我尝试使用 $eGrad 等于 Grade Level 的 switch 语句在我的第四张表上插入数据。 我想根据年级在我的 st_grade 表(第 4 个表)上插入值,其中一些是常量值。

我将不胜感激。谢谢! 这是我正在工作的代码

public function insert($stID, $eFullname, $eGrad, $secID, $eStatus, $eSY, $syID,  $rformOne, $rformTwo, $rPsa, $rGm, $rPic){

        $firstTB = "INSERT INTO st_enrolled (e_stid, e_fullname,e_grade,e_status,e_sy) VALUES('$stID', '$eFullname', '$eGrad',  '$eStatus', '$eSY')";
        $secondTB = "INSERT INTO st_sy (st_sy_stid, st_sy_syid) VALUES('$stID', '$syID')";
        $thirdTB = "INSERT INTO st_requirement (str_id, str_fullname, formOne, formTwo, psa, goodmoral, picture) VALUES ('$stID', '$eFullname',  '$rformOne', '$rformTwo', '$rPsa', '$rGm', '$rPic')";
        
        
        switch($eGrad){
            
            case "Grade1" :
                $fourthTB = "INSERT INTO `st_grade`(`st_id`,`subj_id`,`gr_id`,`sec_id`,`sy_id`,`first`,`second`,`third`,`fourth`,`ave`,`remarks`) VALUES 
                ($stID, 1, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 2, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 3, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 4, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 5, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 6, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 7, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),";
                break;
            case "Grade2" :
                $fourthTB = "INSERT INTO `st_grade`(`st_id`,`subj_id`,`gr_id`,`sec_id`,`sy_id`,`first`,`second`,`third`,`fourth`,`ave`,`remarks`) VALUES 
                ($stID, 8, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 9, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 10, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 11, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 12, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 13, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 14, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),";
                break;  
            default: 
                echo "error";
        }


        if ($this->conn->query($firstTB) && $this->conn->query($secondTB) && $this->conn->query($thirdTB) && $this->conn->query($fourthTB) ) {
            return true;
        } else {
            return false;
        }

    }

【问题讨论】:

标签: php mysql sql switch-statement sql-insert


【解决方案1】:

我已经删除了你的 sql 字符串中的最后一个逗号,现在它应该可以工作了

public function insert($stID, $eFullname, $eGrad, $secID, $eStatus, $eSY, $syID,  $rformOne, $rformTwo, $rPsa, $rGm, $rPic){

        $firstTB = "INSERT INTO st_enrolled (e_stid, e_fullname,e_grade,e_status,e_sy) VALUES('$stID', '$eFullname', '$eGrad',  '$eStatus', '$eSY')";
        $secondTB = "INSERT INTO st_sy (st_sy_stid, st_sy_syid) VALUES('$stID', '$syID')";
        $thirdTB = "INSERT INTO st_requirement (str_id, str_fullname, formOne, formTwo, psa, goodmoral, picture) VALUES ('$stID', '$eFullname',  '$rformOne', '$rformTwo', '$rPsa', '$rGm', '$rPic')";
        
        
        switch($eGrad){
            
            case "Grade1" :
                $fourthTB = "INSERT INTO `st_grade`(`st_id`,`subj_id`,`gr_id`,`sec_id`,`sy_id`,`first`,`second`,`third`,`fourth`,`ave`,`remarks`) VALUES 
                ($stID, 1, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 2, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 3, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 4, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 5, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 6, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 7, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, '')";
                break;
            case "Grade2" :
                $fourthTB = "INSERT INTO `st_grade`(`st_id`,`subj_id`,`gr_id`,`sec_id`,`sy_id`,`first`,`second`,`third`,`fourth`,`ave`,`remarks`) VALUES 
                ($stID, 8, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 9, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 10, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 11, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 12, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 13, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),
                ($stID, 14, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, '')";
                break;  
            default: 
                echo "error";
        }


        if ($this->conn->query($firstTB) && $this->conn->query($secondTB) && $this->conn->query($thirdTB) && $this->conn->query($fourthTB) ) {
            return true;
        } else {
            return false;
        }

    }

【讨论】:

  • 不应回答错字问题,应将其标记为此类问题(Flag 问题下的链接>“需要改进”>“社区特定原因”>“不可重现或由错字'),同时留下评论,指示 OP 关于问题的原因。
【解决方案2】:

最后的逗号导致问题

之前

($stID, 7, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),";
($stID, 14, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),";

之后

($stID, 7, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, '')";
($stID, 14, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, '')";

在两个 switch 案例的末尾删除逗号(,),它会正常工作

【讨论】:

  • 不应回答错字问题,应将其标记为此类问题(Flag 问题下的链接>“需要改进”>“社区特定原因”>“不可重现或由错字'),同时留下评论,指示 OP 关于问题的原因。
猜你喜欢
  • 2011-12-04
  • 1970-01-01
  • 2013-08-02
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2015-02-21
相关资源
最近更新 更多