How to create a specific URL for a user during reg

2019-07-18 20:19发布

How can I do this in MySQL?

When a user creates an account on a website, I want a profile URL to be created for him/her. For example: http://www.domain.com/profile.php?id=111101, the next user's URL may be: http://www.domain.com/profile.php?id=111102 Will I have to set Auto Increment? Some of the fields I do save during the user's registration are:

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

As of now, the field for URL is: ProfileURL
Thank you.

标签: mysql url
2条回答
孤傲高冷的网名
2楼-- · 2019-07-18 20:47

You can create an ID column that will auto increment. However if you pass that to your URL you will have to be very careful with how you handle the query of populating said users profile (e.g. avoid this)

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

So you can do $sql="INSERT INTO Members (ID,fldFullName, fldEmail, Password, Gender, DOB) VALUES ('','$fname','$email','$pass', '$gender', '$date')"

Beware of user input though

查看更多
对你真心纯属浪费
3楼-- · 2019-07-18 20:47

You have to store an ID field with every record in your user database table and you should make it a primary key with auto_increment enabled. That way whenever a new record is added to your database table, the record will automatically be given a unique id number that will allow you to access it.

Next, when you are creating profile.php, do a check at the top of the page for the presence of an id in the $_GET array, if it exists, escape it to prevent sql injetion and do a mysql query to pull the info you need from that particular user's record. Something like this:

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

Now all your user's info is stored in an array called $user for use throughout your page.

Then if you want to get fancy with it and better SEO results, look into apache mod_rewrite module which will allow you to do URL rewriting so that you can have URLs that look like /profile/someusername rather than /profile.php?id=1234. Much better!

Here are some resources for getting started with mod_rewrite:

  1. http://articles.sitepoint.com/article/guide-url-rewriting
  2. http://www.workingwith.me.uk/articles/scripting/mod_rewrite
查看更多
登录 后发表回答