在Php和Mysql中同时更新图像和文本

时间:2016-11-13 08:30:03

标签: php mysqli

用户可以在我的网页上添加,编辑和删除内容。人们可以编辑他们上传的文本和图像。但是,如果我只编辑文本,则不会显示图像。当我编辑文本但不编辑图像时,会显示一个白色框,其中应显示图像。另一方面,如果我只编辑照片而没有其他内容,则会出现图像。当我尝试同时编辑图像和文本时,只有文本会更新。我希望用户能够在个人资料页面上编辑他们的文本和图像。编辑完文本和图像后,我希望将旧图像从文件夹中删除。如何一起编辑图像和文本?我没有收到任何错误。请帮助,我是Php和MySQL的新手。感谢您的时间。 Update system这是代码:

<?php
include "connection.php";
$vid="";
$vname="";
$vprice="";


if(isset($_POST["button_add"])){
$product_name = $_POST["product_name"];
$product_price = $_POST["product_price"];
$product_picture = $_FILES["product_picture"]["name"];

    $qry = mysqli_query($con, "INSERT INTO table_product values('','$product_name','$product_price','$product_picture')") or die("Can not query database" );
    if($qry){
        $target_dir = "picture/";
        $target_file = $target_dir . basename($_FILES["product_picture"]["name"]);
       $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(move_uploaded_file($_FILES["product_picture"]["tmp_name"],
$target_file)){
    echo"file uploaded";
}
 else{
    echo "Upload fail"; 
}

    }   
}
else if(isset($_POST["button_edit"])){
     $product_name = $_POST["product_name"];
     $product_price = $_POST["product_price"];
     $product_id = $_POST["product_id"];

     if(isset($_FILES["product_picture"]["name"])){
$product_picture = $_FILES["product_picture"]["name"];
    $qry = mysqli_query($con,"Update table_product Set product_name='$product_name', product_price='$product_price', product_picture='$product_picture' Where product_id='$product_id'");
        $target_dir = "picture/";
        $target_file = $target_dir . basename($_FILES["product_picture"]["name"]);
       $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    move_uploaded_file($_FILES["product_picture"]["tmp_name"],$target_file);
    }  
    else{
 $qry = "Update table_product Set product_name='$product_name', product_price='$product_price' Where product_id='$product_id'";
}
$qry_update = mysqli_query($con,$qry);
    }

    if(isset($_GET["delete"])){
$qry = mysqli_query($con, "Delete From table_product Where product_id='".$_GET["delete"]."'" );
    if($qry){
        @unlink("picture/".$_GET["picture"]);
    }
    }
else if(isset($_GET["edit"])){
    $qry = mysqli_query($con,"Select * From table_product Where product_id='".$_GET["edit"]."'");
    while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
        $vid=$row["product_id"];
        $vname=$row["product_name"];
        $vprice=$row["product_price"];
    }
}
?>

 <!DOCTYPE html>
<html>
<head>
<title>Product</title>
</head>
<body>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method="post" enctype="multipart/form-data" >
    <table>
        <tr>
            <td>Product ID</td>
            <td><input type="text" name="product_id" value="<?php echo $vid;?>"></td></tr>
        <tr><td>Product Name</td>
        <td><input type="text" name="product_name"  value="<?php echo $vname;?>"></td></tr>
        <tr><td>Product Price</td>
        <td><input type="text" name="product_price"  value="<?php echo $vprice;?>"></td></tr>
        <tr><td>Product Picture</td>
        <td><input type="file" name="product_picture"></td></tr>
        <tr><td colspan="2">
        <input type="submit" name="button_add" value="Add">
        <input type="submit" name="button_edit" value="Edit"></td></tr> </table>
</form>
<table border=1>
    <tr><th>product ID</th><th>product Name</th>
    <th>product price</th><th>product image</th>  <th>Action</th></tr>
    <?php
    $qry =mysqli_query($con, "Select * From table_product");
    while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
        echo '<tr><td>'.$row["product_id"].'</td>';
        echo '<td>'.$row["product_name"].'</td>';
        echo '<td>'.$row["product_price"].'</td>';
        echo '<td><img src="picture/'.$row["product_picture"].'" style=width:100px;height:xpx;"/></td>';

        echo '<td><a href="?edit='.$row["product_id"].'">Edit</a>  |<a href="?delete='.$row["product_id"].'&picture='.$row["product_picture"].'">Delete</a></td></tr>';



    }



    ?>
</table>
<br><br><br>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

当您仅编辑文本时,发生的情况是,调用的查询也会更新图像路径,但由于您不添加图像,因此它将为NULL。 一种方法是在编辑单击

中构建条件查询
if(isset($_FILES["product_picture"]["name"]))
{
$product_picture = $_FILES["product_picture"]["name"];
$sql = "Update table_product Set product_name='$product_name', product_price='$product_price', product_picture='$product_picture' Where product_id='$product_id'";
target_dir = "picture/";
    $target_file = $target_dir . basename($_FILES["product_picture"]["name"]);
   $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
move_uploaded_file($_FILES["product_picture"]["tmp_name"],$target_file);
}
else{
 $sql = "Update table_product Set product_name='$product_name', product_price='$product_price' Where product_id='$product_id'"
}

qry = mysqli_query($con,$sql);