PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
EDIT: Look att the bottom of this page to see updated code
I've got this code for my registration page, but as it is now, when i get errors (when username is empty, password empty, passwords didn't match etc..), they doesn't show up at all, and if I don't get any errors, i get a blank page, and the mysql isn't processed. Here's my "long" code:
register.php:
<?php
require ("config.php");
if(isSet($_SESSION['online']) && $_SESSION['online'] == "yes") {
header("Location: index.php");
}
// no need to add "else {" since once header() is excetuted, php stops and redirects
if(isSet($_POST['submit_reg']) && !empty($_POST['submit_reg'])) {
$errorMsg = null;
if(empty($_POST['username']) || !isSet($_POST['username'])) {
$errorMsg .= "You must enter your username\n";
}
if(empty($_POST['password']) || !isSet($_POST['password'])) {
$errorMsg .= "You must enter a password";
}
if(empty($_POST['retypepass']) || !isSet($_POST['retypepass'])) {
$errorMsg .= "You must confirm your password";
}
if($_POST['password'] !== $_POST['retypepass']) {
$errorMsg .= "You password do not match";
}
if(!empty($errorMsg)) {
echo_header();
$errorMsg;
echo_footer();
exit(); // exits php, you won't need to use a else {
}
// done with validation
$query1 = "SELECT nick FROM keeper WHERE nick = '{$_POST['username']}'";
$result1 = @mysql_query($query1);
$count1 = @mysql_num_rows($result1);
if(empty($result1)) {
echo_header();
echo "Could not select from table becuase of:" . mysql_error();
echo_footer();
exit;
}
if($count1 > 1) {
echo_header();
echo "What the...? There's more than one user with the nick name {$_POST['username']}";
echo_footer();
exit;
}
if($count1 == 1) {
echo_header();
echo "Someone has already taken that name...";
echo_footer();
exit;
}
foreach ($_POST as $key => $value) {
$value = strip_tags($value);
$value = trim($value);
$value = addslashes($value);
$_POST[$key] = $value;
}
$query2 = "INSERT INTO keeper (name, email, pass, country) VALUES ('{$_POST['username']}', '{$_POST['email']}', '{$_POST['password']}', '{$_POST['country']}') LIMIT 1";
$result2 = @mysql_query($query2);
if(empty($result2)) {
echo_header();
echo "Error, can't insert into table because of:" . mysql_error();
echo_footer();
exit;
}
echo_header("<meta http-equiv="refresh" content="5;URL=start.php">");
echo <<< STUFF
<tr>
<td colspan='10'>
You have succesfully registrated!<br>
You will now be redirected to the start page, or<br>
<a href='start.php'>click here if you don't wish to wait.</a>
</td>
</tr>
STUFF;
echo_footer();
} else {
echo_header();
echo_register();
echo_footer();
}
function echo_register() { //Function echo_register
echo "
<tr>
<td colspan='2'>
<b>Register new account:</b><br>Fields with a star (*) is required, maxlenght for usernames are 12 characters & passwords 12.<br>
</td>
</tr><tr><form name='register' method='post' action='register.php'>
<td class='lclmn'>*Username:</td> <td class='rclmn'><input type='text' name='username' size='20'></td>
</tr><tr>
<td class='lclmn'>*Password:</td> <td class='rclmn'><input type='password' name='password' size='20'></td>
</tr><tr>
<td class='lclmn'>*Retype pass:</td> <td class='rclmn'><input type='password' name='retypepass' size='20'></td>
</tr><tr>
<td class='lclmn'>Email:</td> <td class='rclmn'><input type='text' name='email' size='20' value='user@domain.com'></td>
</tr><tr>
<td class='lclmn'>Country:</td> <td class='rclmn'><input type='text' name='country' size='20' value='Sweden'></td>
</tr><tr>
<td colspan='2'><input type='submit' name='submit_reg' value='[ register ]'></form>";
mk_btn("[ go back ]", "index.php");
echo "</td>
</tr>
";
}
?>
CTRL + F5 didn't work in Zend since I needed config.php <_<. Meh.
-Nay
Edit -> I did a CTRL + ALT + A. I wondered. Vigge, you can't do that with functions. $errormsg in a function won't work (I think). Well, Zend told me that 'use of variable before it was defined'. Maybe this is where OOP comes in .
<?php
function say($message = 'nothing') {
echo $message;
}
say(); // will print out "nothing" since an argument was not defined
say("heredoc rulessssssssssss"); // will print out.......well, you can guess it
?>
still won't work, no errors appear at all when something is wrong, and when it should process and add the user, nothing appears in the browser, and nothing is added in the database
Last edited by vigge89 on Sat Dec 27, 2003 1:34 pm, edited 1 time in total.
go it to work now, but when the user should be added, i get the "user could not be added: ".mysql_error();
There is nothing after "added: ", and the user isn't of course added, becuase of that $result2 is empty;