0
  include_once 'db.php';

  if(isset($_POST['submit'])) {
     $stmt = $conn->prepare("UPDATE companies SET name =?, imgname = ?, mime = ?, img = ?, details= ? WHERE id = ?");
     $stmt->bind_param("sssbsi", $name, $imgname, $type, $img, $details, $id);

      $name = $_POST['name'];
      $id = $_POST['id'];
      $details = $_POST['details'];
      $imgData = $_FILES['img']['tmp_name'];
      $imgname = $_FILES['img']['name'];
      $type = $_FILES['img']['type'];
      $img = base64_encode(file_get_contents(addslashes($imgData)));
      $stmt->execute();


      if(!$stmt->execute() === TRUE) {
         die('falied:' . htmlspecialchars($stmt->error));
      } else {
         echo "seccuess";
       }

       $stmt->close();
       $conn->close();
       }

Prevent form process from updating database with empty values from form input. update only coulmns that have value and not empty.

Shadow
  • 33,525
  • 10
  • 51
  • 64
abood alwa7sh
  • 63
  • 1
  • 11
  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Oct 07 '20 at 01:07
  • It's not necessary to do `=== TRUE` on a function that returns a boolean. It's especially strange to see `if (!x === TRUE)`. – tadman Oct 07 '20 at 01:07
  • Note: This is what an ORM does for you. Have a look at [RedBeanPHP](https://redbeanphp.com/), [Doctrine](http://www.doctrine-project.org/), or [Eloquent](https://laravel.com/docs/master/eloquent) for examples. – tadman Oct 07 '20 at 01:08
  • 1
    Does this answer your question? [Updating database using PHP without empty values from HTML form](https://stackoverflow.com/questions/59326574/updating-database-using-php-without-empty-values-from-html-form) – msg Oct 07 '20 at 01:09
  • What have you tried to achieve that? – Nico Haase Oct 07 '20 at 08:16

1 Answers1

0

You want to prevent empty entries from updating your database, right? In this case, just check if the fields are empty, then redirect the user to the page again, preventing this action from being performed.

<?php


include_once 'db.php';

if(isset($_POST['submit'])) {

    $name = $_POST['name'];
    $id = $_POST['id'];
    $details = $_POST['details'];
    $imgData = $_FILES['img']['tmp_name'];
    $imgname = $_FILES['img']['name'];
    $type = $_FILES['img']['type'];
    $img = base64_encode(file_get_contents(addslashes($imgData)));
    $stmt->execute();


    //Checks if the user did not type anything in any of the fields, then redirects to the home page
    if(empty($name) || empty($id) || empty($details) || empty($imgData) || empty($imgname) || empty($type) || empty($type) || empty($img)){
        header("Location: index.php");
        exit();
    }



    $sql = "UPDATE companies SET name = :name, imgname = :imgname, mime = :mime, img = :img, details = :details WHERE id = :id";
    $result = $conn->prepare($sql);
    $result->execute(array(
        ":name" => $name,
        ":imgname" => $imgname,
        ":mime" => $type,
        ":img" => $img,
        ":details" => $details,
        ":id" => $id
    ));


    if($result == false){
        echo "Failed!";
    }
    else{
        echo "Success!";
    }


    $conn->close();

}
Anne Rebb
  • 185
  • 1
  • 9
  • i actually want the database to update even if there was an empty entry. but dont update the table colmuns if they have empty entry. – abood alwa7sh Oct 07 '20 at 01:13