chriscloyd Posted November 30, 2006 Share Posted November 30, 2006 i have never used functions believe it or not i have a register process script and i wanna uses functions for the whole things can someone lead me in the direction to use it for example i want to check to see if email is already used by another user or the same onso like[code]<?phpfunction check_user(){//how do i do it lol }?>[/code] Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted November 30, 2006 Author Share Posted November 30, 2006 okay i just made this function so if they use my form and [CODE]<?phpfunction check_email(){ $check_email = mysql_query("SELECT * FROM users WHERE email = '$email'"); $emailcheck = mysql_num_rows($check_email); if ($emailcheck > 0) { $reason = "-The email address is already in use with another account sorry.<br />"; header("Location: index.php?page=register&reason=$reason"); }}?>[/CODE]how would i call up that function Quote Link to comment Share on other sites More sharing options...
corillo181 Posted November 30, 2006 Share Posted November 30, 2006 i think you have to put the variable inside the check_email($email)then jus tin clude it, or if is in the same page just go as .. check_email(wichever variable); Quote Link to comment Share on other sites More sharing options...
Philip Posted November 30, 2006 Share Posted November 30, 2006 well, there are a few things wrong with your function. here is a fixed version:[code]<?phpfunction check_email($email){ $check_email = mysql_query("SELECT * FROM users WHERE email = '$email'"); // select rows where email = email $numb_email = mysql_num_rows($check_email); //how many rows match the email if ($email > 0) { $reason = "-The email address is already in use with another account sorry.<br />"; header("Location: index.php?page=register&reason=$reason"); } else { return $email; }}?> [/code]Then to call the function, use:[code]<?php $email = 'test@mydomain.com'; //set variable - you would use the user input from the formcheck_email($email); //run function ?>[/code] Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted November 30, 2006 Author Share Posted November 30, 2006 what does return $email do exactly Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted November 30, 2006 Author Share Posted November 30, 2006 this is the code i have right nowregister.php page[CODE]<?phpsession_start();include("db.php");include("functions.php");if (isset($_POST['register'])){$first = $_POST['first'];$last = $_POST['last'];$email = $_POST['email'];$email2 = $_POST['email2'];$password = $_POST['password'];$password2 = $_POST['password2'];$emperorname = $_POST['emperorname'];$cityname = $_POST['cityname'];$key = $_POST['key'];$keyc = $_POST['keyc'];$ip = $_POST['ip'];check_email($email);}?>[/CODE]all it does is stay on the register page but the thing is i already created a row in the table with the email address im registering with Quote Link to comment Share on other sites More sharing options...
bljepp69 Posted November 30, 2006 Share Posted November 30, 2006 You need to understand [url=http://www.php.net/manual/en/language.variables.scope.php]variable scope[/url] when using functions. Unless you specifically declare a variable to be "global" when defining it in the function, it is considered only applicable to that function. As corillo181 mentions, you can pass variables to a function and use the value, but you have to allow for that when defining the function.So, a call to 'check_email($email)' is probably the best way to do this.Your function needs to look like this.[code]<?phpfunction check_email($addr){ $check_email = mysql_query("SELECT * FROM users WHERE email = '$addr'"); $emailcheck = mysql_num_rows($check_email); if ($emailcheck > 0) { $reason = "-The email address is already in use with another account sorry.<br />"; header("Location: index.php?page=register&reason=$reason"); }}?>[/code]Note that you called the 'check_email() function with the variable $email, but you defined the variable in your function as $addr. Inside that function, you use the value $addr (which = $email when called as mentioned).Also, your header location and query string are going to be messy the way you are calling them. The '<br />' will be encoded and then you will have to decode it. It might even break altogether.A better way to define a function like this might be:[code]<?phpfunction check_email($addr){ $check_email = mysql_query("SELECT * FROM users WHERE email = '$addr'"); $emailcheck = mysql_num_rows($check_email); if ($emailcheck > 0) { $err = TRUE; } else $err = FALSE; return $err;}?>[/code]Then, you can call the function like this:[code]if (check_email($addr)) { $err_code = 'A'; header("Location: index.php?page=register&reason=$err_code"); exit();}[/code]Finally, in the index page, you would put a section where you check for the existence of a '$_GET['reason']' and then write out a longer description for 'A', in this example. Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted November 30, 2006 Author Share Posted November 30, 2006 ya but when u use the codeif (check_email($addr)) { $err_code = 'A'; header("Location: index.php?page=register&reason=$err_code"); exit();}it doesnt check if its false tho Quote Link to comment Share on other sites More sharing options...
bljepp69 Posted November 30, 2006 Share Posted November 30, 2006 The function 'check_email()' returns either 'TRUE' or 'FALSE'. It returns 'TRUE' if there is already an email address in the db. So, the statement "if (check_email($addr))" is checking to see if the function is 'TRUE', or, there is already an email address. If so, then process the error and redirect using headers. If the function is 'FALSE' the code will continue processing below this particular 'if' statement. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted November 30, 2006 Share Posted November 30, 2006 i got a question .. the variable needs to be $email ? or you can use name variable and is just going to read it becuase the function is already made? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 30, 2006 Share Posted November 30, 2006 You can say [code=php:0]check_email($addr)[/code] or [code=php:0]check_email($_REQUEST['bubbles'])[/code] or any other name. Inside the function, that variable will be called [code=php:0]$email[/code]. Was that what you were asking?And what [code=php:0]return[/code] does is it says what should happen if you do something like[code=php:0]$var = check_email($addr);[/code]The value you gave for [code=php:0]return[/code] is what [code=php:0]$var[/code] will be set to. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted November 30, 2006 Share Posted November 30, 2006 yes, great! Quote Link to comment 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.