PHP MYSQL Insert into with switch statement
Get the solution ↓↓↓first of all, I'm sorry because I'm still a newbie and want to learn more about coding.
I'm currently working on this. My 1st, 2nd, and 3rd table are working fine. My problem is my 4th table is not receiving any values. I trying to insert data on my 4th table with a switch statement where $eGrad is equal to Grade Level. I want to insert values on my st_grade table(4th table) base on grade level and some of them are constant values.
I'll appreciate any help. Thanks! Here is the code that I'm working
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;
}
}
Answer
Solution:
i've deleted last comma in your sql string now it should works
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;
}
}
Answer
Solution:
comma at the end is causing problem
before
($stID, 7, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),";
($stID, 14, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, ''),";
after
($stID, 7, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, '')";
($stID, 14, $eGrad, $secID, $syID, 0, 0, 0, 0, 0, '')";
remove comma(,) at the end in both switch cases and it'll work fine
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot set properties of undefined (setting '_dt_cellindex')
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.