Php 在MySQL数据库表的一列中存储多个映像路径
我可以将多个图像上载到上载文件夹及其到数据库的路径,但我的代码是在数据库中为每个具有新id的图像创建新行(我不想要)。我想用相同的id和逗号分隔将所有图像路径存储到数据库中的一列 以下是我的php代码:Php 在MySQL数据库表的一列中存储多个映像路径,php,mysql,Php,Mysql,我可以将多个图像上载到上载文件夹及其到数据库的路径,但我的代码是在数据库中为每个具有新id的图像创建新行(我不想要)。我想用相同的id和逗号分隔将所有图像路径存储到数据库中的一列 以下是我的php代码: <?php include('connection.php'); foreach($_FILES['files']['name'] as $i => $name) { $name = $_FILES['files']['name'][$i];
<?php
include('connection.php');
foreach($_FILES['files']['name'] as $i => $name) {
$name = $_FILES['files']['name'][$i];
$size = $_FILES['files']['size'][$i];
$type = $_FILES['files']['type'][$i];
$tmp = $_FILES['files']['tmp_name'][$i];
$explode = explode('.', $name);
$ext = end($explode);
$path = 'uploads/';
$path = $path . basename( $explode[0] . time() .'.'. $ext);
$sql="INSERT into pictures (FILE_NAME, FILE_SIZE, FILE_TYPE )
VALUES('$name','$size','$type'); ";
mysqli_query($conn, $sql);
$errors = array();
if(empty($_FILES['files']['tmp_name'][$i])) {
$errors[] = 'Please choose at least 1 file to be uploaded.';
}else {
$allowed = array('jpg','jpeg','gif','bmp','png');
$max_size = 2000000; // 2MB
if(in_array($ext, $allowed) === false) {
$errors[] = 'The file <b>'.$name.'</b> extension is not allowed.';
}
if($size > $max_size) {
$errors[] = 'The file <b>'.$name.'</b> size is too hight.';
}
}
if(empty($errors)) {
if(!file_exists('uploads')) {
mkdir('uploads', 0777);
}
if(move_uploaded_file($tmp, $path)) {
echo '<p>The file <b>'.$name.'</b> successful upload</p>';
}else {
echo 'Something went wrong while uploading
<b>'.$name.'</b>';
}
}else {
foreach($errors as $error) {
echo '<p>'.$error.'<p>';
}
}
}
?>
只需改进代码,并将插入查询置于foreach循环之外
<?php
include('connection.php');
$files = [];
foreach($_FILES['files']['name'] as $i => $name) {
$name = $_FILES['files']['name'][$i];
$size = $_FILES['files']['size'][$i];
$type = $_FILES['files']['type'][$i];
$tmp = $_FILES['files']['tmp_name'][$i];
$explode = explode('.', $name);
$ext = end($explode);
$updatdName = $explode[0] . time() .'.'. $ext;
$path = 'uploads/';
$path = $path . basename( $updatdName );
if(empty($_FILES['files']['tmp_name'][$i])) {
$errors[] = 'Please choose at least 1 file to be uploaded.';
}else {
$allowed = array('jpg','jpeg','gif','bmp','png');
$max_size = 2000000; // 2MB
if(in_array($ext, $allowed) === false) {
$errors[] = 'The file <b>'.$name.'</b> extension is not allowed.';
}
if($size > $max_size) {
$errors[] = 'The file <b>'.$name.'</b> size is too hight.';
}
}
if(empty($errors)) {
// if there is no error then set values
$files['file_name'][] = $updatdName;
$files['size'][] = $size;
$files['type'][] = $type;
$errors = array();
if(!file_exists('uploads')) {
mkdir('uploads', 0777);
}
if(move_uploaded_file($tmp, $path)) {
echo '<p>The file <b>'.$name.'</b> successful upload</p>';
}else {
echo 'Something went wrong while uploading
<b>'.$name.'</b>';
}
}else {
foreach($errors as $error) {
echo '<p>'.$error.'<p>';
}
}
}
if(!empty($files)) {
$files['file_name'][] = $updatdName;
$files['size'][] = $size;
$files['type'][] = $type;
$names = implode(',', $files['file_name']);
$sizes = implode(',', $files['size']);
$types = implode(',', $files['type']);
$sql="INSERT into pictures (FILE_NAME, FILE_SIZE, FILE_TYPE )
VALUES('$names','$sizes','$types'); ";
mysqli_query($conn, $sql);
}
您在哪里插入路径,我在您的query@Narayan:由于图像的原因,我批准了您的编辑建议,但我认为没有必要再次缩进代码。“但我的代码正在数据库中为每个具有新id的图像创建新行(我不想要)”-您应该想要这个;这被称为适当的规范化。逗号分隔的值迟早会给您带来麻烦。