Can't Insert into MySQL Table using PHP
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_r开发者_Python百科eal_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?
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";
}
}
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)
精彩评论