elstiv elstiv - 4 months ago 7
MySQL Question

Insert PHP array and string in MySQL query

I have the following query which is working fine in inputting the arrays

$stid
and
$attendance_status
. However, I also want to populate the column called SCH_TIMESLOT with a fixed number
22
or eventually a string value which is the same for all records. I am getting an error stating:


You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '22' at line 2


This is my query:

for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES
(NULL, $stid[$i], $attendance_status[$i]), 22") or die (mysql_error());


Thank you for your help

Answer

You have missed out 22 from your VALUES brackets. It has to be inside.

<?php
for($i=0;$i<count($attendance_status);$i++){
 mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES
(NULL, $stid[$i], $attendance_status[$i], 22)") or die (mysql_error());