PHP将图像和视频插入一个mysql记录

时间:2014-10-16 23:55:03

标签: php mysql

我成功地将图像和视频文件插入到我的mysql数据库中,但是它创建了2条记录。我的信念是因为我的“类型”字段,它无法为同一记录创建2个不同的值,因此我创建了“类型”和“类型2”思维来解决问题。它创建了相同的“Image / png”值。 这是我的表单脚本:

<form action="saveimage.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5"      cellpadding="5">
<tbody>
<tr><td>Picture:</td><td><input name="rpic" type="file" accept="image/*"></td></tr>
<tr><td>Video:</td><td><input name="rvideo" type="file" accept="video/*"></td></tr>
<tr><td><input name="Upload Now" type="submit" value="Upload"></td></tr>
</tbody></table>
</form>

这是插入文件的脚本:

<?php
include("config.php");
error_reporting(E_ERROR | E_PARSE);

function GetImageExtension($rpic)
{
    if(empty($rpic)) return false;
    switch($rpic)
    {
        case 'image/bmp': return '.bmp';
        case 'image/gif': return '.gif';
        case 'image/jpeg': return '.jpg';
        case 'image/png': return '.png';
        default: return false;
    }
}
function GetVideoExtension($rvideo)
{
    if(empty($rvideo)) return false;
    switch($rvideo)
    {
        case 'video/asf': return '.asf';
        case 'video/avi': return '.avi';
        case 'video/wmv': return '.wmv';
        default: return false;
    }
}
if (!empty($_FILES["rpic"]["name"]))
{
    $file_name=$_FILES["rpic"]["name"];
    $temp_name=$_FILES["rpic"]["tmp_name"];
    $type=$_FILES["rpic"]["type"];
    $ext= GetImageExtension($type);
    $name=$_FILES["rpic"]["name"];
    $path = "uploads/".$name;

if(move_uploaded_file($temp_name, $path))
{
    $query_upload="INSERT into recipes (type, rpic, path, posted) VALUES ('".$type."','".$rpic."','".$path."','".date("Y-m-d")."')";
    mysql_query($query_upload) or die("Error in $query_upload == ----->".mysql_error());
} else {
    exit("Error while uploading your file.");
    }
}
if (!empty($_FILES["rvideo"]["name"]))
{
    $file_name=$_FILES["rvideo"]["name"];
    $temp_name=$_FILES["rvideo"]["tmp_name"];
    $type=$_FILES["rvideo"]["type"];
    $ext= GetVideoExtension($type2);
    $name=$_FILES["rvideo"]["name"];
    $path = "uploads/".$name;
    $pathvideo = "uploads/".$name;

if(move_uploaded_file($temp_name, $pathvideo))
{
    $query_upload="INSERT into recipes (type, rvideo, pathvideo, posted) VALUES ('".$type."','".$rvideo."','".$pathvideo."','".date("Y-m-d")."')";
    mysql_query($query_upload) or die("Error in $query_upload == ----->".mysql_error());
} else {
    exit("Error while uploading your file.");
    }
}
?>

任何人都可以提供任何帮助将不胜感激。同样,我宁愿只创建一条记录。 提前谢谢!

2 个答案:

答案 0 :(得分:1)

在这里插入Photo&amp;视频同时,同时你需要做两个  验证&amp; Db插入。因此,使用标志概念来实现这一目标,

//标志声明                         $ flag_Photo = 0;                         $ flag_Video = 0;

                    if (!empty($_FILES["rpic"]["name"]))
                    {
                        $file_name=$_FILES["rpic"]["name"];
                        $temp_name=$_FILES["rpic"]["tmp_name"];
                        $type=$_FILES["rpic"]["type"];
                        $ext= GetImageExtension($type);
                        $name=$_FILES["rpic"]["name"];
                        $path = "uploads/".$name;

                    if(move_uploaded_file($temp_name, $path))
                    {
                      $flag_Photo=1;  
                    } 
                    else {
                        $flag_Photo=0;
                        //exit("Error while uploading your file.");
                        }
                    }
                    if (!empty($_FILES["rvideo"]["name"]))
                    {
                        $file_name=$_FILES["rvideo"]["name"];
                        $temp_name=$_FILES["rvideo"]["tmp_name"];
                        $type=$_FILES["rvideo"]["type"];
                        $ext= GetVideoExtension($type2);
                        $name=$_FILES["rvideo"]["name"];
                        $path = "uploads/".$name;
                        $pathvideo = "uploads/".$name;

                    if(move_uploaded_file($temp_name, $pathvideo))
                    {
                    $flag_Video=1; 

                    } else {
                        $flag_Video=0;
                        }
                    }
                    if($flag_Photo ==1 && $flag_Video==1)
                    {
                     $query_upload="INSERT into recipes (type, rpic, path, rvideo, pathvideo,  
                        posted) VALUES 

(&#39;&#34; $类型&#34;&#39;&#39;&#34; $ rpic&#34;&#39;,&#39; &#34; $路径&#34;&#39;&#39;&#34; $ rvideo&#34;&#39;&#39;&#34; $ pathvideo。 &#34;&#39;&#39;&#34; .date(&#34; YM-d&#34)。&#34;&#39;)&#34 ;;                             mysql_query($ query_upload)或die(&#34; $ query_upload中的错误== -----&gt;&#34; .mysql_error());                          }                          其他                          {                             退出(&#34;上传文件时出错。&#34;);                          }

答案 1 :(得分:0)

每次使用INSERT时,都会创建一个新行。如果只想要一行使用一个SQL查询或使用UPDATE查询:

INSERT into recipes (type, rpic, rvideo, pathvideo, posted) VALUES (...)

您必须重新排列逻辑或在第二个查询上使用UPDATE。像这样:

UPDATE recipes SET rvideo = $val WHERE id = $id