0

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

1

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 .

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for answering i've already tried that but still the file_img is not yet readable and it still shows an error Undefined index: file_img
okay, can you provide me the url if that's running so that I can have a look exactly what the problem is.
how can i provide the url here? It wasn't up yet.
0
**IN This Section **
<tr>
    <td>Photo</td>
    <td><input type='file' name='file_img'/></td>
</tr>

Please try this edit, It may help..

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

1 Comment

Thanks for the answer. I've already try this but still has an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.