We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,001 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

making a webpage change depending on url + DB

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!

4
Contributors
9
Replies
1 Day
Discussion Span
3 Months Ago
Last Updated
11
Views
bradly.spicer
Junior Poster
120 posts since Oct 2012
Reputation Points: 0
Solved Threads: 7
Skill Endorsements: 0

@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/

LastMitch
Industrious Poster
4,155 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

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

bradly.spicer
Junior Poster
120 posts since Oct 2012
Reputation Points: 0
Solved Threads: 7
Skill Endorsements: 0

@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.

LastMitch
Industrious Poster
4,155 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

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.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

@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?

LastMitch
Industrious Poster
4,155 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

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.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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

diafol
Keep Smiling
Moderator
10,647 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

In the URL you can't use quotes as far as I know. Spaces should be encoded:

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

or

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

I am not sure which encoding scheme is more correct, anyone else knows? Anyway the form should take care of that or urlencode php function.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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.

LastMitch
Industrious Poster
4,155 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0850 seconds using 2.68MB