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

Edited by Zane
Link to comment
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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.