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.

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

16 Comments

@HQweb Glad that I could help
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...
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 Try my update, I removed the single quotes without thinking about it.
@HQweb Shouldn't it just be $userId?
|
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;

1 Comment

This answer currently fails if any of the values are strings with a space in it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.