2

I am trying to insert data to the database and upload an image. The image name will be saved in the database and the image will be saved in Images folder creating random folder inside. But nothing is saved in the database.

HTML code

 <form action="news.php" method="post">
                    <label>Title</label>
                    <input type="text"  placeholder="Title" class="form-control" name="title" required><br/><br/>

                    <label>Description-1</label>
                    <textarea class="form-control" rows="4" cols="50" name="desc1" required></textarea><br/><br/>

                    <label>Description-2</label>
                    <textarea class="form-control" rows="4" cols="50" name="desc2"></textarea><br/><br/>

                    <label>Description-3</label>
                    <textarea class="form-control" rows="4" cols="50" name="desc3"></textarea><br/><br/>

                    <label>Description-4</label>
                    <textarea class="form-control" rows="4" cols="50" name="desc4"></textarea><br/><br/>

                    <label>Image</label>
                    <input type="file" name="Image" id="ImageFile" class="form-control">

                    <br/><br/>
                    <button type="button" name="submit" class="btn btn-success">Submit</button>
                </form>

php code

if (isset($_REQUEST['submit'])) {

$title = $_POST['title'];
$desc1 = $_POST['desc1'];
$desc2 = $_POST['desc2'];
$desc3 = $_POST['desc3'];
$desc4 = $_POST['desc4'];
$Image = $_POST['Image'];

$folder_path = 'Images/';

$imagename = basename($_FILES['Image']['name']);
$newname2 = $folder_path . $imagename;

if ((@move_uploaded_file($_FILES['Image']['tmp_name'], $newname2))) {
    $query = "INSERT INTO tbl_news(title,desc1,desc2,desc3,dessc4,image) VALUES('{$title}','{$desc1}','{$desc2}','{$desc3}','{$desc4}','{$imagename}')";
    $result = mysql_query($query,$con);
    if ($result) {
        echo "<script type='text/javascript'>alert('Submiited Successfully')</script>";
    }
}

}

Please help!!!

Andrew
  • 223
  • 1
  • 3
  • 12

1 Answers1

1

change your form tag

from

 <form action="news.php" method="post">

to

<form action="news.php" method="post" enctype='multipart/form-data' >

you will need

enctype='multipart/form-data

when ever you are dealing with files

for more reference

see this link Form-data explained

Community
  • 1
  • 1
Meenesh Jain
  • 2,532
  • 2
  • 19
  • 29