PHP MYSQL 插入 with switch 语句
PHP MYSQL Insert into with switch statement
首先,我很抱歉,因为我还是个新手,想学习更多关于编码的知识。
我目前正在处理这个问题。
我的第一个、第二个和第三个 table 工作正常。我的问题是我的第 4 个 table 没有收到任何值。
我尝试使用 $eGrad 等于 Grade Level 的 switch 语句在我的第 4 个 table 上插入数据。
我想根据年级在 st_grade table(4th table) 上插入值,其中一些是常数值。
如有任何帮助,我将不胜感激。谢谢!
这是我正在使用的代码
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;
}
}
我已经删除了你的 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;
}
}
末尾的逗号导致问题
之前
($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 cases 中删除末尾的逗号 (,),它会正常工作
首先,我很抱歉,因为我还是个新手,想学习更多关于编码的知识。
我目前正在处理这个问题。 我的第一个、第二个和第三个 table 工作正常。我的问题是我的第 4 个 table 没有收到任何值。 我尝试使用 $eGrad 等于 Grade Level 的 switch 语句在我的第 4 个 table 上插入数据。 我想根据年级在 st_grade table(4th table) 上插入值,其中一些是常数值。
如有任何帮助,我将不胜感激。谢谢! 这是我正在使用的代码
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;
}
}
我已经删除了你的 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;
}
}
末尾的逗号导致问题
之前
($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 cases 中删除末尾的逗号 (,),它会正常工作