每次语法错误均为NULL时,都会出错。
您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以在第5行的'60',NULL,'1','1')'附近使用正确的语法
这是我的代码:
$sql = "INSERT INTO `productos` (`nombre`, `precio`, `cantidad`, `imagen1`, `imagen2`, `imagen3`,
`imagen4`, `imagen5`, `imagen6`, `categoria`, `subcategoria`, `relacion`, `alto`, `largo`, `ancho`,
`peso`, `eliminar`, `esconder`, `vender`) VALUES
('".$_POST["nombre"]."', '".$_POST["precio"]."', '".$_POST["cantidad"]."'";
for ($x = 0; $x < 6; $x++) {
if ($x < count($img)) {
$sql .= ", '".$img[$x]."'";
} else {
$sql .= ", NULL";
}
}
foreach ($_POST["subcat"] as $val) {
if ($val != "0") {
$subcattmp[] = $val;
}
}/*VERIFICO QUE LA SUBCATEGORIAS SEAN DIFERENTE DE 0*/
if (isset($subcattmp) && count($subcattmp) > 0) {
for ($x = 0; $x < count($subcattmp); $x++) {
$subcat = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM subcategorias WHERE id='".$subcattmp[$x]."'"));
$categorias[] = $subcat["categoria"];
$subcategorias[] = $subcat["id"];
$relaciones[] = $subcat["relacion"];
}
for ($x = 0; $x < count($categorias); $x++) {
if ($x == 0) {
$categoria = $categorias[$x];
} else {
$categoria .= "-".$categorias[$x];
}
}
for ($x = 0; $x < count($subcategorias); $x++) {
if ($x == 0) {
$subcategoria = $subcategorias[$x];
} else {
$subcategoria .= "-".$subcategorias[$x];
}
}
for ($x = 0; $x < count($relaciones); $x++) {
if ($x == 0) {
$relacion = $relaciones[$x];
} else {
if ($relacion != $relaciones[$x]) {
borrar_img($img);
llamar("La relacion entre ambas Subcategorias no existe", "self");
}
}
}
$sql .= ", '".$categoria."', '".$subcategoria."', '".$relacion."', '".$_POST["alto"]."', '".$_POST["ancho"]."', '".$_POST["largo"].", '".$_POST["peso"]."'";
if ($_POST["eliminar"] == 0) {
$sql .= ", NULL";
} else {
$sql .= ", '1'";
}
if ($_POST["esconder"] == 0) {
$sql .= ", NULL";
} else {
$sql .= ", '1'";
}
if ($_POST["vender"] == 0) {
$sql .= ", NULL";
} else {
$sql .= ", '1'";
}
$sql .= ");";
echo $sql."<br>";
if (godb($sql)) {
llamar("Producto Agregado", "self");
} else {
borrar_img($img);
llamar("Error al guardar en DB /ANULADO", "self");
}/*END*/
//for($x=0;$x<30;$x++){godb($sql);}
} else {
borrar_img($img);
llamar("No escogio ninguna subcategoria", "self");
}
如果我对$ sql值进行回显,则会得到以下信息:
INSERT INTO `productos` (`nombre`, `precio`, `cantidad`, `imagen1`, `imagen2`, `imagen3`, `imagen4`, `imagen5`, `imagen6`, `categoria`, `subcategoria`, `relacion`, `alto`, `largo`, `ancho`, `peso`, `eliminar`, `esconder`, `vender`) VALUES ('prueba20', '10', '20', '20190515114608fondo.jpg', NULL, NULL, NULL, NULL, NULL, '2-3', '2-3', '1', '40', '30', '50, '60', NULL, '1', '1');