2012-01-19 87 views
0

我有一個會談「process_register.php」註冊用戶的形式。該文件與一個名爲「user」的類進行對話。我不能讓PHP把信息到MySQL

<form method="post" action="process_register.php"> 
    User Name: <input type="text" name="userName" maxlength="32"><br> 
    Password: <input type="password" name="userPassword" maxlength="32"><br> 
    <input type="submit"> 
</form> 

process_register.php

<?php 
// process_register.php 

include('userclass.php'); 

$newUser = new User; 

// Call the registerUser() method, passing in the required variables 
$newUser->registerUser($userName, $userPassword); 

// If it was an error, the class will kill the script, if not, it will reach this point 
$newUser->displayUserInfo(); 
?> 

userclass.php

<? 
// process_register.php 
class User { 
var $userID, 
     $userName, 
     $userPassword, 
     $dbHost, 
     $dbUser, 
     $dbName, 
     $dbPass, 
     $dbUserTable; 
function registerUser($userName, $userPassword) { 
     // Connect to database 
     $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); 
     if(!$dbLink) die("Could not connect to database. " . mysql_error()); 

     // Select database 
     mysql_select_db($this->dbName); 

     $query = "insert into $this->dbUserTable values (NULL, \"$userName\", \"$userPassword\")"; 
     $result = mysql_query($query); 

     // Test to make sure query worked 
     if(!$result) die("Query didn't work. " . mysql_error()); 

     // Get the user ID 
     $this->userID = mysql_insert_id(); 

     // Close database connection 
     mysql_close($dbLink); 

     // Assign the values to the data members 
     $this->userName = $userName; 
     $this->userPassword = $userPassword; 
    } // End registerUser() 
function displayUserInfo() { 
     echo '<b>User ID: </b>' . $this->userID . '<br>'; 
     echo '<b>User Name: </b>' . $this->userName . '<br>'; 
     echo '<b>User Password: </b>' . $this->userPassword . '<br>'; 
    } // End displayUserInfo() 

問題是,當process_register.php呼叫 'displayUserInfo' 它爲新用戶創建一個數據庫中的插槽,但它是空白的。

任何的幫助深表感謝

+1

打開你的錯誤報告,看看有什麼東西顯示 – 2012-01-19 00:48:52

+0

我不知道有錯誤報告:/ – user1082764 2012-01-19 00:58:33

回答

0

你下面一個老教程?

就行,$ newUser-> registerUser($用戶名,$的userPassword);它假定$ userName,$ userPassword是全局變量。較早版本的PHP默認情況下已註冊全局變量。在當前版本的PHP中,您必須這樣做:

$ newUser-> registerUser($ _REQUEST ['userName'],$ _REQUEST ['userPassword']);

此外,請確保您在添加到查詢所有數據使用mysql_real_escape_string() - 你是開放的SQL注入,如果你不這樣做。

+0

你必須得到這個很多,但我得說一次,你的輝煌。謝謝! – user1082764 2012-01-19 01:07:29

0

您需要線

$newUser->registerUser($userName, $userPassword); 

前值分配給從 「process_register.php」 文件中的數組$ _POST變量。例如,

$userName = $_POST['userName']; 
$userPassword = $_POST['userPassword']; 

但是不要忘記過濾用戶輸入數據。

0

您可能還需要改變INSERT語句放在MD5密碼,而不是明文。

$userPassword = md5($userPassword); before the insert 

不是「必要的」,而是一個很好的安全措施,以備將來參考。