Jump to content

Check if Record Exist


dadedidhodong

Recommended Posts

I have this HTML FORM

<form action="check_record.php" method="post">
<input type="text" placeholder=""Last Name" name="lname"></input>
<input type="text" placeholder=""First Name" name="fname"></input>
SUBMIT or CLEAR
</form>

once the user submits the form I point it to the check_record.php to check if the record already exist or not

<?php

//check_record.php

session_start();
    $connection = mysql_connect("localhost", "root", "");
    $db = mysql_select_db("web", $connection);

    $sql="SELECT * FROM `web`.`tbl_student` WHERE lname = '".$_POST["lname].'", fname = '".$_POST["fname"]."'
   
    $result = mysql_query($sql);

    $row = (I forgot the next codes :\)

header("Location:add_record.php");
?>
<?php

//add_record.php

session_start();
    $connection = mysql_connect("localhost", "root", "");
    $db = mysql_select_db("web", $connection);

    $sql="INSERT INTO `web`.`tbl_student` (`lname`, `fname`) VALUES ('".$_POST["lname].'", '".$_POST["fname"]."')";
    
    $result = mysql_query($sql);

header("Location:index.php");

?>

I am totally new to PHP, my problem here is, once the user submits the form, it will check if the last name or the first name already exist, if it exist it will not allow to add the record and displays a message "Record alreadys exist" if it's not an existing record, it will display a message saying "Record saved." I have no problem with adding, updating, or deleting a record. During our lab activity I figured out how to check if the record already exist but I'm not sure about because after the record has been check and it's not existing it will add though but the last name and the first name are blank :/ please help

Link to comment
https://forums.phpfreaks.com/topic/281495-check-if-record-exist/
Share on other sites

That code is a car crash.  The problem that relates to your question is two part: part one is due to you not having any conditional checking on the header to load the add record page, and part two is that you have no persistence for your variables moving on from the check record page. I don't even know why you have the add record script on a different page anyway, and even if there was a reason for it I can't think why you would redirect to load it rather than just use an include.  I'm not even going to get started on the db transaction stuff.

function makePass($word=''){
$dbSalt = '$2a$07.substr(hash('whirlpool',$word),0,22);
$dbPass = crypt($word, $dbSalt);
return substr($dbPass,12);
}

 

I'm not sure but I guess this code is for password. My problem is, how can I make the program to prohibit adding records that does exist (i.e. Last Name and First Name).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.