Search the Community
Showing results for tags 'check existing record'.
-
This code is to let user add a new record <form action="processAdd.php" method="post"> <tr><td></td><td><input type="text" placeholder="Last Name" name="lname" value="" required></td></tr></br> <tr><td></td><td><input type="text" placeholder="First Name" name="fname" value="" required></td></tr></br> <tr><td></td><td><input type="text" placeholder="Course" name="course" value="" required></td></tr></br> <tr><td colspan=2><input type="submit" name="submit" value="Submit"></td></tr> </form> The codes below is where the user inputs are being processed. But I don't want a duplicate record of the last name and the first name. I can always do that by altering the table I've created in the database and set it to UNIQUE so that no similar record can be inserted. But what I wanna do is when the user inputs a similar records (i.e. last name and firt name) it will display an error message. I don't know what's wrong with my codes, it doesn't display a syntax error though when I run it, but it keeps on adding similar records on my database. I've tried the <?php session_start(); $dbconnect = mysql_connect("localhost","root",""); $db = mysql_select_db("web", $dbconnect); $sql = mysql_query("SELECT * FROM tbl_student WHERE stud_laneme = '".$_POST["lname"]."' AND stud_fname = ".$_POST["fname"]."',"); $result = mysql_query($sql); if(mysql_num_rows($result) > 0) { echo "Record already exists"; } else { $sql="INSERT INTO `web`.`tbl_student`( `stud_id`, `stud_lname`, `stud_fname`, `stud_course` ) VALUES( NULL, '".$_POST["lname"]."', '".$_POST["fname"]."', '".$_POST["course"]."' )"; $result = mysql_query($sql); header("location:displayRecord.php"); } ?>
- 4 replies
-
- php
- check existing record
-
(and 1 more)
Tagged with: