tags:

views:

33

answers:

4

Hello,

Can someone advise me if I am performing the below steps correctly:

When a user wants to register on the website, register.php handles his/her request. Below is some of the code from register.php:

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

Particularly when I wrote the above code, I was somewhat new to PHP/MySQL and still am. Therefore, I made all of the fields above manually in the table via phpmyadmin. Furthermore, I also added the ID field manually via phpmyadmin, as the first field with auto increment and primary key of course. Why I did it manually, I can't remember the reason of. But I'm pretty sure that this may be the reason why I'm having problems.

What I'm trying to do is, when a user registers on the website, I want a profile URL to be created for him/her. For example, the field in the table could be named ProfileURL, whereas the actual value could be http://www.domain.com/profile.php?id=1, where the id is inherited from the actual ID in the table. How can I do this with my above code? Did I do something wrong when I decided to save all the fields manually via phpmyadmin? Note: I've also been creating tables, databases, fields manually via phpmyadmin. However, its values are INSERTed automatically of course. Am I even on the right track?

Thank you.

+1  A: 

In profile.php, check for $_GET['id'], then if it exists, use a SELECT query for the same ID in the database. It would look something like this.

<?php
if (isset($_GET['id']))
{
    $id = (int) $_GET['id'];
    $sql = 'SELECT * FROM Members WHERE ID = ' . $id;

    // Then the rest of the code to check the results goes here
}
?>

A user with an ID of 1 would be profile.php?id=1

john010117
+1  A: 

You are doing right. Now write SQL like this:

$sql = sprintf("SELECT * FROM Members WHERE ID=%d", mysql_real_escape_string($_GET['id']));

And you'll be able to get userdata by $_GET['id']. Remember to use mysql_real_escape_string to protect your queries against SQL injection. sprintf is also a good thing to substitute right data types like numbers or strings.

fuwaneko
what is mysql_real_escape_string for here?
Col. Shrapnel
To escape the value and make it safe for the query, although the `sprintf()` function would automatically check if the parameter was a digit or not based on the `%d` delimiter.
Martin Bean
@col. shrapnel to prevent SQL injection attacks. Since using index.php?id=1 and relying on $_GET['id'] = 1 as a value, what is to stop a user of site from changing ?id=1 to something malicious. See: http://unixwiz.net/techtips/sql-injection.html
Chris
@Chris interesting link but they say nothing about how can mysql_real_escape_string help in this particular case. can you explain please? how it works
Col. Shrapnel
escape_string functions escape characters in a given text string to "sanitize" them. That link is about sql injection, the escape string is a way to make your given string $_GEt['id']=1 safe to send to MySQL db for processing. If it was not escaped, you could have a user change id=1 to id="<some bad Sql>" and this would execute on your server instead of your desired query which is to lookup the person with id=1 in the DB.
Chris
@Chris I have bad news for you. escape_string function alone sanitize NOTHING. you can easily test it with `id=1 or 1` code. It will be executed with no problem. Try to learn what SQL injection is and how escaping works. this function is useless in tis example. it can help nothing and it is not needed because of sprintf formatting
Col. Shrapnel
+1  A: 

You don't heed to save profile url.
You have to build it dynamically.
Because most of the url remains the samy, only id is changing.
So, get id from the database and add it to the url.

Col. Shrapnel
+1  A: 

As stated above, you don't need to save a profile URL to the database. I'm guessing all profile URLs are going to follow some standard form (i.e. www.example.com/profile.php?id=1)?

Well, if you saved all of those in your database and then you decided you were going to change the format to something like www.example.com/profile/1 you're going to have a lot of out-of-date data in your database. You're going to have to go through each record and update it, and that could be dangerous on a database table with say, millions of rows.

Therefore, the solution is to have a script that takes a parameter. Say profile.php. As above, you would check for the profile using the data in the $_GET array:

<?php
if (isset($_GET['id'])) {
    $id  = mysql_real_escape_string($_GET['id']);
    $sql = "SELECT * FROM members WHERE id = '$id' LIMIT 1";
    $res = mysql_query($sql);
    if (mysql_num_rows() > 0) {
        $member = mysql_fetch_object($res);
        // handle displaying of member's profile here
    }
    else {
        // member does not exist with ID
    }
}
?>

That way, if you decide to change the script name or use search engine-friendly URLs, you don't need to change your database structure.

Martin Bean
Thank you Martin and all.
Newbie_25