This is my form code to add new product to my table 'items' in database.
<form method="post" action="additem.php">
Product name: <input method="text" name="itemName" class="field">
Size: <select name="sizes" class="field">
<option value="XS">XS</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
Gender: <select name="gender" class='field'>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Kids">Kids clothing</option>
</select>
Price: <input method="text" name="price" class="field">
Image: 'Your code here'
<input type="submit" class="btn">
</form>
Php code to insert data from form to table 'items'. User enters all properties of item to be added and should upload image file. When user clicks submit button the php code will process.
<?php
$link = mysqli_connect('localhost', 'root', '', 'onlinestorebd');
$itemName=mysqli_real_escape_string($link, $_POST['itemName']);
$itemSize=mysqli_real_escape_string($link,$_POST['sizes']);
$price=mysqli_real_escape_string($link, $_POST['price']);
$gender=mysqli_real_escape_string($link, $_POST['gender']);
$itemPhoto=mysqli_real_escape_string($link,$_POST['itemPhoto']);
$mysqli = new mysqli("localhost", "root", "", "onlinestorebd");
$qq="INSERT INTO items (itemName, sizes, gender, price, itemPhoto) VALUES ('".$itemName."','".$itemSize."', '".$gender."', '".$price."','".$itemPhoto."')";
$result=$mysqli->query($qq);
mysqli_close($mysqli);
IF(!$result){
echo "Data insert failed <br><br>";
echo mysql_error();
} ELSE {
echo "Item has been added";
echo "<br><br><a href=\"editdb.php\">Back to products list</a>";
}
?>
Firstly you have to add a parameter for image with form element.
Your Code
<form method="post" action="additem.php">
New Code
<form method="post" action="additem.php" enctype="multipart/form-data">
Then check your php.ini file
file_uploads = On
And last step
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>