2010-07-22 62 views
2

如何在MySQL中做到這一點?如何在註冊期間爲用戶創建特定的URL?

當用戶在網站上創建帳戶時,我想爲他/她創建一個配置文件URL。例如:http://www.domain.com/profile.php?id=111101,下一個用戶的URL可能是:http://www.domain.com/profile.php?id=111102我需要設置自動增量嗎? 有些字段我在用戶註冊過程中保存的是:

$sql="INSERT INTO Members (fldFullName, fldEmail, Password, Gender, DOB) 
VALUES 
('$fname','$email','$pass', '$gender', '$date')"; 

截至目前,對於URL字段爲:ProfileURL
謝謝。

+0

請從輸出更新您的問題 – 2010-07-22 15:51:39

回答

1

您可以創建一個自動遞增的ID列。然而,如果你傳遞給你的 網址你必須非常小心你如何處理填充的查詢表示,用戶 分佈(如避免這種情況)

SELECT * FROM Members WHERE ID = '.$id.'; 

所以,你可以做 $ SQL =「INSERT INTO成員(ID,fldFullName,fldEmail,密碼,性別,DOB) VALUE ('','$ fname','$ email','$ pass','$ gender','$ date')「

小心用戶輸入雖然

0

爲您的用戶數據庫表中的每條記錄存儲一個ID字段,並且應該將其設置爲啓用了auto_increment的主鍵。這樣,無論何時將新記錄添加到數據庫表中,該記錄都將自動獲得一個唯一的ID號,以便您訪問它。

接下來,當您創建profile.php時,請在頁面頂部檢查$ _GET數組中是否存在id,如果存在,請將其轉義以防止sql注入並執行mysql查詢從特定用戶的記錄中提取所需的信息。事情是這樣的:

if(isset($_GET['id'])) { //check if id exists in $_GET 
    $id = mysql_real_escape_string($_GET['id']); //prevent sql injection 
    $resc = mysql_query("SELECT fldFullName, fldEmail, Password, Gender, DOB FROM Members WHERE id='$id' LIMIT 1"); //query the db for record 
    if(mysql_num_rows($resc) == 1) { //make sure the user exists 
     $user = mysql_fetch_assoc($resc); //convert mysql resource into array that can be used throughout your script 
    } else { 
     echo "no user with that id"; 
} else { 
    echo "no id provided."; 
} 

現在所有用戶的信息都存儲在一個陣列稱爲$user以便在整個頁面中使用。

然後,如果你想獲得幻想與它更好的SEO效果,看看阿帕奇mod_rewrite模塊,這將允許你做URL重寫,這樣就可以有一個看起來像/profile/someusername而非/profile.php?id=1234網址。 好多了!

這裏是開始與mod_rewrite的一些資源:`DESC members`:

  1. http://articles.sitepoint.com/article/guide-url-rewriting
  2. http://www.workingwith.me.uk/articles/scripting/mod_rewrite
相關問題