0

Having trouble inserting new records into a table. Anybody care to help?

<?php include('config.php'); ?>
<?php 

if(!empty($_POST['forename']) && !empty($_POST['surname']) && !empty($_POST['email']) && !empty($_POST['emailconfirm']) && !empty($_POST['password'])) {
    if($_POST['email'] == $_POST['emailconfirm']) {
        $forename = mysql_real_escape_string($_POST['forename']);
        $surname = mysql_real_escape_string($_POST['surname']);
        $email = mysql_real_escape_string($_POST['email']);
        $password = md5(mysql_real_escape_string($_POST['password']));
        register();
    }
    else { $errormessage = "Emails do not match"; }
}
else { $errormessage = "Not all fields filled"; }

function register() {
$check = mysql_query("SELECT * FROM Users WHERE EmailAddress = '".$email."'");
    if (mysql_num_rows($check) == 1) {
        $errormessage = "Account already exists with the email address provided";
    }
    else {
        $register = mysql_query("INSERT INTO Users (EmailAddress, Password, Forename, Surname) VALUES('".$email."', '".$password."', '".$forename."', '".$surname."')");
        $errormessage = "Account Added";
    }
}
?>

I think the problem lies within the MySQL Query Statement?

2
  • Are you receiving an error message? If so, what is it? Commented Aug 8, 2010 at 2:25
  • Aaron W.: No error message, it inserts blanks into the Table Commented Aug 8, 2010 at 2:27

2 Answers 2

1

The variables in your register function are out of scope. Need to globalize them.

function register() {
    global $email, $password, $forename, $surname, $errormessage;

    $check = mysql_query("SELECT * FROM Users WHERE EmailAddress = '".$email."'");
    if (mysql_num_rows($check) == 1) {
        $errormessage = "Account already exists with the email address provided";
    }
    else {
        $register = mysql_query("INSERT INTO Users (EmailAddress, Password, Forename, Surname) VALUES('".$email."', '".$password."', '".$forename."', '".$surname."')");
        $errormessage = "Account Added";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yikes! Don't reference them as globals. Pass them in. There is no reason for them to be considered globals here.
0

No params pass to the function register()?? In this file, you want to use register() to add record to the sql. But how could the register() know what value should be use for $mail variable? You can use global in register() in this way:

global  $email, $password, $forename, $surname, $errormessage;

Or you can rewrite the register() as

function register($email, $password, $forename, $surname, $errormessage){....}

And pass the params to register($email, $password, $forename, $surname, $errormessage)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.