Я строю динамический запрос и я с трудом выяснить этот код ошибки:PHP MySQL Динамическая ошибка запроса
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
Вот мой 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));
}
}
?>
Это, очевидно, ошибка в моем запросе, и я уверен, что я что-то пропустил. Любая помощь будет принята с благодарностью!
Распечатайте запрос после замены переменных. 95% времени, ошибка очевидна. –
Хорошая идея, почему я не подумал об этом. –
Выход: INSERT INTO камера («изображение», «бренд», «модель», «тип», «c-тип», «poe», «specs», «объектив», «цена»). VALUES ('unnamed. png ',' asdasd ',' dsad ',' bullet ',' IP ', 1,' asdasd ',' 2.8mm ', 2000) Я не вижу ничего плохого: S –