1,105,395 Community Members

making a webpage change depending on url + DB

Member Avatar
bradly.spicer
Posting Whiz in Training
238 posts since Oct 2012
Reputation Points: 47 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 6 [?]
 
0
 

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!

LastMitch
Deleted Member
 
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/

Member Avatar
bradly.spicer
Posting Whiz in Training
238 posts since Oct 2012
Reputation Points: 47 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 6 [?]
 
0
 

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

LastMitch
Deleted Member
 
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.

Member Avatar
broj1
Posting Virtuoso
1,576 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
2
 

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.

LastMitch
Deleted Member
 
0
 

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

Member Avatar
broj1
Posting Virtuoso
1,576 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

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
diafol
Where are my eyes?
12,973 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,847 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

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
broj1
Posting Virtuoso
1,576 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

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.

LastMitch
Deleted Member
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article