du512053619 2014-10-16 23:55
浏览 27
已采纳

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

I am successfully able to insert an image and video file to my mysql database, but it is creating 2 records. My belief is because of my "Type" field, it's not able to create 2 different values for the same record, so I created "Type" and "Type2" thinking that would solve the issue. It created the same "Image/png" value. Here is my form script:

<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>

This is the script for inserting the files:

<?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.");
    }
}
?>

Any help that anyone could provide would be appreciated. Again, I would prefer to only have one record created. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • douzhi2760 2014-10-17 01:54
    关注

    Here you are inserting both the Photo & Video at same-time, In meantime you need to do both
    Validation & Db insertion. So,use the flag concept to attain this,

    // flag declaration $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 
    

    ('".$type."','".$rpic."','".$path."','".$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."); }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程