Aisha_sg 0 Newbie Poster

hey I'm beginner learning php have hard time to Insert data to product in database with image because the category I did make it work but now I need to update data the same style that I add it with

the Insert Code:

<?php
include('./pro_crud/config.php');

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

if(isset($_POST['btn_insert'])) {
    $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_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_insert" class="btn btn-danger" style="float:right;" value="إضافة"></a>
        </div>
      </form>
    </div>
  </body>
</html>

How can i edit data with image without affect the text data?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.