Home > Enterprise >  I'm having issue when i submit my form data, Image is blank when submitted to the database afte
I'm having issue when i submit my form data, Image is blank when submitted to the database afte

Time:12-21

I'm trying to update my edit page but when i submit the form data, it will submit to the database but leaving the image empty or blank. i want the image to change only if a new image is selected when the form is submitted but the image should remain the same when not selected or changed. pls can someone help me.

Here's the error i'm getting: Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 in C:\xampp\htdocs\royalconcept\admin\edit_product.php:82 Stack trace: #0 C:\xampp\htdocs\royalconcept\admin\edit_product.php(82): mysqli_query(Object(mysqli), 'SELECT * FROM `...') #1 {main} thrown in C:\xampp\htdocs\royalconcept\admin\edit_product.php on line 82

Please can someone help me out?

Here's my form data:

      <div >
        <div >
          <?php include './message.php'; ?>

            <div >
              <div >
                <h5 >Edit Product</h5>
                  <?php
                  
                      include './config.php';

                      $id = $_GET['id'];

                      $sql = "SELECT * FROM `products` WHERE id = $id";
                      $result = mysqli_query($conn, $sql);
                      $row = mysqli_fetch_assoc($result);

                  ?>
                            
                    <!-- No Labels Form -->
                    <form action="./edit_product.php" method="POST"  enctype="multipart/form-data">

                      <input type="hidden" name="id" value="<?php echo $row['id']; ?>">

                      <div >
                          <label for="name" >Category</label>
                          <input type="text" name="category_name"  value="<?php echo $row['category_name'] ?>">
                      </div>

                      <div >
                          <label for="name" >Product Name</label>
                          <input type="text" name="name"  value="<?php echo $row['name'] ?>">
                      </div>

                      <div >
                          <label for="price" >Price</label>
                          <input type="text" name="price"  value="<?php echo $row['price'] ?>">
                      </div>

                      <div >
                          <label for="details" >Details</label>
                          <textarea  rows="8" name="details"><?php echo $row['details'] ?></textarea>
                      </div><br><br>

                      <div >
                          <input type="hidden" name="oldimage" value="<?= $photo; ?>">
                          <label for="customFile" >Photo</label>
                          <input type="file" name="photo" >
                          <img src="uploads/<?php echo $row['photo'] ?>" width="150" >
                      </div><br><br>

                      <div >
                          <input type="submit" name="update"  value="Update Product">
                          <input type="reset" name="clear"  value="Reset Product">
                      </div>
                    </form><!-- End No Labels Form -->

              </div>
            </div>

        </div>

      </div>

And here's my PHP Code:

<?php
  include 'config.php';

  if(isset($_POST['update'])){
      //text data variables
      $id            = mysqli_real_escape_string($conn, $_POST['id']);
      $category_name = mysqli_real_escape_string($conn, $_POST['category_name']);
      $name          = mysqli_real_escape_string($conn, $_POST['name']);
      $price         = mysqli_real_escape_string($conn, $_POST['price']);
      $details       = mysqli_real_escape_string($conn, $_POST['details']); 
      $photo         = mysqli_real_escape_string($conn, $_FILES['photo']['name']);

        if(isset($_FILES['photo']['name']) && ($_FILES['photo']['tmp_name']!="")){
          $newimage=$_FILES['photo']['name'];

          $temp_name  = $_FILES['photo']['tmp_name'];
          unlink($oldimage);

          move_uploaded_file($temp_name,"uploads/$newimage");
        }
        else {
            $oldimage = $newimage;
        }

    // form validation: ensure that the form is correctly filled
    $sql = "UPDATE `products` SET `category_name`='$category_name', `name`='$name', `price`='$price', `details`=
    '$details', `photo`='$photo' WHERE id=$id ";

    $result = mysqli_query($conn, $sql);

      if($result)
      {

        $_SESSION['message'] = "Product Updated Successfully!";

        header ("Location: edit_product.php?msg= Product Updated Successfully") ;
        exit(0);

      } else {

        $_SESSION['message'] = "Sorry!...Something went wrong!";

        header ("Location: edit_product.php?msg=Something went Wrong!") ;
        exit(0);
      
      }
  
    }

?>

Here's the SELECT CODE which i used to submit the product to the database

CodePudding user response:

The MySQL error message claims that there is an issue with the "SELECT" statement, but the code snippet you are showing does not contain such a statement. The error must be at another location. Where is the "SELECT" used in your code?

CodePudding user response:

Here's the SELECT CODE which i used to add/submit the product to the database

  • Related