2016-12-13 3 views
0

Ich bin eine dynamische Abfrage zu bauen und ich habe eine harte Zeit mit diesem Fehlercode herauszufinden:PHP MySQL Dynamische Abfragefehler

Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''image','brand','model','type','c-type','poe','specs','lens','price') VALUES('un' at line 1

Hier ist meine PHP:

<?php 
include("config.php"); 

$cameraCol = array("'image'", "'brand'", "'model'", "'type'", "'c-type'", "'poe'", "'specs'", "'lens'", "'price'"); 
$accessoriesCol = array("image", "brand", "model", "price"); 
$cableCol = array("image", "brand", "model", "type", "price"); 
$powerCol = array("image", "brand", "model", "channels", "price"); 
$recorderCol = array("image", "brand", "model", "type", "poe", "specs", "ports", "price"); 
$routerCol = array("image", "brand", "model", "wireless-capability", "price"); 
$switchCol = array("image", "brand", "model", "ports", "price"); 
$wirelessCol = array("image", "brand", "model", "frequency", "speed", "price"); 

$category = $_POST['category']; 

if($category == "camera"){ 
    $cameraImp = implode(",", $cameraCol); 
    $cameraVal = array("'".$_POST['image']."'", "'".$_POST['brand']."'", "'".$_POST['model']."'", "'".$_POST['type']."'", "'".$_POST['c-type']."'", 1, "'".$_POST['specs']."'", "'".$_POST['lens']."'", $_POST['price']); 
    $cameraVal = implode(",", $cameraVal); 
    insertItem($cameraImp, $category, $cameraVal); 

} 

function insertItem($columns, $category, $cameraValArr){ 
    include("config.php"); 
    $sql2 = "INSERT INTO ". $category ."(". $columns .") VALUES(". $cameraValArr .")"; 
    $result2 = mysqli_query($conn, $sql2); 
    if(!$result2){ 
     die("Error: " . mysqli_error($conn)); 
    } 
} 
?> 

Dies ist offensichtlich ein Fehler in meiner Anfrage und ich bin mir ziemlich sicher, dass ich etwas verpasst habe. Jede Hilfe würde sehr geschätzt werden!

+0

Drucken Sie die Abfrage nach der Variablensubstitution aus. In 95% der Fälle ist der Fehler offensichtlich. –

+0

Gute Idee, warum habe ich nicht daran gedacht. –

+0

Ausgabe: INSERT INTO Kamera ('Bild', 'Marke', 'Modell', 'Typ', 'C-Typ', 'Poe', 'Specs', 'Objektiv', 'Preis') WERTE ('unbenannt. png ',' asdasd ',' dsad ',' Kugel ',' IP ', 1,' asdasd ',' 2.8mm ', 2000) Ich sehe nichts falsches: S –

Antwort

0

Lösung: entfernt den Bindestrich von einem Spaltennamen in meiner Datenbank speziell die "C-type" Spalte.