2010-08-08 47 views
0

無法將新記錄插入表中。有人在乎幫忙嗎?無法使用PHP插入到MySQL表中

<?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"; 
    } 
} 
?> 

我認爲問題出在MySQL查詢語句中?

+0

您是否收到錯誤消息?如果是這樣,那是什麼? – 2010-08-08 02:25:30

+0

Aaron W .:沒有錯誤信息,它將空格插入表中 – ritch 2010-08-08 02:27:01

回答

1

register函數中的變量超出範圍。需要全球化他們。

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"; 
    } 
} 
+4

* Yikes!*不要將它們作爲全局變量引用。將它們傳入。這裏沒有理由讓它們被認爲是全局變量。 – Charles 2010-08-08 02:32:40

+0

謝謝你。 – ritch 2010-08-08 02:33:04

0

沒有參數傳遞給函數register() ??在這個文件中,你想用register()來添加記錄到sql中。但是register()怎麼可能知道應該用什麼值爲$mail變量? 可以以這種方式使用register()global

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

或者你可以重寫register()作爲

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

而且通過PARAMS到register($email, $password, $forename, $surname, $errormessage)