php - Why is my nested IF condition not working? -
i want check if record exists , if go page a, otherwise go page b. goes page whether there record or not.
<?php $ini = parse_ini_file("../phpconfig.ini"); $conn = mysqli_connect($ini['hostaddress'], $ini['username'], $ini['password'], $ini['databasename']); if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $options = ['cost' => 10,]; $number = mysqli_real_escape_string($conn, $_post['number']); $password = password_hash((mysqli_real_escape_string($conn, $_post['password'])), password_bcrypt, $options); $sql = "insert usertemp (employee_number, password) values ('$number', '$password')"; if (mysqli_query($conn, $sql)) { $sql2 = "select exists(select 1 employee number = '$number')"; $row2 = mysqli_query($conn, $sql2); if (mysqli_num_rows($row2) > 0) { //has record in employee table header("location: display_createaccount_a.php"); } else { //no record in employee table header("location: display_createaccount_b.php"); } } else { echo "error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?>
your select exists(...)
going return 1 row true or false.
in case, read first index of result , use conditional value.
or better yet, use limit
instead , keep app logic is:
$sql2 = "select * employee number = '$number' limit 1";
also, recommend checking out php documents on prepared statements. safer way of querying mysql.
Comments
Post a Comment