Home > Net >  PHP SQL Insertion not showing any error and its not working
PHP SQL Insertion not showing any error and its not working

Time:10-28

Hi i did some validation for my sql insert but its not working and its not showing any error. Please help thanks a lot

My Code

$insertValidationSQL = "SELECT * FROM $Hospname where NRIC = '" . $_SESSION['NRIC'] . "'";
$insertValidationresult = @mysqli_query($this->conn, $insertValidationSQL);

if(empty(insertValidationresult)){
            $insertToHospitalSQL = "INSERT INTO $Hospname SELECT * FROM patients where NRIC = '" . $_SESSION['NRIC'] . "'";
            $insertToHospitalresult = @mysqli_query($this->conn, $insertToHospitalSQL);
        }

I tried another way and its not working as well.

This is the code

if(empty(mysqli_fetch_assoc(insertValidationresult))){
            $insertToHospitalSQL = "INSERT INTO $Hospname SELECT * FROM patients where NRIC = '" . $_SESSION['NRIC'] . "'";
            $insertToHospitalresult = @mysqli_query($this->conn, $insertToHospitalSQL);
        }

Thanks For helping.

CodePudding user response:

I think you need to do like below, it will take you to if conditions if records are 0 in first query.

$insertValidationSQL = "SELECT * FROM $Hospname where NRIC = '" . $_SESSION['NRIC'] . "'";
$insertValidationresult = @mysqli_query($this->conn, $insertValidationSQL);

if($insertValidationresult->num_rows==0){
            $insertToHospitalSQL = "INSERT INTO $Hospname SELECT * FROM patients where NRIC = '" . $_SESSION['NRIC'] . "'";
            $insertToHospitalresult = @mysqli_query($this->conn, $insertToHospitalSQL);
        }

issue with your code is you are checking like: if(empty($insertValidationresult)) but $insertValidationresult will always return array with result info so your condition is wrong, also you are missing $ before insertValidationresult in your mysqli_fetch_assoc condition.

  • Related