drlh197610 2014-11-29 11:16
浏览 96
已采纳

PHP将文件和文本从多个文本字段上传到mysql

I'm making a form with 5 text fields and 1 file field. When I tried having 2 text fields it works but if there's more,nothing works.The image successfully uploads to the folder. here's what i have so far. I'm still a noob and i'm in the stage where i gather codes that work so i'm sorry if these are outdated,messy, or something.

<?php
if(isset($_FILES['filename'])){
$errors = array();
$file_name = $_FILES['filename']['name'];
$file_size =$_FILES['filename']['size'];
$file_tmp =$_FILES['filename']['tmp_name'];
$file_type=$_FILES['filename']['type'];   
$file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));

if(isset($_POST['randomtext'])){
    $text_here = $_POST['randomtext'];
}

$expensions= array("jpeg","jpg","png");         
if(in_array($file_ext,$expensions)=== false){
    $errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
 $errors[]='File size must be excately 2 MB';
}          

// if there are no errors...     
if (empty($errors)==true) {

    // upload the file...
    move_uploaded_file($file_tmp,"uploads/".$file_name);

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "admin";

    // and create a new record in the database
    mysql_connect($servername, $username, $password) or die ('MySQL Not found // Could Not Connect.');
    mysql_select_db("admin") or die(mysql_error()) ;
    mysql_query("INSERT INTO upload_test (text, fileName) VALUES ('$text_here', '$file_name')") ;

    echo "Success";
    }else{
    print_r($errors);
    }
  }

 ?>

The form:

<form name="form" method="POST" enctype="multipart/form-data" >
 <input name="randomtext" type="text" id="randomtext" /><br/><br/>
 <input name="filename" type="file" id="filename" />
 <input name="submit" type="submit" id="submit"/>
</form>

There. the randomtext is a sample textfield.what if there are more of it?. Kindly help me, please. You can code your own but the goal is a form with 5 text fields and 1 filefield.the filename and other text are inserted to the db and the uploaded file is uploaded to a folder.

  • 写回答

1条回答 默认 最新

  • doufeng2877 2014-11-29 11:40
    关注

    You can add more field like :

    <input name="randomtext2" type="text" id="randomtext2" /><br/><br/>
    <input name="randomtext3" type="text" id="randomtext3" /><br/><br/>
    <input name="randomtext4" type="text" id="randomtext4" /><br/><br/>
    
    if(isset($_POST['randomtext2'])){
        $text2_here = $_POST['randomtext2'];
    }
    if(isset($_POST['randomtext3'])){
        $text3_here = $_POST['randomtext3'];
    }
    if(isset($_POST['randomtext4'])){
        $text4_here = $_POST['randomtext4'];
    }
    

    mysql_query("INSERT INTO upload_test (text, text2, text3, text4, fileName) VALUES ('$text_here', '$text2_here', '$text3_here', '$text4_here','$file_name')") ;

    You have to add in the database columns text2, text3, text4 too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错