PHP文件上传到mysql

时间:2015-01-05 05:57:03

标签: php mysql file-upload

我创建了一个表单如下。我正在做的是

  1. 用户从下拉列表中选择客户端并上传文件。当他点击“添加”按钮时,页面将重定向到Confirm.php。

  2. Confirm.php将显示用户提供输入的内容,即客户名称和他上传的文件的名称。

  3. 在Confirm.php中,用户正在提交表单,它应该添加到数据库,除了文件上载剩余字段正在添加到数据库。在数据库中,文件上载字段显示为空。请有人解决这个问题。 而且我没有得到如何从Confirm.php访问路径变量到Add.php。

  4. 我是php的新手。所以任何帮助都将不胜感激。

    谢谢。

    Home.php

    <form action="Confirm.php" method="post" enctype="multipart/form-data" novalidate>
        <label> <span>Client</span>
          <select class="required" name="client">
                 <?php
    mysql_connect ("localhost","root","");
                        mysql_select_db ("eservice");
                        $select="eservice";
                        if (isset ($select)&&$select!="")
                        {
        $select=$_POST ['NEW'];
                        }
                ?>
              <?php
        $list=mysql_query("select * from client");
                        while($row_list=mysql_fetch_assoc($list))
                                                {
               ?>
              <?php $ct = $row_list['cname'];?>
              <option value="<?php echo $ct; ?>"<?php if($ct==$select){ echo "selected"; } ?> > <?php echo $ct; ?></option>
              <?php } ?>
          </select>
        </label>
    
        <label> <span>SRN</span>
    <?php
    $con=mysqli_connect("localhost","root","","eservice");
    // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    $sql="Select * from main";
    if ($result=mysqli_query($con,$sql))
      {
      // Return the number of rows in result set
      $rowcount=mysqli_num_rows($result);
      $rowcount++;
      // Free result set
      mysqli_free_result($result);
      }
    mysqli_close($con);
    ?>
    <input name="srn" type="text" id="srn" size="15" readonly="readonly" maxlength="40" value="<?php echo "$rowcount"; ?>"/>
    </label>
        </div>
            <label>
                <span>File upload</span>
                <input type="file" name ="filename" required>
            </label>
        <button id='send' type='submit'>Add</button>
    <button id='clear' type='reset'>Reset</button>
    </form>
    

    这是我的cofirmation页面

    Confirm.php

      <form action="Add.php" method="post" enctype="multipart/form-data" novalidate>
    <label> <span>Client</span>
        <?php include_once('dbconn.php'); ?>
         <input name="client" type="text" id="client" size="15" readonly="readonly" maxlength="40" value="<?php echo $_POST['client']; ?>"/>
       </label>
    
            <label>
                <span>File upload</span>
        <?php  $path = '';
    $folder = "Folder/";
    if (is_uploaded_file($_FILES['filename']['tmp_name']))  
    {   
        if (move_uploaded_file($_FILES['filename']['tmp_name'], $folder.$_FILES['filename']['name'])) 
        {
        $path = $folder . $_FILES['filename']['name'];
        } 
        else 
        {
         $path = '';
        };
    } 
    else 
    {
          $path = '';
    }; ?>
            <input name ="filename" readonly="readonly" value="<?php echo $_FILES['filename']['name']; ?>"/>
            </label>
        <button id='clear' type='reset'>Back</button>
    <button id='send' type='submit'>Add</button>
    </form>
    

    dbconn.php

    <?php
    $username = "root";
    $password = "";
    $hostname = "localhost"; 
    $dbhandle = mysql_connect($hostname, $username, $password) 
      or die("Unable to connect to MySQL");
    $selected = mysql_select_db("eservice",$dbhandle) 
      or die("Could not select newsite database");
    ?>
    

    这里我将上传的文件添加到phpmyadmin。

    Add.php

    <?php
    include_once('dbconn.php');
    session_start();
    $_SESSION['example']='Session Created';
    
     $client = $_POST['client']; // required
    
     if($client !='')
     {
    $insQry = "Insert into `main` (client,upload) Values ('$client','$path')";
        $insertQ = mysql_query($insQry);
        if($insertQ!=''){
            echo "<h2>Data inserted successfully...</h2>";
        } else {
            echo "<h2>Not added</h2>";
        }
    }
    ?>
    

2 个答案:

答案 0 :(得分:0)

未定义的索引意味着如果该索引未设置或为空......您必须检查它是否为空。

在文件名错误中添加条件

if ( $_FILES['filename']['error'] == 0 ){
///your uploading code
}

并且对于可验证的检查,它是否为isset,使用三元运算符: -

$client =isset( $_POST['client'])?$_POST['client']:'';

答案 1 :(得分:0)

您的confirm.php代码应为

<form action="Add.php" method="post" enctype="multipart/form-data" novalidate>
<label> <span>Client</span>
    <?php include_once('dbconn.php'); ?>
     <input name="client" type="text" id="client" size="15" readonly="readonly" maxlength="40" value="<?php echo $_POST['client']; ?>"/>
   </label>

        <label>
            <span>File upload</span>
    <?php  $path = '';
$folder = "Folder/";
if (is_uploaded_file($_FILES['filename']['tmp_name']))  
{   
    if (move_uploaded_file($_FILES['filename']['tmp_name'], $folder.$_FILES['filename']['name'])) 
    {
    $path = $folder . $_FILES['filename']['name'];
    } 
    else 
    {
     $path = '';
    };
} 
else 
{
      $path = '';
}; ?>
        <input name ="filename" readonly="readonly" value="<?php echo $_FILES['filename']['name']; ?>"/>
<input name ="path" type="hidden" value="<?php echo $path; ?>"/>
        </label>
    <button id='clear' type='reset'>Back</button>
<button id='send' type='submit'>Add</button>
</form>

您可以使用add.php

$path = $_POST['path']上获取隐藏字段值