samedi 28 novembre 2020

If else statement does not procced to else when condition is false in PHP?

why my if else statement does not procced to else when my condition is false? help me what is wrong with my code. the first sql statement work to verify the username is already register but the second sql statement does not work which is to insert the data if the condition are false. here is my code:

<?php

            if (isset($_POST['btn_add'])) 
            {
                $docFname = $_POST['docFname'];
                $docMname = $_POST['docMname'];
                $docLname = $_POST['docLname'];
                $docGender = $_POST['docGender'];
                $docType = $_POST['docType'];
                $docAge = $_POST['docAge'];
                $docNationality = $_POST['docNationality'];
                $docAddress = $_POST['docAddress'];
                $docContact = $_POST['docContact'];
                $docDateAdded = date('Y-m-d');
                $docUsername = $_POST['docUsername'];
                $docUserpass = $_POST['docUserpass'];

                $vald = "SELECT docUsername FROM doctors WHERE docUsername='" . $docUsername . "'";
                $result = $conn->query($vald);
                if($result->num_rows>0)
                {
                    while($row = $result->fetch_assoc())
                    {
                        $rigestered = $row['docUsername'];
                    }
                    if ($docUsername == $rigestered) 
                    {
                        echo "username already taken!";
                    }
                    else
                    {
                        $ins = "INSERT INTO doctors (docFname, docMname, docLname, docGender, docType, docAge, docNationality, docAddress, docContact, docDateAdded, docUsername, docUserpass) VALUE ('$docFname','$docMname','$docLname','$docGender','$docType','$docAge','$docNationality','$docAddress','$docContact','$docDateAdded','$docUsername','$docUserpass')";
                        if($conn->query($ins)===TRUE)
                        {
                            header('location:blank_page.php');
                        }
                        else
                        {
                            echo "SQL Query: " . $ins . "->Error: " . $conn->error;
                        }
                    }
                }

            }

            ?>

Aucun commentaire:

Enregistrer un commentaire