teampoison's avatar

Only Text Insert in Database. Image not insert in database.

Issue was in PHP

When i fill the form only text field is update on database image file not updated on database.

database table name : datesheet

datesheet table column name : coursename , sheet

After fill form only data was fill on coursename. sheet show blank but i add image on form. it's also not uploaded on path that i define where the image file are stored.

HTML FORM FOR UPLOAD :

<form action="action/Add-Datesheet.php>" method="post" enctype="multipart/form-data">
                <div class="row">

                    <div class="col-lg-6 col-md-6 col-sm-11">
                        <div class="form-group">
                            <label for="">Course Full Name</label>
                            <input type="text" class="form-control" name="coursename" required>
                        </div>
                    </div>

                    <div class="col-lg-6 col-md-6 col-sm-11">
                        <div class="form-group">
                            <label for="">Upload Datsheet</label>
                            <input type="file"  class="form-control" name="sheet" required>
                        </div>
                    </div>


                </div>
                
                <div class="form-group">
                    <input type="submit" value="Upload Datesheet" name='image'>
                </div>
            </form>

Form Action Code

<?php
session_start();
include('../../config.php');

$coursename = $_POST['coursename']; 
$sheet = $_POST['sheet']; 
 


$file_tmp = $_FILES["sheet"]["tmp_name"];


$img_path = "images/UserImage/";

$imagename = $img_path . $file_name;

$Insert_Sql = "INSERT INTO datesheet (sheet, coursename) VALUES('$sheet', '$coursename')";

$result = mysqli_query($conn, $Insert_Sql);

if($result){
    move_uploaded_file($file_tmp, "/images/UserImage/". $file_name);
    echo "
        <script>
            alert('Datesheet Added');
            window.location.href='../View-Blogs.php';
        </script>
    ";
    
}else{
    echo "
        <script>
            alert(Some Error Found. Please Try Again Later.);
        </script>
    ";
}

?>

0 likes
8 replies
kokoshneta's avatar

This doesn’t appear to have anything to do with Laravel. Also, don’t link to your code – include it in the question itself by putting a line with three backticks ``` before and after each code block.

1 like
kokoshneta's avatar

@teampoison It’s PHP, yes, but it’s not Laravel. This is a Laravel forum. Doesn’t mean plain PHP is necessarily off-topic, but if there’s no Laravel element to it, why not ask in a larger forum like Stack Overflow?

Tray2's avatar

Where does the file_name come from?

$imagename = $img_path . $file_name;
Sergiu17's avatar
$coursename = $_POST['coursename']; 

$file_tmp = $_FILES["sheet"]["tmp_name"];
$file_name = $_FILES["sheet"]["name"];
$file_path = "images/UserImage/" . $file_name;

$result = mysqli_query(
	$conn, 
	"INSERT INTO datesheet (sheet, coursename) VALUES('$file_path', '$coursename')"
);

move_uploaded_file($file_tmp, $file_path);

this should do, also check some articles, like this one https://www.w3schools.com/php/php_file_upload.asp

for the $file_name you may add some timestamp or some random characters, in order to avoid overwriting files with the same name

$file_name = $_FILES["sheet"]["name"] . time();

Please or to participate in this conversation.