I would like to upload an item, accompanied with multiple images of the item which should be stored in a mysql database and a folder "uploads" as well. The code for inserting the item works well but the code for uploading the image does not execute. I have 2 tables images and properties with a one to many relationship.
<?php
require('DBconnect.php');
if(isset($_POST['submit']))
{
// removes backslashes
$title = stripslashes($_REQUEST['title']);
$title = mysqli_real_escape_string($connection,$title);
$area = stripslashes($_REQUEST['area']);
$area = mysqli_real_escape_string($connection,$area);
$price = stripslashes($_REQUEST['price']);
$price = mysqli_real_escape_string($connection,$price);
$query = "INSERT into `properties` (propertyTitle, propertyArea, propertyPrice)
VALUES ('$title', '$area', '$price')";
$result = mysqli_query($connection,$query) or die(mysqli_error($connection));
$prpId= mysqli_insert_id($connection);
if($result){
echo "Property registered successfully";
$targetFolder = "uploads";
//the code below is not executed by the server. Reasons for which i have no clue
foreach($_FILES as $file => $fileArray)
{
if(!empty($fileArray['name']) && $fileArray['error'] == 0)
{
$getFileExtension = pathinfo($fileArray['name'], PATHINFO_EXTENSION);;
if(($getFileExtension =='jpg') || ($getFileExtension =='jpeg') || ($getFileExtension =='png') || ($getFileExtension =='gif'))
{
if ($fileArray["size"] <= 5000000)
{
$breakImgName = explode(".",$fileArray['name']);
$imageOldNameWithOutExt = $breakImgName[0];
$imageOldExt = $breakImgName[1];
$newFileName = strtotime("now")."-".str_replace(" ","-",strtolower($imageOldNameWithOutExt)).".".$imageOldExt;
$targetPath = $targetFolder."/".$newFileName;
if (move_uploaded_file($fileArray["tmp_name"], $targetPath))
{
$qry = "INSERT INTO images (image, propertyId)
VALUES ('".$newFileName."', '".$prpId."')";
$rs = mysqli_query($connection, $qry);
}
}
}
}
}
}
?>
I added
enctype="multipart/form-data"
to the form and it worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.