mysqli - Updating an image and text at the same time in Php and Mysql -


users can add, edit , delete content on web page. people can edit text , image upload. however, image not display if edit text. when edit text not image, white box displayed image should appear. on other hand, image appear if edit photo nothing else. text update when try edit both image , text together.i want user able edit text , image can on profile page. once text , image edited, want old image deleted out of folder. how can edit image , text together? not getting errors. please help, i'm new php , mysql. thank time. update system 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 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' 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' product_id='$product_id'"; } $qry_update = mysqli_query($con,$qry);     }      if(isset($_get["delete"])){ $qry = mysqli_query($con, "delete table_product product_id='".$_get["delete"]."'" );     if($qry){         @unlink("picture/".$_get["picture"]);     }     } else if(isset($_get["edit"])){     $qry = mysqli_query($con,"select * table_product 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 * 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> 

what happening when edit text, query called updates image path since don`t add image null. 1 way build conditional query inside 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' 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' product_id='$product_id'" }  qry = mysqli_query($con,$sql); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -