0

I was wondering how to construct the correct syntax for the if-else statement, or if there's something missing in my code.

<?php
    include "../dbcon.php";
    session_start();
    ob_start();
    $sql = mysqli_query($con,"SELECT * FROM clientdocuments  WHERE docID = $_POST[docID]");
    $rows = mysqli_fetch_array($sql, MYSQLI_ASSOC);
    //IF CSS input value is filled
    if(!empty($_POST)){
        $output = '';  
        $message = '';  
        $docID = mysqli_real_escape_string($con, $_POST["docID"]);
        $docSIG_Contract = mysqli_real_escape_string($con, $_POST["docSIG_Contract"]); 
        //I don't get what this "if(isset($_POST["docID"])){" purpose (Sorry very new to php)
        if(isset($_POST["docID"])){
            if (!empty($docID)) {
                $query = "UPDATE clientdocuments(docID, docSIG_Contract) VALUES('$docID', '$docSIG_Contract');"; //UPDATE ONCE docID ALREADY EXIST ON THE DATABASE 
            } else {
                $query = "INSERT INTO clientdocuments(docID, docSIG_Contract) VALUES('$docID', '$docSIG_Contract');"; //INSERT IF THE docID doesn't exist yet
            }
            $str = mysqli_query($con,$query);
            if(!$str){
                echo 'FAILED';
            }
        }else{
            header('HTTP/1.1 500 Internal Server Booboo');
            header('Content-Type: application/json; charset=UTF-8');
        } 
    }
?>
GYaN
  • 2,327
  • 4
  • 19
  • 39
wannabes
  • 25
  • 6

2 Answers2

1
  1. remove this if statment: if (!empty($docID)) {

  2. Make sure that u send with each post update the "docID" value

Swissa Yaakov
  • 186
  • 3
  • 15
0

if(isset($_POST["docID"])) statement checks to see whether the input with the name docID has a value.

if(!empty($_POST)) I am not sure whether this will work, my guess is that you are trying to check whether the request method is POST (if the save button was clicked). For this I use if ($_SERVER['REQUEST_METHOD'] === 'POST') {

I would then check to see whether docID has a value ie (isset($_POST["docID"])) OR (!empty($_POST["docID"])) Difference between isset and !empty What's the difference between 'isset()' and '!empty()' in PHP?

If there is a value, $query would be the update statement If there is no value $query would be the insert statement In this situation don't enter the DocID value (because then it would always be 0 which will also cause errors)

Hope that makes sense!

user7423780
  • 23
  • 1
  • 5
  • What's the correct syntax to know if the inputted value is already present in the database? so I can do the update query? – wannabes Apr 12 '18 at 04:51
  • I am not sure what you mean. If you want to check whether the ID is in the database, then you have to run a select statement. You would probably be doing this when the page originally loads, so I would just check if the DocID input field has a value. Again depending on how you populate the DocID field when it is a new record, you will need to use isset or !empty. If DocID is 0, then I would use !empty. If you pass through null, then isset is fine. I prefer to use !empty – user7423780 Apr 12 '18 at 05:35
  • Also worth noting the SQL format for your update statement is wrong. https://www.w3schools.com/sql/sql_update.asp – user7423780 Apr 12 '18 at 05:40