"You have an error in your SQL syntax" with array

Asked

Viewed 61 times

0

personal I’m having a problem this error keeps appearing 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 '','daniel')' at line 1

<?php

$codigo = array_filter($_POST['codigo']);

foreach ($codigo as $key => $value) {
    $codigo = $value;
 $nome_pedido = $_POST["nome"][0];
    $pedido = $_POST["pedido"][$key];

echo $codigo."<br>";
    echo $nome_pedido."<br>";
    echo $pedido."<br>";



    $sql = "INSERT INTO pedidos VALUES";
    $sql .="(NULL,'$codigo',$nome_pedido','$pedido')";
     var_dump($sql);


    if ($conn1 ->query($sql) === true){

echo "    <div id=\"sucess-forn\" class=\"alert alert-success\" role=\"alert\" style=\"position: absolute; \n";
echo "top: 0; \n";
echo "left: 0; \n";
echo "z-index: 10; \n";
echo "padding:5px; \n";
echo "width:99%;\n";
echo " position: fixed; \">\n";
echo "<a class=\"close\" data-dismiss=\"alert\" href=\"#\">×</a>";

echo "        <center><h1 class=\"alert-heading\">Fornecedor cadastrado com sucesso!</h1></center>\n";
echo "</div>\n";


    } else {
        echo "erro:". $sql . "<br>" . $conn1->error;
    }

}


$conn1->close();






?>

someone could help me solve this problem and explain to me what is happening?

2 answers

2

A single quotation mark is missing in your $promptname variable of your sql:

$sql .="(NULL,'$codigo','$nome_pedido','$pedido')";

I hope I’ve helped.

1


Dude, I think it’s all about the bad formatting of your code, and I saw some mistakes, and I’m sending you here. I believe the biggest problem was in its variable $sql because your query was not recognizing the values because it is separated into 2 parts, which is not necessary, it can be placed in only 1 line, even more by the size of this one. Test around to see if it will solve your problem, I could not test here because I do not have access to your HTML code and the like.

<?php

$codigo = array_filter($_POST['codigo']);

foreach ($codigo as $key => $value) {
    $codigo = $value;
    $nome_pedido = $_POST["nome"][0];
    $pedido = $_POST["pedido"][$key];

    echo $codigo."<br>";
    echo $nome_pedido."<br>";
    echo $pedido."<br>";



    $sql = "INSERT INTO pedidos VALUES (NULL, '$codigo', '$nome_pedido', '$pedido')";
    var_dump($sql);


    if ($conn1->query($sql) === true) {
        echo "    <div id=\"sucess-forn\" class=\"alert alert-success\" role=\"alert\" style=\"position: absolute; \n";
        echo "top: 0; \n";
        echo "left: 0; \n";
        echo "z-index: 10; \n";
        echo "padding:5px; \n";
        echo "width:99%;\n";
        echo " position: fixed; \">\n";
        echo "<a class=\"close\" data-dismiss=\"alert\" href=\"#\">×</a>";

        echo "        <center><h1 class=\"alert-heading\">Fornecedor cadastrado com sucesso!</h1></center>\n";
        echo "</div>\n";
    } else {
        echo "erro:". $sql . "<br>" . $conn1->error;
    }
}

$conn1->close();
?>
  • solved the problem thanks, but explain me it was just a matter of ma formatting or had some more error?

  • 1

    So man, in my IDE when I played your code already appeared that from the NULLof his $sql he was no longer recognizing, until pq tbm in his variable $nome_pedido was missing a ' before this. You can concatenate the query but you don’t need to declare it twice and you don’t need to use the .=, can just skip the line and use the .. On the line $conn1->query($sql was with the -> Separately, I don’t know if it impacted anything, but by good practices it is good to leave in the correct syntax. If possible, mark the answer as correct and click the up arrow rs.

  • I got it, thank you so much for actually I was going crazy

Browser other questions tagged

You are not signed in. Login or sign up in order to post.