php - 在数据库中插入带有 2 个图像字段的表单数据

标签 php mysql forms file-upload image-uploading

我对此进行了搜索,但找不到任何明确的内容。 我有一个带有 2 个输入文件字段的表单,我需要插入所有数据。 我不想进行多文件上传,我需要将每个输入文件分开。 我喜欢上面的代码,因为它重命名了每个文件。 我已经尝试过了

`$host="hst";
$databasename="nme";
$user="usr";
$pass="pwd";
/**********MYSQL Settings****************/


$conn=mysql_connect($host,$user,$pass);

if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
die('Not connected : ' . mysql_error());
}
$name     = $_POST["name"] ;
//email:
 $email = $_POST["email"] ;
 //description:
 $description        = $_POST["description"] ;
 //partie:
  $partie       = $_POST["partie"] ;

function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
    switch($imagetype)
   {
        case 'image/bmp': return '.bmp';
       case 'image/gif': return '.gif';
       case 'image/jpeg': return '.jpg';
       case 'image/png': return '.png';
       default: return false;
   }
 }

if (!empty($_FILES["image"]["name"])) {

$file_name=$_FILES["image"]["name"];
$temp_name=$_FILES["image"]["tmp_name"];
$imgtype=$_FILES["image"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "images/assets/syltattoo/Emails/".$imagename;
    if (!empty($_FILES["image2"]["name"])) {

$file_name2=$_FILES["image2"]["name"];
$temp_name2=$_FILES["image2"]["tmp_name2"];
$imgtype2=$_FILES["image2"]["type2"];
$ext= GetImageExtension($imgtype);
$imagename2=date("d-m-Y")."-".time().$ext;
$target_path2 = "images/assets/syltattoo/Emails/".$imagename2;

if(move_uploaded_file($temp_name, $target_path)) {
$query_upload="INSERT INTO `modx_demandes`(image, image2, name, email, description, partie) VALUES 

('".$target_path."','".$target_path2."','$name', '$email', '$description', '$partie')";
mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());
}else{

 exit("Error While uploading image on the server");
} 

}}`

这是表格:

<form action="demandes-de-tatouage.html" method="post" enctype="multipart/form-data">

<div id="name"><label for="name">Nom:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="name" value="" /></div>
<div id="name"><label for="name">Email:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="email" value="" /></div>

<div id="description"><label for="description">Description:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="description" value="" /></div>
<div id="name"><label for="name">Partie du corps:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="partie" value="" /></div>
<div>
<label for="image">Envoyez vos images</label></div>
<input id="image" name="image" type="file" value="" maxlength="100000" /> 
<input id="image2" name="image2" type="file" value="" maxlength="100000" /> 

<input class="btn btn-primary" type="submit" value="Envoyer" /></form>

我尝试集成第二个 image2 字段,但它在数据库中为 image1 和 image2 保存了相同的结果。有人可以帮助我使用这个脚本制作一些可以与多个输入字段图像一起使用的东西吗?

最佳答案

您在脚本中犯了一些拼写错误(您从 $imgtype 获得了相同的扩展名),并且在将它们插入数据库时​​没有使用原始文件名。此外,您没有将第二个文件保存在任何地方。此代码适合您吗?

<?php
$host = "hst";
$databasename = "nme";
$user = "usr";
$pass = "pwd";
/**********MYSQL Settings****************/


$conn = mysql_connect($host, $user, $pass);

if ($conn) {
    $db_selected = mysql_select_db($databasename, $conn);
    if (!$db_selected) {
        die ('Can\'t use foo : ' . mysql_error());
    }
} else {
    die('Not connected : ' . mysql_error());
}
$name = $_POST["name"];
//email:
$email = $_POST["email"];
//description:
$description = $_POST["description"];
//partie:
$partie = $_POST["partie"];

function GetImageExtension($imagetype)
{
    if (empty($imagetype)) {
        return false;
    }
    switch ($imagetype) {
        case 'image/bmp':
            return '.bmp';
        case 'image/gif':
            return '.gif';
        case 'image/jpeg':
            return '.jpg';
        case 'image/png':
            return '.png';
        default:
            return false;
    }
}

if (!empty($_FILES["image"]["name"])) {

    $file_name = $_FILES["image"]["name"];
    $temp_name = $_FILES["image"]["tmp_name"];
    $imgtype = $_FILES["image"]["type"];
    $ext = GetImageExtension($imgtype);
    $imagename = $file_name . "-" .  date("d-m-Y") . "-" . time() . $ext;
    $target_path = "images/assets/syltattoo/Emails/" . $imagename;
    if (!empty($_FILES["image2"]["name"])) {

        $file_name2 = $_FILES["image2"]["name"];
        $temp_name2 = $_FILES["image2"]["tmp_name"];
        $imgtype2 = $_FILES["image2"]["type"];
        $ext2 = GetImageExtension($imgtype2);
        $imagename2 = $file_name2 . "-" . date("d-m-Y") . "-" . time() . $ext2;
        $target_path2 = "images/assets/syltattoo/Emails/" . $imagename2;

        if (move_uploaded_file($temp_name, $target_path) && move_uploaded_file($temp_name2, $target_path2)) {
            $query_upload = "INSERT INTO `modx_demandes`(image, image2, name, email, description, partie) VALUES

('" . $target_path . "','" . $target_path2 . "','$name', '$email', '$description', '$partie')";
            mysql_query(
                $query_upload
            ) or die("error in $query_upload == ----> " . mysql_error());
        } else {

            exit("Error While uploading image on the server");
        }

    }
}
?>

这是一些重构的代码。即使用户没有发送任何图像,此代码也应该有效。

<?php
$host = "hst";
$databasename = "nme";
$user = "usr";
$pass = "pwd";
/**********MYSQL Settings****************/


$conn = mysql_connect($host, $user, $pass);

if ($conn) {
    $db_selected = mysql_select_db($databasename, $conn);
    if (!$db_selected) {
        die ('Can\'t use foo : ' . mysql_error());
    }
} else {
    die('Not connected : ' . mysql_error());
}
$name = $_POST["name"];
//email:
$email = $_POST["email"];
//description:
$description = $_POST["description"];
//partie:
$partie = $_POST["partie"];

function GetImageExtension($imagetype)
{
    if (empty($imagetype)) {
        return false;
    }
    switch ($imagetype) {
        case 'image/bmp':
            return '.bmp';
        case 'image/gif':
            return '.gif';
        case 'image/jpeg':
            return '.jpg';
        case 'image/png':
            return '.png';
        default:
            return false;
    }
}

function uploadImage($image) {
    if(!empty($image["name"])) {
        $file_name = $image["name"];
        $temp_name = $image["tmp_name"];
        $imgtype = $image["type"];
        $ext = GetImageExtension($imgtype);
        $imagename = $file_name . "-" . date("d-m-Y") . "-" . time() . $ext;
        $target_path = "images/assets/syltattoo/Emails/" . $imagename;
        if (move_uploaded_file($temp_name, $target_path)) {
            return $target_path;
        }
        exit("Error While uploading image on the server");
    }
    return null;
}

$target_path = uploadImage($_FILES["image"]);
$target_path2 = uploadImage($_FILES["image2"]);

$query_upload = "INSERT INTO `modx_demandes`(image, image2, name, email, description, partie) VALUES ('" . $target_path . "','" . $target_path2 . "','$name', '$email', '$description', '$partie')";
mysql_query($query_upload) or die("error in $query_upload == ----> " . mysql_error());

?>

关于php - 在数据库中插入带有 2 个图像字段的表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26348630/

相关文章:

php - 包含 php 数组键的 Mysql 查询? IE。 "WHERE profession = $user[' 职业']”

php - 无法在 PHP 中连接 2 个数组

php - jquery文件上传-使用ajax发布php变量

MySQL建表报错-表不存在

javascript - 在 ReactJS 中将数据从表单发送到父组件

javascript - AngularJS - 选择选项并在函数中使用它

php - 获取当前登录用户的用户 ID 并将其作为外键值添加到不同的表中返回 null - MySQL - CodeIgniter

mysql - MySQL 唯一键/索引使用什么数据类型

mysql - 选择...以嵌套方式进行更新和提交

javascript - 如何在 html 的 javascript 中制作 "/"命令