[英]Uploading multiple images php, mysql
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. 我想上传一个项目,并附有该项目的多个图像,这些图像应存储在mysql数据库中,以及一个文件夹“ uploads”。 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.
我有2表图像和属性与一对多的关系。
<?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. 的形式,它的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.