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


Insert form data with 2 image fields in db

我已经搜索过了,但找不到任何明确的内容。我有一个有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());
?>

这是因为文件名的日期和时间相同。。。放入

$imagename2=date("d-m-Y")."-".time() . "-2" .$ext;

对于第二个文件名

您还需要移动第二个文件

if(move_uploaded_file($temp_name, $target_path) && move_uploaded_file($temp_name2, $target_path2)) {