Candra Sihombing
Candra Sihombing

Reputation: 61

PHP MySQL Insert database

I have code below:

  for($x=0; $x<=count($_POST["id_k"])-1; $x++) {
    $db->query("INSERT INTO evaluation (id_e, id_k, id_p, bulan, tahun, k1, k2, k3, k4, 
         k5, k6, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, e1, e2, e3, e4, e5, e6, e7, 
         e8, e9, e10, e11, e12, f1, f2, f3, remark, iplog, created, modified) 

         VALUES (null, '$id_k[$x]', $id_p, $bulan, $tahun, '$k1[$x]', '$k2[$x]', 
         '$k3[$x]', '$k4[$x]', '$k5[$x]', '$k6[$x]', '$s1[$x]', '$s2[$x]', '$s3[$x]', '$s4[$x]',
         '$s5[$x]', '$s6[$x]', '$s7[$x]', '$s8[$x]', '$s9[$x]', '$s10[$x]', '$e1[$x]', '$e2[$x]', 
         '$e3[$x]', '$e4[$x]', '$e5[$x]', '$e6[$x]', '$e7[$x]', '$e8[$x]', '$e9[$x]', '$e10[$x]',
         '$e11[$x]', '$e12[$x]', '$f1[$x]', '$f2[$x]', '$f3[$x]', $remark, $iplog, 
         $created, $modified)");
    }

When executing that query I got the following error:

 Warning: 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 '14:59:57, ::1, 
 2014-11-06 14:59:57, 2014-11-06 14:59:57)' at line 2 in

How can I fix this error?

Upvotes: 2

Views: 104

Answers (1)

Mureinik
Mureinik

Reputation: 311978

You're missing quotes around the dates in order to make them character literals:

for($x=0; $x<=count($_POST["id_k"])-1; $x++) {
    $db->query("INSERT INTO evaluation (id_e, id_k, id_p, bulan, tahun, k1, k2, k3, k4, 
         k5, k6, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, e1, e2, e3, e4, e5, e6, e7, 
         e8, e9, e10, e11, e12, f1, f2, f3, remark, iplog, created, modified) 

         VALUES (null, '$id_k[$x]', $id_p, $bulan, $tahun, '$k1[$x]', '$k2[$x]', 
         '$k3[$x]', '$k4[$x]', '$k5[$x]', '$k6[$x]', '$s1[$x]', '$s2[$x]', '$s3[$x]', '$s4[$x]',
         '$s5[$x]', '$s6[$x]', '$s7[$x]', '$s8[$x]', '$s9[$x]', '$s10[$x]', '$e1[$x]', '$e2[$x]', 
         '$e3[$x]', '$e4[$x]', '$e5[$x]', '$e6[$x]', '$e7[$x]', '$e8[$x]', '$e9[$x]', '$e10[$x]',
         '$e11[$x]', '$e12[$x]', '$f1[$x]', '$f2[$x]', '$f3[$x]', $remark, $iplog, 
         '$created', '$modified')");
    }

Upvotes: 1

Related Questions