1.11M Members

making a webpage change depending on url + DB

 
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!

 
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.

 
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?

 
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.

 
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

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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article