duanpanhuo0618 2017-06-11 04:12
浏览 70

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

Hi everyone i need some help regarding this error. Im trying to create a module that will allow users to upload their image and display it to the other page. I am using 3 forms (index.php for displaying, create.php for sql query and addform.php for adding records). But everytime I run the program it always shows an error: Undefined index: 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();
    }
}

?>

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>

The tricky part here is if i combined the addform.php to create.php and address bar is "..../..../create.php" the program runs smoothly and the input type was identified. but i need these 2 to be separated and not combined on one page so the webpage will not be refreshed everytime because im also using a javascript and jquery and the address should only be "..../..../index.php".

It will be much appreciated if you could help me out.

  • 写回答

2条回答 默认 最新

  • douwen7603 2017-06-11 04:31
    关注

    In your addform.php file the form dosen't have an action yet, The form action should be create.php.

    In the create.php file the condition:

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

    Should be modified as :

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

    This way after saving the records to the database you will be redirected to index.php .

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效