使用php和mysql的数据和文件上传表单

时间:2014-08-10 12:15:56

标签: php mysql database forms file-upload

我需要帮助将数据(经销商名称,品牌,型号等)和多个文件(图像和视频)上传到文件夹,然后上传到数据库的路径。

我的代码看起来很完美,因为我没有收到任何错误,但没有任何内容被发送到数据库或文件夹。

upload.php的

<?php

$con = mysqli_connect('localhost', 'root', 'mbiti', 'vaws');

//check connection
//if (!$link) {

//  die('Connect Error(' .mysqli-connect_errno(). ')'
//                  .mysqli-connect_errno());


//}

//escape variables for added security
//these field are entered by user using the form
$dealer_name = mysqli_real_escape_string($con, $_POST['dealer_name']);
$phone_number = mysqli_real_escape_string($con, $_POST['phone_number']);
$advert_title = mysqli_real_escape_string($con, $_POST['advert_title']);
$make = mysqli_real_escape_string($con, $_POST['make']);
$model = mysqli_real_escape_string($con, $_POST['model']);
$type = mysqli_real_escape_string($con, $_POST['type']);
$year = mysqli_real_escape_string($con, $_POST['year']);
$price = mysqli_real_escape_string($con, $_POST['price']);
$condition_ = mysqli_real_escape_string($con, $_POST['condition_']);

$query2="INSERT into advert('dealer_name' , 'phone_number' , 'advert_title' , 'make' , 'model' , 'type' , 'year' , 'price' , 'condition_') VALUES ('$dealer_name' , '$phone_number' , '$advert_title' , '$make' , '$model' , '$type' , '$year' , '$price' , '$condition_')";
mysqli_query($con, $query2);    


if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 20971520){
            $errors[]='File size must be less than 20 MB';
        }   

        $query="INSERT into media (`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$file_name','$file_size','$file_type')";


        $desired_dir="user_data";

        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"user_data/".$file_name);
            }else{                                  //rename the file if another one exist
                $new_dir="user_data/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
            mysqli_query($con, $query); 
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
    }
}

mysqli_close($con);

?>

0 个答案:

没有答案