Gotharious Posted November 5, 2011 Share Posted November 5, 2011 Hello, I want to check when adding a new user, that if there are 2 records that has the same recruiter id, it's should give an error, if less, than it makes him complete the registration Also check if the National ID number is not assigned to another user, so if there is a record that has it, it should give and error, if it's unavailable, it should let him complete the registration here is my code register.html <html> <form action="post.php" method="post"> <table> <tr><td>First Name: <input type="text" name="fname" /></td></tr> <tr><td>Middle Name: <input type="text" name="mname" /></td></tr> <tr><td>Last Name: <input type="text" name="lname" /></td></tr> <tr><td>Mobile: <input type="text" name="mobile" /></td></tr> <tr><td>Telephone: <input type="text" name="tel" /></td></tr> <tr><td>Address: <input type="text" name="address" /></td></tr> <tr><td>Job Title: <input type="text" name="job" /></td></tr> <tr><td>Company: <input type="text" name="company" /></td></tr> <tr><td>National ID Number: <input type="text" name="nid" /></td></tr> <tr><td>Recruiter ID: <input type="text" name="recruiteris" /></td></tr> <tr><td>Email: <input type="text" name="email" /></td></tr> <tr><td>Password: <input type="password" name="password" /></td></tr> <tr><td>Type: <input type="text" name="type" /></td></tr> <tr><td><input type="submit" /></td></tr> </table> </form></html> post.php <?php $sql="INSERT INTO users (id,fname, mname, lname, mobile, tel, address, job, company, nid, recruiteris, email, password, type) VALUES ('','$_POST[fname]','$_POST[mname]','$_POST[lname]','$_POST[mobile]','$_POST[tel]','$_POST[address]','$_POST[job]','$_POST[company]','$_POST[nid]','$_POST[recruiteris]','$_POST[email]','$_POST[password]','$_POST[type]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; echo "<br />"; echo "<a href='http://waw-eg.com/admin/register.html'>Add More Client? Click Here</a><br/>"; echo "<a href='http://waw-eg.com/admin/Users.php'>View Users Click Here</a>"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/250528-check-if-record-exists/ Share on other sites More sharing options...
trq Posted November 6, 2011 Share Posted November 6, 2011 You'll need to execute a SELECT query first so that you can check for already existing records. Where exactly are you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/250528-check-if-record-exists/#findComment-1285374 Share on other sites More sharing options...
Gotharious Posted November 6, 2011 Author Share Posted November 6, 2011 I've tried that it was <?php $fetch_check=mysql_query("SELECT email, nid FROM users WHERE email = '$email' OR nid = 'nid'"); $count=mysql_num_rows($fetch_check); If($count>0); { echo 'Email or National ID already exist'; } else { //Insert query here } ?> but after that, it neither gave me an error or registered the user no matter if any existed or not Quote Link to comment https://forums.phpfreaks.com/topic/250528-check-if-record-exists/#findComment-1285376 Share on other sites More sharing options...
trq Posted November 6, 2011 Share Posted November 6, 2011 Why not post your actual code that isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/250528-check-if-record-exists/#findComment-1285377 Share on other sites More sharing options...
Gotharious Posted November 6, 2011 Author Share Posted November 6, 2011 That's the whole code of post.php <?php <?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $fetch_check=mysql_query("SELECT email, nid FROM users WHERE email = '$email' OR nid = 'nid'"); $count=mysql_num_rows($fetch_check); If($count>0); { echo 'Email or National ID already exist'; } else { $sql="INSERT INTO users (id,fname, mname, lname, mobile, tel, address, job, company, nid, recruiteris, email, password, type) VALUES ('','$_POST[fname]','$_POST[mname]','$_POST[lname]','$_POST[mobile]','$_POST[tel]','$_POST[address]','$_POST[job]','$_POST[company]','$_POST[nid]','$_POST[recruiteris]','$_POST[email]','$_POST[password]','$_POST[type]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; echo "<br />"; echo "<a href='http://waw-eg.com/admin/register.html'>Add More Client? Click Here</a><br/>"; echo "<a href='http://waw-eg.com/admin/Users.php'>View Users Click Here</a>"; mysql_close($con) } ?> ?> Quote Link to comment https://forums.phpfreaks.com/topic/250528-check-if-record-exists/#findComment-1285381 Share on other sites More sharing options...
trq Posted November 6, 2011 Share Posted November 6, 2011 Are you getting an error? What? You really need to ensure you sanitise your data before using it any queries as well. See mysql_real_escape_string and use it. Quote Link to comment https://forums.phpfreaks.com/topic/250528-check-if-record-exists/#findComment-1285395 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.