Hey guys,

as part of my project I am trying to make it so each record in my database has it's own page in a fashion similar to:

http://domain.com/Customer.php?customer_name="John Doe"

And this will echo all the information in the table about John Doe. (In the future I would like to have an edit button which takes me to Customeredit.php?customer_name="John Doe"

So far I have grabbed the name and echo'd it into the header.

Here is what I have so far:

        <?PHP
            Require 'Configuration.php';
        ?>
           <h2>
            <?PHP
                $sql="SELECT id,customer_name FROM Customers";
                $result =mysql_query($sql);
                while ($data=mysql_fetch_assoc($result)){
                ?>
                <?php echo $data['customer_name']; } ?>
           </h2>

I am slightly confused as to why it is echoing all the information. into the header. I have these two records in my database:

John Doe
Jeff

It is making the header

John Doe Jeff

As seen here:

http://rhino.minepress.co.uk/Customer.php?customer_name="Jeff"

Thanks in advance!

Member Avatar for LastMitch

@bradly.spicer

I am slightly confused as to why it is echoing all the information. into the header. I have these two records in my database:

The code you provide is not related to the issue you are having. There's nothing wrong with your code you provded above.

You need to used Rewrite your URL. Read and try these examples:

http://www.cyberdesignz.com/blog/website-design/url-rewriting-top-5-ways-of-php-url-rewriting/

I think I probably reworded this wrong.

Basically I want the header AND page information to reflect whichever customer I choose in the URL.

At the moment It is just echoing ALL of the customer_names

but if I put in Customer.php?customer_name="John"
I will want the header to only be John

Not John Jeff Tim

Thanks

Member Avatar for LastMitch

@bradly.spicer

I think I probably reworded this wrong.
Basically I want the header AND page information to reflect whichever customer I choose in the URL.

reworded wrong?

You want Customer.php?customer_name="John" to be this Customer.php?id=John

Am I correct?

Then you will need to rewrite it in your URL and add a few lines in the .htaccess.

The link I provide explain to you how to write the URL like the one you can asking.

You have to add a WHERE clause to the query:

// first check if a querystring exists at all (avoiding errors)
if(isset($_GET['customer_name'])) {

    // escape the value form the url (security)
    $customer_name = mysql_real_escape_string($_GET['customer_name']);
    // then use the value in WHERE statement
    $sql="SELECT id,customer_name FROM Customers WHERE customer_name='$customer_name'";
}

And consider using mysqli instead of mysql.

Member Avatar for LastMitch

@broj1

So it has nothing with rewriting with the URL? I'm just curious can you explain how this works. Maybe I got confused what he is asking. So by Selecting id, customer_name and using Where clause will automatically make the name appear without add or changing the URL?

As far as I understood the question he wants to display data about a particular user. The user is defined in the querystring ?customer_name=John+Doe. So you can get cuustomer name from the $_GET array where variables from query string are stored. You have to use this info in the query to filter out the record with appropriate data.

Where the querystring comes from I do not know since it was not posted. It could be dropdown box or input box etc.

So it has nothing with rewriting with the URL?

I am not saying that. I haven't checked your proposal for solution. As far as I know rewriting the URL makes URLs look nicer, easier to bookmark, easier to get read by search engines etc.

Member Avatar for diafol

The url can't affect the SQL unless it has a where clause and the querystrng parameter is used in it. Rewriting has nothing to do with this. After all, rewriting is just prettifying exisiting domain and associated querystring

Member Avatar for LastMitch

Rewriting has nothing to do with this. After all, rewriting is just prettifying exisiting domain and associated querystring

Anyway the form should take care of that or urlencode php function.

OK, I didn't understand that's why I ask. Thanks it's good to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.