PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 27, 2022

[FIXED] Updating an image and text at the same time in Php and Mysql

 January 27, 2022     mysqli, php     No comments   

Issue

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>

Solution

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


Answered By - Mihai
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing