0
<?php

    if(isset($_POST['hour'])&&isset($_POST['day'])){

        //the path to store the uploaded image.
        $target = "images/".basename($_FILES['image']['name']);

        $db = mysqli_connect("localhost","root","","a_database");

        $image = $_FILES['image']['name'];
        $carmodel = $_POST['carmodel'];
        $cartype = $_POST['cartype'];
        $hour = $_POST['hour'];
        $day = $_POST['day'];

        $query = "INSERT INTO `cardetails` (`image`, `carmodel`, `cartype`, `hour`, `day`) VALUES ('".$image."', '".$carmodel."', '".$cartype."', '".$hour."', '".$day."')";
        mysqli_query($db,$query);//stores the data

        //move uploaded image into the folder.
        if(move_uploaded_file($_FILES['image']['tmp_name'],$target)){
             header('Location:upload_success.php');
        }else{

             header('Location:upload_fail.php');
        }

    }else{
            echo ' ';
    }

?>


<div class="col-lg-offset-4">
    <form method="POST" action="upload.php" enctype="multipart/form-data">
    <input type="hidden" name="size" value="1000000">
    <div class="form-group row">
        <input type="file" name="image">
    </div>

    <div class="form-group col-lg-4 row">
        <label> Car Model </label>
        <select name="carmodel" class="form-control" >
        <option value="Myvi">Myvi</option>
        <option value="Vios">Vios</option>
        <option value="Lambo">Lambo</option>
        </select>
    </div>

    <div class="form-group row col-lg-offset-4">
    <label> Car Type </label>
        <input type="radio" name="cartype" value="Auto">Auto 
        <input type="radio" name="cartype" value="Manual"> Manual 
    </div> 

    <div class="form-group">
        <input type="text" name="hour" placeholder="$/hour">
    </div>

    <div class="form-group">
        <input type="text" name="day" placeholder="$/day">
    </div>

    <div class="form-group">
        <button type="submit">Upload</button>
    </div>

</form>

</div>

I've successfully insert the data into database with the image name but the image doesn't seems to upload into my images file, what could be wrong? Based on the code above, upload_fail.php is called. If isset with $_POST['image'], nothing happens.

  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly May 08 '17 at 11:39
  • what is your complete image path ? did you give 755 permission ? – Niklesh Raut May 08 '17 at 11:40
  • move_uploaded_file() returns false if an error occured, and issues a Warning. Maybe you should look in your Apache error log to see what the warning is? Or turn on error output (display_errors = On in your php.ini) so that you can see the output directly. – Alex Schenkel May 08 '17 at 11:56
  • I had a similar issue a long time ago, it was because I hadn't got read/write permissions on the temp folder. – Ginger Squirrel May 08 '17 at 12:09

1 Answers1

0

I think, you don't have sufficient permissions for writing to images directory. Does exist images directory itself?

If you want to know what's wrong, you can enable error reporting on beginning of your php file like:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);   
    ...
von Oak
  • 823
  • 5
  • 14