I want to insert an image into a database. I am working on localhost ( with XAMPP ) . I know it's a bad practice to store images into databases instead of uploading them, but i have to do it this way. This is my html form:
<form action="addItem.php" method="post" enctype="multipart/form-data" >
<h1> Add item </h1>
<p>
<label for="a">Name:</label>
<input id="a" type="text" name="name" required />
</p>
<p>
<label for="b">Condition:</label>
<select id="b" name="condition" type="text" required />
<option value="Nou">Nou </option>
<option value="Folosit">Folosit </option>
</select>
</p>
<p>
<label for="c">End date: </label>
<input id="c" type="text" name="end_date" required placeholder="Ex. 2018.06.13 23:59:00"/>
</p>
<p>
<label for="e">Category:</label>
<select id="e" name="category" required >
<option value="mobile phones">Mobile Phones</option>
<option value="cars">Cars</option>
<option value="houses">Houses</option>
<option value="tv">TV</option>
<option value="PC">PC</option>
<option value="clothes">Clothes</option>
</select>
</p>
<p>
<label for="f"> Starting Price:</label>
<input id="f" type="text" name="startingPrice" required min="0" />
</p>
<p>
<label for="g"> Pictures: </label>
<input id="g" type="file" accept="image/*" name="image" />
</p>
<p>
<label for="h">Description: </label>
<textarea id="h" rows="5" cols="40" name="description">
</textarea>
</p>
<input type ="submit" value="Add item" name="submit" />
</form>
This is the php code :
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("INSERT INTO products (product_name, product_description, seller_id, category_id, image, grad_uzura)
VALUES (?,?,?,?,?,?)");
$stmt->bind_param("ssiibs", $product_name, $product_description,
$seller_id_products, $category_id, $image, $condition);
// insert into products
$condition = $_POST['condition'];
$target = "img\\".basename($_FILES['image']['name']);
$image = file_get_contents($_FILES['image']['tmp_name']);
echo $image;
move_uploaded_file($_FILES['image']['tmp_name'],$target);
//$imagegetmp = addslashes (file_get_contents($_FILES['pictures']));
$product_name =$_POST['name'];
echo "product name :".$product_name."<br>";
$product_description = $_POST['description'];
echo "product desc:".$product_description."<br>";
$afisare=$_SESSION['utilizator'];
echo "username :".$afisare."<br>";
$sql = "SELECT user_id FROM users WHERE username ='$afisare' "; //extrag user id din sesiune
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
echo "user_id :".$row['user_id']."<br>";
$seller_id_products= $row['user_id'];
$category_name = $_POST['category']; //extrag id-ul categoriei
echo "category_name :".$category_name."<br>";
$sql = "SELECT category_id FROM categories WHERE category_name = '$category_name'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$category_id= $row['category_id'];
echo "category id :".$category_id."<br>";
$stmt->execute();
I test to see if the image is extracted from the form with echo $image and it works. But in phpmyadmin i don't have the image inserted. All the other fields in my table have the right data in them, except for the image, so i don't think my problem is with the prepared statement. I also tried to modify the max size for files in the Apache config. I don't know what i'm doing wrong.