我在插入mysql时遇到问题。 此功能应将图像传输到图像文件夹,并为每个图像创建一个新查询,这样如果有3个图像,则执行3个查询,但只执行第一个
public function insertNews($author, $subject, $content)
{
if($this->databaseConnection())
{
// * So this is initalizing our database, now all we have to do is create a simple query.
$unique_id = bin2hex(random_bytes(3));
$insert_news = $this->db_connection->prepare("INSERT INTO news (author, subject, content, time, uniqueid) VALUES (:author, :subject, :content, NOW(), :id)");
$insert_news->bindValue(':author', $author, PDO::PARAM_STR);
$insert_news->bindValue(':subject', $subject, PDO::PARAM_STR);
$insert_news->bindValue(':content', $content, PDO::PARAM_STR);
$insert_news->bindValue(':id', $unique_id, PDO::PARAM_STR);
$insert_news->execute();
// * Checking if any images are waiting to be uploaded
if(isset($_FILES['images']) && !empty($_FILES['images']['name']))
{
$extension = array("jpeg", "jpg", "png", "gif");
$insert_images = $this->db_connection->prepare("INSERT INTO news_images (news_id, location) VALUES (:id, :location)");
foreach($_FILES['images']['tmp_name'] as $key => $tmp_name)
{
$file_name = $_FILES["images"]["name"][$key];
$file_tmp = $_FILES["images"]["tmp_name"][$key];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$tname = bin2hex(random_bytes(10));
$temp = explode(".", $_FILES['images']['name']);
$newfilename = $tname . '_news.' . $ext;
$location = "images/".$newfilename;
if(in_array($ext, $extension))
{
if(!file_exists($location))
{
// * Now we have to upload it to the database
$insert_images->bindValue(':id', $unique_id, PDO::PARAM_STR);
$insert_images->bindValue(':location', $location, PDO::PARAM_STR);
$insert_images->execute();
// * moving file
move_uploaded_file($file_tmp = $_FILES["images"]["tmp_name"][$key], $location);
}
}
}
}
header('Location: news.php');
exit();
}
}