0

Those are the fields on my DB

  • partamento int
  • codigocurso int
  • diurno int
  • contacto int
  • pos_laboral int
  • contacto2 int
  • proc_por int

I’ve been trying to fix this error since yesterday but I cannot figure out where is the error. **

foreach ($diurno as $userId) {
        $data .= "(".$id.",".$grdid.",".$userId.",".$contacto.",".$pos_laboral.",".$contacto2.",".$idd.")";
    }
    
    $data = rtrim($data, ',');
    $sql = "insert into cursosprogramas (departamento, codigocurso, diurno, contacto, pos_laboral, contacto2, proc_por) values (".$data.");";
    echo $sql;

the error

insert into cursosprogramas (departamento, codigocurso, diurno, contacto, pos_laboral, contacto2, proc_por) values ((100,120,7,646,5,363,2)(100,120,4,646,5,363,2));Query failed.

sticky bit
  • 36,626
  • 12
  • 31
  • 42
HQweb
  • 17
  • 5

2 Answers2

2

You are missing a comma between data sets, and you also don't need parenthesis around them all. You are already using rtrim() to remove the very last comma, but you aren't actually adding the comma at the end of the data set.

foreach ($diurno as $userId) {
    
    //add a comma at the end
    //note you can write variables directly into a string that is wrapped with double quotes
    $data .= "('$id', '$grdid', '$userId', '$contacto', '$pos_laboral', '$contacto2', '$idd'),";
}

//this gets rid of the very last comma in the string
$data = rtrim($data, ',');

//remove parenthesis around `$data`
$sql = "insert into cursosprogramas (departamento, codigocurso, diurno, contacto, pos_laboral, contacto2, proc_por) values {$data};";
echo $sql;

NOTE: Little Bobby says this code MAY be at risk for SQL Injection Attacks depending on how the variables inside of $data are created. Learn about Prepared Statements with parameterized queries.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
  • @HQweb Glad that I could help – GrumpyCrouton Jul 01 '20 at 11:52
  • I added more columns on my table and I tried to insert data on the table and I got query failed, so I tried to troubleshoot and I got this: postgres=# insert into cursosprogramas (departamento,codigocurso,curso,codigo,disciplina,legenda,ano,r,t,tp,pl,ts,total,ms,diurno,contacto,pos_laboral,contacto2,proc_por)values (100,2121,Medicina,5235,sgsgs, s,1º,1º,4,4,4,4,4, 4,5,646464,5,6363, 2),(100,2121,Medicina,5235,sgsgs, s,1º,1º,4,4,4,4,4, 4,4,646464,5,6363, 2); ERROR: syntax error at or near "§"LINE 1: ...roc_por) values (100,2121,Medicina,5235,sgsgs, s,1§,1§,4,4,4... – HQweb Jul 01 '20 at 12:53
  • Those are the fields on my DB departamento int codigocurso int curso text codigo int disciplina text legenda text ano text r text From column t to column proc_por they are integer – HQweb Jul 01 '20 at 12:53
  • @HQweb Try my update, I removed the single quotes without thinking about it. – GrumpyCrouton Jul 01 '20 at 12:54
  • Sorry, where did you remove it? – HQweb Jul 01 '20 at 13:00
  • @HQweb Before my update, I didn't have single quotes around the variables in `$data`, these are needed because some of your variables are strings and not just numbers. – GrumpyCrouton Jul 01 '20 at 13:12
  • it still the samepostgres=# insert into cursosprogramas (departamento, codigocurso, curso, codigo, disciplina, legenda, ano, r, t, tp, pl, ts, total, ms, diurno, contacto, pos_laboral, contacto2, proc_por) values (100,2121,Medicina,5235,sgsgs,s,1º,1º,4,4,4,4,4,4,5,646464,5,6363,2),(100,2121,Medicina,5235,sgsgs, s,1º,1º,4,4,4,4,4, 4,4,646464,5,6363, 2); ERROR: syntax error at or near "§" LINE 1: ...roc_por) values (100,2121,Medicina,5235,sgsgs, s,1§,1§,4,4,4... – HQweb Jul 01 '20 at 13:20
  • foreach ($diurno as $userId) { $data .= '('.$id.','.$grdid.','.$curso.','.$codigo.','.$disciplina.', '.$legenda.','.$ano.','.$r.','.$t.','.$tp.','.$pl.','.$ts.','.$total.', '.$ms.','.$userId.','.$contacto.','.$pos_laboral.','.$contacto2.', '.$idd.'),'; } like this? – HQweb Jul 01 '20 at 13:21
  • I've made some changes on my form, I decided that the pos_laboral will also be multiple insert on my form. How can I implement those changes on the foreach? Because I just noticed that I can't do like: foreach ($diurno as $userId and $pos_laboral as $userId2). How is it supposed to be? – HQweb Jul 06 '20 at 11:02
  • @HQweb Just do the other loop separately. – GrumpyCrouton Jul 06 '20 at 11:37
  • i did it like this foreach ($pos_laboral as $userIdd) but it is saying undefined variable userIdd – HQweb Jul 06 '20 at 11:51
  • 1
    @HQweb Shouldn't it just be `$userId`? – GrumpyCrouton Jul 06 '20 at 11:51
  • Well, it can be another name – HQweb Jul 06 '20 at 11:53
  • foreach ($diurno as $userId) { $data .= "('$id', '$grdid', '$userId', '$contacto', '$pos_laboral', '$contacto2', '$idd'),"; //the pos_lavoral on the first foreach will give a error like: array to string conversation // and if I change to new it will be undefined variable new. } foreach ($pos_laboral as $new) { $data .= "('$id', '$grdid', '$userId', '$contacto', '$new, '$contacto2', '$idd'),"; } $data = rtrim($data, ','); $sql = "insert into cursosprogramas (departamento, codigocurso, diurno, contacto, pos_laboral, contacto2, proc_por) values {$data};"; echo $sql; – HQweb Jul 06 '20 at 12:08
  • Sorry, you're probably going to need to post a new question. – GrumpyCrouton Jul 06 '20 at 12:08
  • Okay @GrumpyCrouton – HQweb Jul 06 '20 at 12:13
1

You need a comma between the value tuples. Try a comma at the end of line two like this:

foreach ($diurno as $userId) {
$data .= "(".$id.",".$grdid.",".$userId.",".$contacto.",".$pos_laboral.",".$contacto2.",".$idd."),";
}

$data = rtrim($data, ',');
$sql = "insert into cursosprogramas (departamento, codigocurso, diurno, contacto, pos_laboral, contacto2, proc_por) values (".$data.");";
echo $sql;
Alex Funk
  • 346
  • 2
  • 7