PHP and MySQL Newbe Question

Robizzle01

Limp Gawd
Joined
Sep 30, 2005
Messages
159
I'm fairly new to php and i've never touched MySQL before today. The first thing I did was create a database and a table for user accounts. Right now, i'm trying to teach myself the basics by following some online tutorials but at the same time, I am changing them to do more what I want them to.

I'm having trouble getting arguments passed in the URL to be recognized in php, well, i'm likely just doing it wrong. Here is a sample of what I mean.

Code:
<?php

$username="username";
$password="password";
$database="database";


$db = mysql_connect("localhost", $username, $password);

@mysql_select_db($database,$db) or die("Unable to select database");

if ($uid == "")
{
printf("<h2>Listing all users from Users table</h2>\n\n");

$result = mysql_query("SELECT * FROM Users", $db);

if ($myrow = mysql_fetch_array($result))
{
	echo "<table border=\"1\" cellpadding=\"5\">\n";
	echo "\t<tr bgcolor=\"#dddddd\"><td>First Name</td><td>Last Name</td><td>Email</td><td>TimeOfAccountCreation</td><td>Details</td></tr>\n\n";
}

do
{
	printf("\t<tr bgcolor=\"#eeeeee\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><a href=\"%s\">View</a></td></tr>\n",
		$myrow["FirstName"], $myrow["LastName"], $myrow["Email"], $myrow["TimeOfAccountCreation"], $PHP_SELF."?uid=".$myrow["UID"]);
} while ($myrow = mysql_fetch_array($result));

echo "</table>\n";
}
else
{
	echo "displaying detailed information for user with UID == ".$uid."\n";
}
mysql_close();
?>

The idea is, if I just pull up list_users.php in my browser, I should (and do) get a table of all the users in the database. The table has a collumn called details with a link for each user that goes to the current page, but with ?uid=somevalue where some value is the UID of the user whom we want a full details page for. The if ($uid == "") at the top should make the page display the list of all users if no parameter is passed, but otherwise I want to go into the else { } block at the very bottom. However, even if I point my browser to list_users.php?uid=1, I still just get the generic table and the else block is never executed.

What am I doing wrong, I bet its something simple but I sure don't see it. I've been loosely following along with http://webmonkey.wired.com/webmonkey/99/21/index3a_page3.html?tw=programming

--Thanks in advance guys and gals
 
Most likely, your server has something called allow_globals (I think that's the name) turned off. What that does is turn the variables in the URL into global variables. This is a good thing, because it's security risk to have it on.

So instead, variables in the URL are stored in a array. If you've worked with HTML forms before, you are familiar with GET and POST methods. GET variables are in the URL, POST is not. In PHP, you access these variables using the $_GET[] and $_POST[] arrays, in your case, you're getting a variable from the URL, which is the GET method, so you want to look in the $_GET[] array. The uid variable will be $_GET['uid'].

So replace the $uid in your script with $_GET['uid'] and you should be good.

More info here: http://us2.php.net/variables.predefined
 
bingo! Much thanks.

I feel silly making a new thread for this other question I had so i'll try asking it here too and see what happens.

When a new user signs up for an account I make him/her enter in his/her birthdate. Is there any function that will check if the date entered is valid (ie, not the 31st day of february) or would I have to write/find my own?

Also, on a related note, is there a good way to make the choices of one dropdown box change depending on the selection made in another one without making the user submit the form. For example, it would be best if I could make it so that if the user selects february, I only display choices 1-28 (unless it is a leap year then 29.) I've seen some sites do it but never did figure out how its done. Maybe javascript? If it is javascript, what is the general consensus among web developers. Take advantage of its features or stay clear since not all users have it turned on/supported?

--Thanks
 
For the PHP date validation, this page seems to have what you're looking for:
http://codewalkers.com/seecode/440.html

The comments there suggest using checkdate(), a built-in PHP function. You'd have to compare the two (the function supplied on the above page, and PHP's checkdate() ) to see the advantages of each.

You could also do it Javascript, but server side validation is probably better, for reasons like you stated. Generally I think Javascript is a given in web development. People that have it turned off usually have it turned off for a reason and are familiar with the side effects. That said, you're site shouldn't break down and should still be navigable without Javascript.
 
Back
Top