dongshi2836 2016-11-13 08:30
浏览 20
已采纳

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

Users can add, edit and delete content on my web page. People can edit the text and the image they upload. However, the image will not display if I only edit the text. When I edit the text but not the image, a white box is displayed where the image should appear. On the other hand, the image will appear if I only edit the photo but nothing else. Only the text will update when I try to edit both the image and text together.I want the user to be able to edit their text and image like they can on a profile page. Once the text and the image is edited, I want the old image to be deleted out of the folder. How can I edit the image and the text together? I am not getting any errors. Please help, I'm new to Php and MySQL. Thank you for your time. Update system This is the code:

<?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条回答 默认 最新

  • dongyong1897 2016-11-13 08:48
    关注

    What is happening is when you edit only the text, the query called also updates the image path but since you don`t add an image it will be NULL. One way is to build a conditional query inside the edit click

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体