Garath531 Posted March 29, 2007 Share Posted March 29, 2007 I am trying to create a log in and register code. <?php //This function will display the registration form function register_form(){ $date = date('D, M, Y'); echo "<form action='?act=register' method='post'>" ."Username: <input type='text' name='username' size='30'><br>" ."Password: <input type='password' name='password' size='30'><br>" ."Confirm your password: <input type='password' name='password_conf' size='30'><br>" ."Email: <input type='text' name='email' size='30'><br>" ."<input type='hidden' name='date' value='$date'>" ."<input type='submit' value='Register'>" ."</form>"; } //This function will register users data function register(){ //Connecting to database $connect = mysql_connect("localhost", "username", "password"); if(!$connect){ die(mysql_error()); } //Selecting database $select_db = mysql_select_db("test"); if(!$select_db){ die(mysql_error()); } //Collecting info $username = $_REQUEST['username']; $password = $_REQUEST['password']; $pass_conf = $_REQUEST['password_conf']; $email = $_REQUEST['email']; $date = $_REQUEST['date']; //Here we will check do we have all inputs filled if(empty($username)){ die("Please enter your username!<br>"); } if(empty($password)){ die("Please enter your password!<br>"); } if(empty($pass_conf)){ die("Please confirm your password!<br>"); } if(empty($email)){ die("Please enter your email!"); } //Let's check if this username is already in use $user_check = mysql_query("SELECT username FROM users WHERE username='$username'"); $do_user_check = mysql_num_rows($user_check); //Now if email is already in use $email_check = mysql_query("SELECT email FROM users WHERE email='$email'"); $do_email_check = mysql_num_rows($email_check); //Now display errors if($do_user_check > 0){ die("Username is already in use!<br>"); } if($do_email_check > 0){ die("Email is already in use!"); } //Now let's check does passwords match if($password != $pass_conf){ die("Passwords don't match!"); } //If everything is okay let's register this user $insert = mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')"); if(!$insert){ die("There's little problem: ".mysql_error()); } echo $username.", you are now registered. Thank you!<br><a href=?act=login>Login</a> | <a href=index.php>Index</a>"; } switch($act){ default; register_form(); break; case "register"; register(); break; } ?> This is my code, but I can't get the switch to work. When the user presses submit, it should go to ?act=register, which should launch the function register(). Can anyone help me? I have no idea where the problem is so I posted the whole code. Php 5.2.1, Apache 2.0.59. Safe mode = Off. Mod Edit : Changed your php tags to code tags Link to comment https://forums.phpfreaks.com/topic/44828-solved-switch-help/ Share on other sites More sharing options...
kenrbnsn Posted March 29, 2007 Share Posted March 29, 2007 Case statements are terminated by a colon, not a semi-colon. Also it's customary to put the "default" case at the end of the block, without a "break" statement: <?php switch($act){ case "register": register(); break; default: register_form(); }?> Ken Link to comment https://forums.phpfreaks.com/topic/44828-solved-switch-help/#findComment-217674 Share on other sites More sharing options...
Garath531 Posted March 29, 2007 Author Share Posted March 29, 2007 I tried this and it didn't work. Could it be something to do with my php.ini file? Link to comment https://forums.phpfreaks.com/topic/44828-solved-switch-help/#findComment-217773 Share on other sites More sharing options...
shaunrigby Posted March 29, 2007 Share Posted March 29, 2007 Where is $act defined? i cannot see it anywhere in your script... Link to comment https://forums.phpfreaks.com/topic/44828-solved-switch-help/#findComment-217777 Share on other sites More sharing options...
Garath531 Posted March 29, 2007 Author Share Posted March 29, 2007 I got the switch and form design from a different site and wrote the rest. I guess $act is supposed to be the ?act= in the url. Link to comment https://forums.phpfreaks.com/topic/44828-solved-switch-help/#findComment-217807 Share on other sites More sharing options...
Barand Posted March 29, 2007 Share Posted March 29, 2007 In which case, put $act = $_GET['act']; before the switch statement Link to comment https://forums.phpfreaks.com/topic/44828-solved-switch-help/#findComment-217809 Share on other sites More sharing options...
Garath531 Posted March 29, 2007 Author Share Posted March 29, 2007 Thanks, it solved the problem. Link to comment https://forums.phpfreaks.com/topic/44828-solved-switch-help/#findComment-217810 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.