Jump to content

If empty help and check if user exists.


Anders Moen

Recommended Posts

As the topic says, I wanna make a message if not all required fields are written in. And I also wanna make so they can't register the same user twice.If anyone can help, here's the code:

<?php if($_POST['submit']) {$con = mysql_connect("localhost","username","password");mysql_select_db ("database_name",$con) or die ("ERROR!");if(empty($bruker)) { // If I use || or OR and put on more fields there, I just get an error message :S (I can't say it cause I don't have it right now). But I want all required fields to have their own "personal" message if they haven't been filled out. die ('Fill in all required fields.');}$bruker = htmlspecialchars(strip_tags($_POST['bruker']));$pass = htmlspecialchars(strip_tags($_POST['pass'])); $mail = htmlspecialchars(strip_tags($_POST['mail']));$age = htmlspecialchars(strip_tags($_POST['age']));$interests = htmlspecialchars(strip_tags($_POST['interests']));$query = mysql_query("INSERT INTO Members (bruker, pass, mail, age, interests) VALUES ('$bruker', '$pass', '$mail', '$age', '$interests')");}if($query){ echo("<table><tr><td><strong>Username</strong></td><td>$bruker</td></tr><tr><td><strong>Mail</strong></td><td>$mail</td></tr><tr><td><strong>Age</strong></strong></td><td>$age</td></tr><tr><td><strong>Interests</strong></td><td>$interests</td></tr></table>"); } else { echo(" <form action=\"\" method=\"post\"> <p>Username:<br /><input type=\"text\" size=\"40\" name=\"bruker\" /></p><p>Password:<br /><input type=\"password\" size=\"40\" name=\"pass\" /></p> <p>E-mail:<br /><input type=\"text\" size=\"40\" name=\"mail\" /></p><p>Age:<br /><input type=\"text\" size=\"40\" name=\"age\" /></p><p>Interests<br /><textarea rows=\"3\" name=\"interests\"></textarea></p><p><input type=\"submit\" value=\"Register \" name=\"submit\" onmouseover=\"validate()\" /></p> </form> "); }?>

For the check if user exists, the username field in database, is called "bruker" just so you know :)

Link to comment
Share on other sites

Are you saying that the following produces errors?:

if(empty($bruker) || empty($pass) || empty($mail))

As for checking if a username already exists, you might set the username column as an identity column which doesn't allow duplicates. Then, when you try to INSERT a record with a username that already exists in the database, the INSERT will fail. You could check after you attempt the INSERT whether an error was returned or you might be able to check how many records were affected - a successful INSERT would have affected 1 record wereas a failed INSERT would be 0 records. Alternatively, you could run a SELECT against the table for a record with a particular username. If the SELECT returns nothing, then you know that there isn't already a record in the database with that username and you can proceed with the INSERT.If you were using stored procedures, you could probably take care of this with one call to the database.

Link to comment
Share on other sites

Instead of using die() you can use one of these:

header("Location: index.php?user=$bruker&interest=$interest&...&msg=Fill in all fields");..header("Location: index.php?user=$bruker&interest=$interest&...&msg=That username is taken");...// Above the form putif ($_GET['msg']) {	  echo '<span style="color #00F">'.$_GET['msg']."</span><br />\n";}// In the form use (replace field with the fieldname<input type="text" name="field" value="<?php echo $_POST['field'];?>" />

or yoy can use this (it looks better than die, but not as good as the one above)

echo '<script type="text/javascript">'echo 'alert("Fill in all fields"); history.back();"echo '</script>';exit();...echo '<script type="text/javascript">'echo 'alert("The username is taken"); history.back();"echo '</script>';exit();

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...