954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie in need of help with some PHP code :)

Hey! I'm quite new to this whole thing, so please don't fire me with shait on this one =D

I'm trying to learn PHP and MySQL, and atm I am trying to make a website which corresponds with a database - a database with customers.

1. I want the page to show all the customers in the database - with first- and surname.

2. I want each customer to be clickable - when you click a customer, it shows all the information about that customer which is stored in the database.

I can post some of the stuff I tried out with, but I'm not good with PHP (yet! =D), and therefore I got stuck quite early... but here's some of the stuff I've done so far..

<?php
                // Connect to the database server
                $dbcnx = @mysql_connect('localhost', 'root', 'password');
                if (!$dbcnx) {
                    exit('<p>Unable to connect to the database server at this time.</p>');
                }

                // Select the database
                if (!@mysql_select_db('customerdb')) {
                    exit('<p>Unable to locate the database at this time');
                }

                ?>

<p>Here are all the customers:</p>

// Request data from the database
                $customers = @mysql_query('SELECT * FROM customers');
                if (!$customers) {
                    exit('<p>Error performing query: ' . mysql_error() . '</p>');
                }

            while ($row = mysql_fetch_array($customers)) {
                    $customerID = $row['kundeID'];
                    $customer_forename = $row['forename'];
                    $customer_surname = $row['surname'];
                    echo '<p>' . $customer_forename .
                    $customer_surname . 
                    ' <a href="' . $_SERVER['PHP_SELF'] .
                    '?showcustomer=' . $customerID . '">' .
                    'customer details</a></p>';
                } 

                ?>


This piece of code also gives me no spaces between the $customer_forename and $customer_surname.. How can I make a space between them?

And one last question :) In this example, I have just created a link at the end of the customers listed - which says customer details.. Is there a way to make the $customer_forename and $customer_surname a clickable link which executes what I am trying to achieve?

Thanks, and sorry for not being really good at this, at all! =D
Have a nice day!

hawkontvn
Newbie Poster
2 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Hi there,

To get a space you could the html

&nbsp

That should sort that, as for making the name clickable replace 'customer details' in the link to .$customer_forname. " " .$customer_surname.

That should work in theory

Hope it helps

emhmk1
Junior Poster
107 posts since Feb 2008
Reputation Points: 11
Solved Threads: 7
 

You can do that above, but simply adding a quoted space should suffice as well:

echo '<p>' . $customer_forename . " " . $customer_surname .

Never use a hammer when a screwdriver will do.

Cronless
Light Poster
29 posts since Sep 2009
Reputation Points: 14
Solved Threads: 2
 

Ok guys, thanks for the help so far! :D
I should have known the   or the empty " " would work for the spacing... I also managed to make the names clickable..

But I still lack the tricky stuff :p

I made the names from the database clickable, but how can I make them show the rest of the data associated with the names show when I click a name? :)

Hope somebody can help out!

hawkontvn
Newbie Poster
2 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Also there is function called die() that will save you alot of coding.You have:

#
$dbcnx = @mysql_connect('localhost', 'root', 'password');
#
if (!$dbcnx) {
#
exit('<p>Unable to connect to the database server at this time.</p>');
#
}


but you can do this:

$dbcnx = @mysql_connect('localhost', 'root', 'password') or die('<p>Unable to connect to the database server at this time.</p>');

See how simple it is :lol:

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
I made the names from the database clickable, but how can I make them show the rest of the data associated with the names show when I click a name? :)


I don't get you here! What do you mean? display data from database or what? :)

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

ur link points to php self ie the same file. I suggest u to handle customer details retrieval from another php file.
anyway if u want to do that from the same file itself, u can have an if statement like

if(isset($_REQUEST['showcustomer'])){
   $query = 'SELECT * FROM customers where customerId = '.$_REQUEST['showcustomer'];
    $customerDetails = @mysql_query($query); //here is the data u need. Use it the way u want
}


Hope this helpsOk guys, thanks for the help so far! :D
I should have known the   or the empty " " would work for the spacing... I also managed to make the names clickable..

But I still lack the tricky stuff :p

I made the names from the database clickable, but how can I make them show the rest of the data associated with the names show when I click a name? :)

Hope somebody can help out!

venkat0904
Junior Poster
190 posts since Oct 2009
Reputation Points: 13
Solved Threads: 21
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You