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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历