无法将图像输入到数据库。图像文件上传错误:未定义的索引:file_img

时间:2017-06-11 04:12:53

标签: php

大家好我需要一些关于此错误的帮助。我试图创建一个模块,允许用户上传他们的图像并将其显示到另一页。我使用3个表单(index.php用于显示,create.php用于sql查询,addform.php用于添加记录)。但每次我运行程序时它总是显示错误:未定义的索引:file_img

index.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="assets/jquery-1.11.3-jquery.min.js">           </script>

</head>
<body>
    <div class="container">

    <h2 class="form-signin-heading">Employee Records.</h2><hr />
    <button class="btn btn-info" type="button" id="btn-add"> <span class="glyphicon glyphicon-pencil"></span> &nbsp; Add Employee</button>
    <button class="btn btn-info" type="button" id="btn-view"> <span class="glyphicon glyphicon-eye-open"></span> &nbsp; View Employee</button>
    <hr />

    <div class="content-loader">

    <table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
    <thead>
    <tr>
    <th>Title</th>
    <th>Content</th>
    <th>Photos</th>
    </tr>
    </thead>

    <?php
    require_once 'dbconfig.php';

    $sql = $db_con->prepare("select id, name, content, imgname from tblkeanu");
    $sql->execute();
    while($result=$sql->fetch(PDO::FETCH_ASSOC))
    {
    echo '<tr>';
    echo '<td>' . $result["name"] . '</td>';
    echo '<td>' . $result["content"] . '</td>';
    echo "<td><img src = 'images/" . $result['imgname'] . "' height='350px;' width='300px;' class='img'></td>";
    echo "<td><a id = $result[id] class = 'edit_link' href='#' title = 'EDIT'> EDIT </a></td>";
    echo "<td><a id = $result[id] class = 'delete_link' href='#' title = 'DELETE'> DELETE </a></td>";
    echo '  </td>';
    echo '</tr>';
    }
    ?>

    </table>

    </div>

</div>

<br />

<div class="container">

    <div class="alert alert-info">
    <a href="http://www.codingcage.com/2015/12/simple-jquery-insert-update-delete-with.html" target="_blank">Tutorial Link</a>
    </div>
</div>

</body>
</html>

create.php

<?php
require_once 'dbconfig.php';




if($_POST)
{
    $name = $_POST['txtname'];
    $content = $_POST['txtcontent'];
    $filetmp = $_FILES['file_img']['tmp_name'];
    $filename1 = $_FILES['file_img']['name'];
    $filetype = $_FILES['file_img']['type'];
    $filepath = 'images/'.$filename1;



    try{

        move_uploaded_file($filetmp,$filepath); 
        $sql = $db_con->prepare("INSERT INTO `tblkeanu`(`name`, `content`, `imgname`, `imgpath`, `imgtype`) Values (:name,:content,:filename1,:filepath,:filetype)");
        $sql->bindParam(":name",$name);
        $sql->bindParam(":content",$content);
        $sql->bindParam(":filename1",$filename1);
        $sql->bindParam(":filetype",$filetype);
        $sql->bindParam(":filepath",$filepath);

        if($sql->execute())
        {
            echo "Successfully Added";
        }
        else{
            echo "Query Problem";
        }   
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
}

&GT;

addform.php

 <form method='post' id='emp-SaveForm' action="#" enctype="multipart/form-data">

<table class='table table-bordered'>

    <tr>
        <td>Title</td>
        <td><input type='text' name='txtname' class='form-control' placeholder='EX : john doe' required /></td>
    </tr>

    <tr>
        <td>Content</td>
        <td><input type='text' name='txtcontent' class='form-control' placeholder='EX : Web Design, App Design' required></td>
    </tr>

    <tr>
        <td>Photo</td>
        <td><input type='file' name='file_img'/></td>
    </tr>

    <tr>
        <td colspan="2">
        <button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
        <span class="glyphicon glyphicon-plus"></span> Save this Record
        </button>  
        </td>
    </tr>

</table>

这里棘手的部分是如果我将addform.php合并到create.php并且地址栏是“.... / .... / create.php”,程序将顺利运行并确定输入类型。但我需要将这两个分开,而不是在一个页面上合并,因此每次都不会刷新网页,因为我也使用了javascript和jquery,地址应该只是“.... / .... / index.php ”

如果你能帮助我,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

在你的addform.php文件中,表单还没有动作,表单动作应该是create.php。

在create.php文件中的条件为:

if($sql->execute())
    {
        echo "Successfully Added";
    }
    else{
        echo "Query Problem";
    }  

应修改为:

if($sql->execute())
    {
        header("Location: index.php");
    }
    else{
        echo "Query Problem";
    }   

这样,将记录保存到数据库后,您将被重定向到index.php。

答案 1 :(得分:0)

**IN This Section **
<tr>
    <td>Photo</td>
    <td><input type='file' name='file_img'/></td>
</tr>

请尝试此编辑,这可能会有所帮助..

<tr>
    <td>Photo</td>
    <td><input type='file' name='file_img' accept='image/*'/></td>
</tr>