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


PHP upload file and text from multiple text fields to mysql

我正在制作一个包含 5 个文本字段和 1 个文件字段的表单。当我尝试拥有 2 个文本字段时,它可以工作,但如果有更多,则没有任何效果。图像已成功上传到文件夹。这是我到目前为止所拥有的。我仍然是一个菜鸟,我正处于收集有效代码的阶段,所以如果这些代码过时、凌乱或其他东西,我很抱歉。

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

形式:

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

随机文本是一个示例文本字段。如果有更多呢?。请帮帮我。您可以自己编写代码,但目标是包含 5 个文本字段和 1 个文件字段的表单。文件名和其他文本将插入数据库,上传的文件将上传到文件夹。

您可以

添加更多字段,例如:

<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')") ;

您还必须text2, text3, text4添加数据库列。