При заполнении только поля title вылезает ошибка Невозможно вставить данные в базу: 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 ' '', '', '')' at line 1
index.php
<?php
include "config.php";
include "product.php";
if(isset($_COOKIE['img'])){
echo $_COOKIE['img'];
}
if(isset($_COOKIE['result'])){
echo $_COOKIE['result'];
}
?>
<form method="POST" enctype="multipart/form-data" action="add.php">
<p><label for="title">Название товара</label><br>
<input type="text" id="title" name="title">
</p>
<p><label for="price">Цена товара</label><br>
<input type="number" id="price" name="price">
</p>
<p><label for="description">Описание товара</label><br>
<textarea name="description" id="description" cols="30" rows="3"></textarea>
</p>
<p><label for="category">Категория товара</label><br>
<input type="text" id="category" name="category">
</p>
<p><label for="image">Картинка товара</label><br>
<input id="image" type="file" name="image">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
</p>
<input type="submit" value="Добавить товар">
</form>
add.php
<?php
include "config.php";
function is_valid_type($filename){
$valid_types = array("jpeg", "png", "jpg", "svg");
$file_type = end(explode(".", $filename));
if (in_array($file_type, $valid_types)){
return true;
}
else{
return false;
}
}
if(isset($_POST['title'])){
include "product.php";
$new_product = new product;
$new_product->title = $_POST['title'];
$new_product->price = (isset($_POST['price'])?$_POST['price']:0);
$new_product->description = (isset($_POST['description'])?$_POST['description']:"");
$new_product->category = (isset($_POST['category'])?$_POST['category']:"");
if(isset($_FILES['image'])){
$image = $_FILES['image'];
if(is_valid_type($image['name'])){
$TARGET_PATH .=$image['name'];
if(move_uploaded_file($image['tmp_name'], $TARGET_PATH)){
$new_product->img = $TARGET_PATH;
setcookie("img", "Картинка успешно загружена", time()+10);
// header("Location: index.php");
}
else{
setcookie("img", "Картинка не загружена", time()+10);
setcookie("result", "", time()-10);
}
}
else{
setcookie("img", "Картинка не подходит", time()+10);
setcookie("result", "", time()-10);
}
}
else{
setcookie("img", "Картинка не загружена", time()+10);
$new_product->img = "";
// $new_product->add_product($link, $new_product);
// header("Location:index.php");
}
$new_product->add_product($link, $new_product);
header("Location:index.php");
}
?>
product.php
<?
include "db.php";
include "config.php";
class product{
// public $id, $title, $price, $description, $category, $img;
public function add_product($link,$new_product){
$sql = "INSERT INTO products (title, price, description, category, img) VALUES ('$new_product->title', $new_product->price, '$new_product->description', '$new_product->category', '$new_product->img')";
$result = mysqli_query($link, $sql) or die ("Невозможно вставить данные в базу: " . mysqli_error($link));
if($result){
setcookie("result", "Товар успешно добавлен", time()+10);
}
else if(!$result){
setcookie("result", "Товар не добавлен", time()+10);
}
}
}
?>
config.php
<?php
session_start();
// Путь к папке с каринками
$TARGET_PATH = "images/products/";
global $TARGET_PATH;
?>