php – Upload image to folder and save path in mysql-ThrowExceptions

Exception or error:

I’m trying to save an image in folder and save the path in my database, but I can’t do that.
When I insert only name and phone all is ok, but when I add the input type file, it saves my name and phone number but not the image path and not upload the image in folder.
Here is the code:

PED.PHP

            <form class="form-horizontal form-label-left input_mask" method="post" id="add" name="add" enctype="multipart/form-data">
                    <div class="form-group"" >
                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">CLIENT</label>
                        <div class="col-md-9 col-sm-9 col-xs-12"  style="float: left; width:70px;">
                     <input type="text" style="width:200px; float:left;"name="client" class="form-control" placeholder="" >
                        </div>
                     </div>
                      <div class="form-group" style="border: 1px ; height:45px;" >
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">PHONE:
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12"  style="float: left; width:70px;">
                     <input type="text" style="width:200px; float:left;"name="phone" class="form-control" placeholder="" >
                        </div>
                     </div>
                     <div class="form-group" style="border: 1px ; height:45px;" >
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">PICTURE:
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12"  style="float: left; width:70px;">
                            <input type="file" name="uploadImage" id="uploadImage">
                        </div>
                     </div>
                    <div class="ln_solid"></div>
                    <div class="form-group">
                     <button id="save_data" type="submit" class="btn btn-success">Guardar</button>
                        </div>
                    </div>    
                  </form

and here I receive all data:
ADDPED.PHP

<?php   
date_default_timezone_set('America/Mexico_City');
session_start();
if (empty($_POST['name'])) {
       $errors[] = "Selecciona una tienda";
    } else if (empty($_POST['cliente'])){
        $errors[] = "Ingrese cliente";
    } else if (
        !empty($_POST['name']) &&
        !empty($_POST['cliente'])
    ){


    include "../config/config.php";

    $client =   $_POST["cliente"];
    $phone  =   $_POST["telefono"];
    ("images/fotos/" . $_FILES["uploadImage"]["name"]);


  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"],
        "images/fotos/" . $_FILES["file"]["name"]);
    }

    $sql="insert into pedido (client,phone,image) value ('$client','$phone','".$_FILES['uploadImage']['name']."')";

    $query_new_insert = mysqli_query($con,$sql);
        if ($query_new_insert){
            $messages[] = "Tu ticket ha sido ingresado satisfactoriamente.";
        } else{
            $errors []= "Lo siento algo ha salido mal intenta nuevamente.".mysqli_error($con);
        }
    } else {
        $errors []= "Error desconocido.";
    }

    if (isset($errors)){

        ?>
        <div class="alert alert-danger" role="alert">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
                <strong>Error!</strong> 
                <?php
                    foreach ($errors as $error) {
                            echo $error;
                        }
                    ?>
        </div>
        <?php
        }
        if (isset($messages)){

            ?>
            <div class="alert alert-success" role="alert">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <strong>¬°Bien hecho!</strong>

                    <?php
                        foreach ($messages as $message) {
                                echo $message;

                            }
                        ?>
            </div>
            <?php
        }

    ?>
How to solve:

The maximum default file size is 2MB for file upload. but you can change it in the configurations and also check the data type you used in the DB and the maximum limit of it. It’s better to upload the images in a folder and insert the file name to the DB. it will also increase the performance of your system. check this example for upload image to Database.

Leave a Reply

Your email address will not be published. Required fields are marked *