Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在db中插入带有2个图像字段的表单数据_Php_Mysql_Forms_File Upload_Image Uploading - Fatal编程技术网

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

Php 在db中插入带有2个图像字段的表单数据,php,mysql,forms,file-upload,image-uploading,Php,Mysql,Forms,File Upload,Image Uploading,我已经找过了,但找不到任何清楚的东西。 我有一个有2个输入文件字段的表单,我需要插入所有数据。 我不想一个多文件上传,我需要每个输入文件是分开的。 我喜欢上面的代码,因为它重命名了每个文件。 我试过这个 `$host="hst"; $databasename="nme"; $user="usr"; $pass="pwd"; /**********MYSQL Settings****************/ $conn=mysql_connect($host,$user,$pass); i

我已经找过了,但找不到任何清楚的东西。 我有一个有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>

笔名:
电邮:
说明:
军团党:
特使vos图像

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

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

$imagename2=日期(“d-m-Y”)。“-”.time()。“-2”。$ext

第二个文件名

您还需要移动第二个文件


如果(move_upload_file($temp_name,$target_path)和move_upload_file($temp_name2,$target_path2)){
您在脚本中输入了一些错误(您从
$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");
        }

    }
}
?>

不,这两个字段的结果都是一样的。可以查看您的HTLM表单吗?好的,您不应该将图像2的代码中的
type2
tmp\u name2
作为目标,但是
type
tmp\u name
。我刚刚用这个补丁编辑了我的答案。您能试试吗?如果不行,您能回显
$target_name
和插入前的
$target_name 2
部分有效,如果两个图像字段为空,则不会记录任何数据。我不明白。您的评论是否愤世嫉俗:)?
<?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());

?>