mysql - PHP - mysqli - check the table if a value is already inserted -


i trying check if ref number added when creating new mysql row in use. don't have problems in adding new row however, script not check database first.

    if ($_post['add_new_bus']){     if (($_post['add_ref'] != "")&&($_post['add_name'] != "")&&($_post['add_address'] != "")&&($_post['add_area'] != "")){         $add_ref = $_post['add_ref'];         $add_name = $_post['add_name'];         $add_address = $_post['add_address'];         $add_area = $_post['add_area'];         $chech_sql = "insert `details` (`ref`) values ('$add_ref')";    if (!($conn->query($chech_sql))) {         echo "ref in use";     }else{         mysqli_query($conn, "insert `details` (`ref`, `name`, `address`, `area`) values ('$add_ref', '$add_name', '$add_address', '$add_area')");         echo "<p style='float:right;'>" . $_post['add_name'] . " " . "has been added register ref number:" . " " . $_post['add_ref'] . "</p>";         }     } 

any idea how check if ref number in use?

for giving correct idea how it, please check below code:-

<?php if (isset($_post['add_new_bus']){     if (($_post['add_ref'] != "") &&($_post['add_name'] != "")&&($_post['add_address'] != "")&&($_post['add_area'] != "")){         $add_ref = $_post['add_ref'];         $add_name = $_post['add_name'];         $add_address = $_post['add_address'];         $add_area = $_post['add_area'];         $chech_sql = "select add_ref details add_ref = '".$add_ref."'";         $result = $conn->query($chech_sql);    if (mysqli_num_rows($result) > 0) {         echo "ref in use";     }else{         mysqli_query($conn, "insert `details` (`ref`, `name`, `address`, `area`) values ('$add_ref', '$add_name', '$add_address', '$add_area')");         echo "<p style='float:right;'>" . $_post['add_name'] . " " . "has been added register ref number:" . " " . $_post['add_ref'] . "</p>";         }     } } ?> 

note:- checking variables value , other things you. because have them in code.thanks.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -