Jump to content

functions


chriscloyd

Recommended Posts

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 on
so like
[code]
<?php
function check_user(){
//how do i do it lol
}
?>
[/code]
Link to comment
Share on other sites

okay i just made this function so if they use my form and
[CODE]<?php
function 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
Link to comment
Share on other sites

well, there are a few things wrong with your function. here is a fixed version:
[code]
<?php
function 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 form
check_email($email); //run function
?>[/code]
Link to comment
Share on other sites

this is the code i have right now
register.php page
[CODE]
<?php
session_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
Link to comment
Share on other sites

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]
<?php
function 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]
<?php
function 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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.