0

Mysql Database attribute image name and type

`Image varchar256`

I m trying to upload image name in mysql but issue is image is move to folder as per requirement but name of image is not showing in database. kindly help...

<form method="post" enctype="multipart/form-data" >
    <div class="content table-responsive table-full-width">
        <div class="title" style="margin-left: 7%;"> Product ID</div>
        <input  class="form-control"  type="text"  name="P_id" placeholder="Product ID" readonly/>
        <div class="title" style="margin-left: 7%;" >Product Name</div>
        <input class="form-control"  type="text" name="P_Name" placeholder="Product Name" required/>
        <div class="title" style="margin-left: 7%;">Descriptiom</div>
        <textarea class="form-control"  type="text" name="P_Description" placeholder="Description"required></textarea>
        <div class="title" style="margin-left: 7%;">Category</div>

        <!-- Category Add in Drop Down-->
        <select name="C_id" class="form-control" required>
        <option>Select Category</option>
            <?php
            $get_category= "select * from category";
            $run_category= mysqli_query($con,$get_category);
            while($row_category=mysqli_fetch_array($run_category))
            {
                $C_id=$row_category['C_id'];
                $C_Name=$row_category['C_Name'];
                echo "<option value='$C_id'>$C_Name</option>";
            }
            ?>
        </select><!-- End Conde-->

        <div class="title" style="margin-left: 7%;">Price</div>
        <input class="form-control"  type="text" name="P_Price" placeholder="Enter Price"required/>
        <div class="title" style="margin-left: 7%;">Quantity</div>
        <input class="form-control"  type="text" name="P_Quantity" placeholder="Enter Quantity"required/>
        <div class="title" style="margin-left: 7%;">Image</div>
        <input class="form-control"  type="file" name="image"  placeholder="Upload Image"required accept=""/>
        <div class="navbar-form">
            <input type="submit" class="btn btn-primary " name="Insert" value="Insert"style="margin-left: 7%;"/>
            <label name="Label" ></label>
        </div>
        <div class="navbar-form">
            <a href="Categoryadd.php" class="btn btn-primary"style="margin-left: 7%;">+Add Category</a>
            <a href="Brands.php" class="btn btn-primary">+Add Brand</a>
        </div>
    </div>
</form>

The Example which move image to folder but image name not showing in database

<?php
if(isset($_POST['Insert'])){
    $P_id=$_POST['P_id'];
    $P_Name=$_POST['P_Name'];
    $P_Description= $_POST['P_Description'];
    $CId=$_POST['C_id'];
    $P_Price=$_POST['P_Price'];
    $P_Quantity=$_POST['P_Quantity'];

    //image names
    $PImage = $_FILES['image']['name'];

      //image temp names

   $tempname = $_FILES["image"]["tmp_name"];

//for image upload on folder
    move_uploaded_file($tempname,"Images/$PImage");

    if($PName='' OR $PDescription='' OR $C_id='' OR $Price='' OR $Quantity='' OR $PImage='' )
    {
        echo"<script>alert('please fill fields')</script>";
    }
    else {

        $insert_Product = "INSERT INTO `product`(`P_id`, `P_Name`, `P_Description`, `Price`, `Quantity`, `C_Id`, `Image`) VALUES ('','$P_Name','$P_Description','$P_Price','$P_Quantity','$CId','$PImage')";

        $run_Product = mysqli_query($con, $insert_Product);

        if($run_Product){
            $label= "Label";
            $label= "Enter Data successfully";
            echo $label;
        }
    }
}
?>

How can i resolve my issue ?

lalit bhakuni
  • 607
  • 5
  • 15
  • You have inconsistencies with variable names. You declare `$P_Name` but you call `$PName`. Same for many other variables. – cchoe1 Nov 24 '17 at 06:26
  • `if ($PName='' OR ...` - You realize that you're actually _setting_ all the variables in that `if`-statement? You need `==` or `===` to _compare_ the variables. – M. Eriksson Nov 24 '17 at 06:26
  • You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries. Specially since you're not escaping the user inputs at all! – M. Eriksson Nov 24 '17 at 06:28
  • `mysqli_query($con, $insert_Product)` - Where do you set `$con`? – M. Eriksson Nov 24 '17 at 06:30
  • There are many issues and questions about this code. I'm quite sure that if you checked your servers error log, you would get a bunch of helpful error and warning messages. A good idea is also to turn `display_errors` on in your local PHP environment. Read more here: [How do I get PHP errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – M. Eriksson Nov 24 '17 at 06:33
  • my problem is solved thanx to all of you helping me.... thanx alot – Umair Jutt Nov 24 '17 at 11:57

2 Answers2

0

Try this Query..

       if(empty($P_Name) || empty($P_Description) || empty($P_Price) || empty($P_Quantity) || empty($PImage) || empty($CId)) 
                {
                    echo"<script>alert('please fill fields')</script>";
                }

                else {
                 $insert_Product = "INSERT INTO `product`(`P_id`, `P_Name`, `P_Description`, `Price`, `Quantity`, `C_Id`, `Image`) VALUES ('','$P_Name','$P_Description','$P_Price','$P_Quantity','$CId','$PImage')";
                 .
                 .
                 .
                 .

                 }

Hope this helps..

Raghav
  • 388
  • 1
  • 12
-1
This code is use to save image path into database.
  • In this first line get you image into file variable.
  • Second line use to get file extension.
  • Third line is used to change your image file name
  • Fourth line is use to move image into your given folder name
  • And the last line is getting image name with folder path

            $file      = $_FILES['image'];
            $extension = $file->getClientOriginalExtension();
            $imageName = 'anything'.time().'.'.$extension;
            $file->move(public_path('/foldername'), $imageName);
            $imageFile = '/foldername/'.$imageName;
    

Insert $imageFile into your database image column. It works for both.. image move to your folder and save image path into database.

  • You might want to mention that your code is actually using _Symfonys http-foundation_-library, which the OP never said is used. `$file->getClientOriginalExtension();` isn't a native PHP-method. Plus, this doesn't actually solve the OP's issues. – M. Eriksson Nov 24 '17 at 06:47