Click here to Skip to main content
15,504,830 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this code that work fine , it add data and image but I want to edit data the style
how can i Edit Data the same way:
PHP
<pre>
<?php
include('./pro_crud/config.php');

$sql_cat = "SELECT * FROM category";
$result = $con->query($sql_cat);

if(isset($_POST['btn_edit'])) {
    $name = $_POST["pdct_name"];
    $price = $_POST["pdct_price"];
    $qty = $_POST["pdct_qty"];
  //  $filename = $_FILES["pdct_img"];
    $co = $_POST["pdct_code"];
    $cat = $_POST["pdct_cat"];
    $cat_name = "";
    $sql_query2 = "SELECT * FROM category where cat_id =$cat;";
    $result2 = $con->query($sql_query2);
    if($result2->num_rows > 0)
    {
        while($row = $result2->fetch_assoc()) 
        {
          $cat_name = $row['cat_name'];
        }
      }
    $target_dir = "../images/".$cat_name."/";
    $target_file = $target_dir.basename($_FILES["pdct_img"]["name"]);

    $uploadok = 1;
    $imagFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

    //check if this file is image or not
    $check= getimagesize($_FILES["pdct_img"]["tmp_name"]);
    if($check !== false)
    {
      echo "File is an image";
      $uploadok = 1;
    }
    else {
      echo "File is not image";
      $uploadok = 0;
    }
    // already exists
    if(file_exists($target_file))
    {
      echo "file already exists";
      $uploadok = 0;
    }
    
    // if($_FILES["pdct_img"]["size"] > 900000)
    // {
    //   echo "sorry, image size is too large ";
    //   $uploadok = 0;
    // }

    if($imagFileType !='jpg' && $imagFileType !='png' && $imagFileType !='jpeg') 
    {
      echo "Sorry your image not jpg or png";
      $uploadok = 0;
    }
    if($uploadok == 0)
    {
      echo 'لم يتم تحميل الصوره';
    }
    else 
    {
      if(move_uploaded_file($_FILES["pdct_img"]["tmp_name"],$target_file))
      {
        $target_d = "images/".$cat_name."/";
        $target_f = $target_d.basename($_FILES["pdct_img"]["name"]);

        $sql = "insert into product values(0,'$name','$price','$qty','$target_f','$co','$cat')";
        if($con->query($sql)){
            echo '<script>alert("تمت أضافة بنجاح");</script>';
            header("location:viewProducts.php");
        }
        else{
            echo $con->error;
        }
      }
    }


} 
?>
<!DOCTYPE html>
<html>
  <head>
    <title>تعديل منتج </title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  </head>
  <body class="bg-dark"> 
    <div class=" card text-center" style="padding:15px;" >
      <h4>تعديل بيانات منتج </h4>
    </div><br><br> <br><br> <br>
    <div class="container ">
      <form method="POST"  enctype="multipart/form-data">
        <div class="form-group text-right text-light">
          <label for="name">:رقم المنتج</label>
          <input type="text" class="form-control text-right" name="pdct_id" placeholder="رقم المنتج " required="">
        </div>
        <div class="form-group text-right text-light">
          <label for="name">:اسم المنتج</label>
          <input type="text" class="form-control text-right" name="pdct_name" placeholder="مانجا ناروتو" required="">
        </div>
        <div class="form-group text-right text-light">
          <label>:سعر المنتج</label>
          <input type="number" class="form-control text-right" name="pdct_price" placeholder="50" required="">
        </div>
        <div class="form-group text-right text-light">
          <label>:الكمية </label>
          <input type="number" class="form-control text-right" name="pdct_qty" placeholder="80" required="">
        </div>
        <div class="form-group text-right text-light">
          <label>:الصوره </label>
          <input type="file" class="form-control text-right" name="pdct_img" >
        </div>
        <div class="form-group text-right text-light">
          <label for="username">:التصنيف  </label>
          <select class="form-control text-right" name="pdct_cat">
              <option>اختار تصنيف</option>
              <?php
              if($result->num_rows > 0)
              {
                  while($row = $result->fetch_assoc()) 
                  {
                      echo '<option value="'.$row["cat_id"].'">'.$row["cat_name"].'</option>';
                  }
              }
              ?>
          </select>
        </div>
        <div class="form-group text-right text-light">
          <label for="username">:رمز المنتج </label>
          <input type="text" class="form-control text-right" name="pdct_code" placeholder=" 80  " required="">
        </div>

        <div class=" text-right form-group text-light">
          <a href="../pro.php"   class="btn btn-light btn-right">إلغاء</a>
          <input type="submit" name="btn_edit" class="btn btn-danger" style="float:right;" value="إضافة"></a>
        </div>
      </form>
    </div>
  </body>
</html>



What I have tried:

ever code the i try will not edit the image only edit the text data
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900