Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
There is issue with inserting product_image to database. I've tried a lot but couldn't find out what is the problem

What I have tried:

<!DOCTYPE html>
<?php
include("includes/db.php");
?>

<html>
<head>
  <script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
  <script>tinymce.init({ selector:'textarea' });</script>

	<title>Inserting new product</title>

</head>
<body bgcolor="orange">
<form action="insert_product.php" method="post" enctype="multipart/form.data">
<table align="center" width="700" border="2" bgcolor="white">

<tr align="center">
	<td colspan="7"><h2 align="center">Insert new product here</h2></td>
</tr>

<tr>
	<td align="right">Product title:</td>
	<td><input type="text" name="product_title" size="60" /></td>

</tr>
<tr>
	<td align="right">Product Category:</td>
	<td><select name="product_cat">
		<option>Select a category</option>

		<?php

	$get_cats = "select * from categories";
	$run_cats= mysqli_query($con,$get_cats);

	while ($row_cats=mysqli_fetch_array($run_cats)) {

		$cat_id = $row_cats['cat_id'];
		$cat_title = $row_cats['cat_title'];
		echo "<option value='$cat_id'>$cat_title</option>";
	}
	?>
	</select>
	</td>

</tr>
<tr>
	<td align="right">Product Brand:</td>
	<td><select name="product_brand">
<option>Select a Brand</option>
<?php

    $get_brands = "select * from brands";
	$run_brands= mysqli_query($con,$get_brands);

	 while ($row_brands=mysqli_fetch_array($run_brands)) {

	  $brand_id = $row_brands['brand_id'];
	  $brand_title = $row_brands['brand_title'];
	  echo "<option value='$brand_id'>$brand_title</option>";
	}
?>
</select>


	</td>

</tr>
<tr>
	<td align="right">Product image:</td>
	<td><input type="file" name="product_image"/></td>

</tr>
<tr>
	<td align="right">Product price:</td>
	<td><input type="text" name="product_price"/></td>

</tr>
<tr>
	<td align="right">Product Description:</td>
	<td><textarea name="product_desc" cols="20" rows="10"></textarea></td>

</tr>
<tr>
	<td align="right">Product Keywords:</td>
	<td><input type="text" name="product_keywords" size="50" /></td>

</tr>
<tr align="center">
		<td colspan="7"><input type="Submit" name="insert_post" value="Insert now" /></td>

</tr>
</table>

</body>
</html>

<?php
if(isset($_POST['insert_post'])){

//getting the text data from the fields
$product_title = $_POST['product_title'];
$product_cat = $_POST['product_cat'];
$product_brand = $_POST['product_brand'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_keywords = $_POST['product_keywords'];
 // getting the image from the fields
$product_image=$_FILES['product_image']['name'];
$product_image_tmp=$_FILES['product_image']['tmp_name'];

echo $insert_product = "INSERT INTO products(product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keywords) values('$product_cat','$product_brand','$product_title','$product_price','$product_desc','$product_image','product_keywords')";

}
Posted
Updated 14-Jun-20 8:07am
v5
Comments
Richard MacCutchan 23-Sep-17 3:57am    
Which is line 110 and what is the value of the array objects being referred to? Not also the missing $ sign in your use of 'product_keywords' in your value list.
ALEX8998 23-Sep-17 4:05am    
110 is line of $product_image
Richard MacCutchan 23-Sep-17 4:14am    
The error message is telling you that the index value 'product_image' is not valid. So use your debugger to find out why.

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
The error is clear the index 'product_image' does not exists in the array $_FILES...
Why? The name does not exist in your HTML? You not actually uploaded any file?
The actual answer is in your hand - use the debugger to find it...
 
Share this answer
 
I think it is not enctype="multipart/form.data"

it is enctype="multipart/form-data"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900