图像名称未使用PHP

时间:2017-11-24 06:17:19

标签: php html css

Mysql数据库属性图像名称和类型

`Image varchar256`

我试图在mysql中上传图片名称,但问题是图片是按照要求移动到文件夹但是图片名称没有显示在数据库中。请帮助...

<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>

将图像移动到文件夹但图像名称未在数据库中显示的示例

<?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;
        }
    }
}
?>

我如何解决我的问题?

2 个答案:

答案 0 :(得分:0)

尝试此查询..

       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')";
                 .
                 .
                 .
                 .

                 }

希望这会有所帮助..

答案 1 :(得分:-1)

This code is use to save image path into database.
  • 在第一行中,将图像转换为文件变量。
  • 第二行用于获取文件扩展名。
  • 第三行用于更改图像文件名
  • 第四行用于将图片移动到您指定的文件夹名称
  • 最后一行是获取带文件夹路径的图像名称

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

将$ imageFile插入数据库图像列。 它适用于..图像移动到您的文件夹并将图像路径保存到数据库。