Author | PHP help | fuser Member

Posts: 960 Location: in front of a computer (duh)
Joined: 05.04.07 Rank: Mad User | | well, this is kinda embarrassing since I'm working on a college project, and right now I'm stuck at the registration page. Here's the code and the error:
Registration Page
Code <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Online Music Player Registration Site</title><style type="text/css">
.prompt{color: blue; font-family:Arial; font-size:medium}
</style></head>
<body>
<p><big style="font-weight: bold;">
REGISTER TO THIS SITE</big><br>
</p>
<p><br>
Register to this site in order to gain the full benefits this site offers.<br>
</p>
<form method="post" action="form.php">
<table width="100%" border="0" cellpadding="0" cellspacing="1">
<tr>
<td width="10%" valign="top">Username</td>
<td width="48%"><input type="text" name="username" /> <br /> </font></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /><br>
</tr>
<tr>
<td colspan="2"><hr size="1" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" /> <br/><font color="red" size="2">(E-mail is important so that your registration can be verified) </font></td>
</tr>
<tr>
<td><p>Type of user</p></td>
<td>
<input type="radio" name="user" value= "normal user" /> Normal user
<input type="radio" name="user" value= "musician" /> Musician
</td>
</tr>
<tr>
<td><p>Gender</p></td>
<td>
<input type="radio" name="gender" value="female" />Female
<input type="radio" name="gender" value="male" /> Male </td>
</tr>
<tr>
<td><p> </p></td>
<td><p><br />
<input type="submit" name="Submit" value="Register" class="funcbutton" />
</p></td>
</tr>
</table>
<input type="hidden" name="ref" value="" />
</form>
form.php
Code <?php
$username = htmlspecialchars($_POST['username']);
if (preg_match("/\s/",$text))
{
die("do not use spaces, tabs or newlines in your username");
}
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
die("Invalid E-mail address");
}
$Password = htmlspecialchars($_POST['Password']);
if (preg_match("/\s/",$text))
{
die("do not use spaces, tabs or newlines in your password");
}
?>
<html>
<body>
Your name is: <?php echo $username; ?><br />
Your e-mail: <?php echo $email; ?><br />
<br />
</body>
</html>
<?php
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
die($problem);
}
return $data;
}
function show_error($anyError)
{
?>
<html>
<body>
<b>Please correct the following errors:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
The message I got was:
Notice: Undefined variable: text in C:\wamp\www\mplayer\form.php on line 3
Notice: Undefined index: Password in C:\wamp\www\mplayer\form.php on line 13
Notice: Undefined variable: text in C:\wamp\www\mplayer\form.php on line 14
Your name is: fuser
Your e-mail: hadri90@gmail.com
and another function I was working on is a date of birth selection option, since my supervisor suggested that I include it in my page to filter out users who are below 15 years old.
Code
<html xmlns="http://www.w3.org/1999/xhtml">
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?> ">
<select name="day"><?php
for ($i = 1; $i <= 31; i++) {
echo "<option value=\"$i\">$i</option>\n";
}
?></select>
<select name="month"><?php
for ($i = 1; $i <= 12; i++) {
$monthname = date('F', mktime(12, 0, 0, $i, 1, 1930));
echo "<option value=\"$i\">$monthname</option";
}
?></select>
<select name="year"><?php
for ($i = 1930; $i = 2010; i++) {
echo "<option value=\"$i\">$i</option>";
}
?></select>
</form>
</html>
the message I got for this one is:
Parse error: parse error, expecting `')'' in C:\wamp\www\dateselection.php on line 4
and another thing, does anyone know how I can implement the date selection code into the registration form?






[url=http://userbarz.com/][img]ht
Edited by fuser on 27-01-10 05:04 |
 |
Author | RE: PHP help | Member

Posts: Location:
Joined: 01.01.70 Rank: Guest | | I'm not sure if you're trying to do something odd here, but might you want to replace $text with $username and whatnot to actually test them? Looks like you might've copied template code and forgotten to replace the vars
Code
$username = htmlspecialchars($_POST['username']);
if (preg_match("/\s/",$text))
{
die("do not use spaces, tabs or newlines in your username");
}
|
 |
Author | RE: PHP help | fuser Member

Posts: 960 Location: in front of a computer (duh)
Joined: 05.04.07 Rank: Mad User | | oh, I must've missed it, since my I had a friend of mine help me with the code and I didn't check it twice before running it.






[url=http://userbarz.com/][img]ht |
 |
Author | RE: PHP help | fuser Member

Posts: 960 Location: in front of a computer (duh)
Joined: 05.04.07 Rank: Mad User | | ok, I've fixed the errors in both code, while the first one kind-of works, (I still haven't made the databases yet), it seems fine.
I still have a problem on the birth selection code, though. It runs without any errors, the drop-down boxes doesn't even show any numbers (day, month, year) for me to choose from. any idea why this happens?
the amended code is:
Code <html xmlns="http://www.w3.org/1999/xhtml">
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?> ">
<select name="day"><?php
for ($i = 1; $i <= 31; $i++) {
echo "<option value=\"$i\">$i</option>\n";
}
?></select>
<select name="month"><?php
for ($i = 1; $i <= 12; $i++) {
$monthname = date('F', mktime(12, 0, 0, $i, 1, 1971));
echo "<option value=\"$i\">$monthname</option>";
}
?></select>
<select name="year"><?php
for ($i = 1971; $i <= 2010; $i++) {
echo "<option value=\"$i\">$i</option>";
}
?></select>
</form>
</html>






[url=http://userbarz.com/][img]ht
Edited by fuser on 27-01-10 06:30 |
 |
Author | RE: PHP help | ynori7 Member

Posts: 1486 Location: #valhalla
Joined: 08.10.07 Rank: God | | There were a few minor things missing. This code works for me.
Code <html xmlns="http://www.w3.org/1999/xhtml">
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?> ">
<select name="day"><?php
for ($i = 1; $i <= 31; $i++) {
echo "<option value=\"$i\">$i</option>\n";
}
?></select>
<select name="month"><?php
for ($i = 1; $i <= 12; $i++) {
$monthname = date('F', mktime(12, 0, 0, $i, 1, 1930));
echo "<option value=\"$i\">$monthname</option>";
}
?></select>
<select name="year"><?php
for ($i = 1971; $i <= 2010; $i++) {
echo "<option value=\"$i\">$i</option>";
}
?></select>
</form>
</html>
EDIT: Looks the same as yours. Must be your php install or something.
Edited by ynori7 on 27-01-10 06:31 |
 |
Author | RE: PHP help | AldarHawk Member

Posts: 1690 Location: Canada
Joined: 26.01.06 Rank: Hacker Level 1 | | Another thing you may want to look at is the number of days in the month. You have it loop through and set the days to 31. I want to be born on February 31st!
You may want to have the month and year selection first. this will then reflect the day loop. just remember the months that have 30 days, the ones that have 31 days, and the one that has 28 or 29...This will help you make this form a lot better.
|
 |
Author | RE: PHP help | fuser Member

Posts: 960 Location: in front of a computer (duh)
Joined: 05.04.07 Rank: Mad User | | well, I've been trying to figure out how to do that for the whole day, but so far I haven't been able to gain any results.
also, I'm still stuck trying to figure out how to subtract from the selected date and the date the user signed up, and then figure out if the user it at least is 15 years old (a restriction placed by my project supervisor), and this also results in me hitting a wall most of the time.






[url=http://userbarz.com/][img]ht |
 |
Author | RE: PHP help | ynori7 Member

Posts: 1486 Location: #valhalla
Joined: 08.10.07 Rank: God | | fuser wrote:
also, I'm still stuck trying to figure out how to subtract from the selected date and the date the user signed up, and then figure out if the user it at least is 15 years old (a restriction placed by my project supervisor), and this also results in me hitting a wall most of the time.
Here's one way you can go about doing it:
http://www.daniwe. . .4794.html#
Found by googling "age verification script".
And here's the method I mentioned to you in IM last night:
http://www.phpf1.. . .rence.html
Found by googling "php date difference".
|
 |
Author | RE: PHP help | fuser Member

Posts: 960 Location: in front of a computer (duh)
Joined: 05.04.07 Rank: Mad User | | well, thanks for the links ynori, but most of the tutorials I stumble upon is usually based on a set date, my problem now is trying to figure out how to make it work from the date selected through date selection menu.
I know I'm doing it wrong, I know the concept, but I'm still stuck trying to implement it into code.
I''ve been at it all morning, now I'm beat. Maybe I'll have better luck tomorrow.






[url=http://userbarz.com/][img]ht
Edited by fuser on 27-01-10 19:16 |
 |
Author | RE: PHP help | ynori7 Member

Posts: 1486 Location: #valhalla
Joined: 08.10.07 Rank: God | | fuser wrote:
well, thanks for the links ynori, but most of the tutorials I stumble upon is usually based on a set date, my problem now is trying to figure out how to make it work from the date selected through date selection menu.
This should work:
Code $date = mktime(0,0,0,$_POST['month'],$_POST['day'],$_POST['year']);
Just have your code check to see that the post variables are set (meaning that the user clicked submit), and then make the date object.
Edited by ynori7 on 27-01-10 19:32 |
 |
Author | RE: PHP help | AldarHawk Member

Posts: 1690 Location: Canada
Joined: 26.01.06 Rank: Hacker Level 1 | | I would suggest something like this (crappy code threw together in 1 minute):
Code
if( $month == 6 || $month == 8 || $month == 9 || $month == 11 )
{
$maxday = 30;
}
elseif( $month == 2 )
{
if( $year % 4 )
{
$maxday = 28;
}
else
}
$maxday = 29;
}
}
else
{
$maxday = 31;
}
Please do not mind the crap code, but you get the gist.
|
 |
Author | RE: PHP help | GTADarkDude Member

Posts: 142 Location: The Netherlands
Joined: 23.02.08 Rank: Newbie | | AldarHawk wrote:
I would suggest something like this (crappy code threw together in 1 minute):
*code*
Please do not mind the crap code, but you get the gist. Might go wrong in February 2100 though, as that's not a leap year. Y2.1K Bug?
(Yes you did say the code was crappy, but that doesn't have to mean it's also incorrect. )
...
Edited by GTADarkDude on 28-01-10 12:42 |
 |
Author | RE: PHP help | tkearn5000 Member

Posts: 32 Location: US
Joined: 13.04.09 Rank: Apprentice | | Here is some pseudocode from wikipedia on the subject of leap years that could easily be included in your code.
Code
if year modulo 400 is 0
then is_leap_year
else if year modulo 100 is 0
then not_leap_year
else if year modulo 4 is 0
then is_leap_year
else
not_leap_year
Also, the reason nothing major happened with y2k is that many programmers were hired specifically to make sure that their companies applications were y2k compliant and would not malfunction. Years of work went into making sure that nothing happened.
 |
 |
Author | RE: PHP help | fuser Member

Posts: 960 Location: in front of a computer (duh)
Joined: 05.04.07 Rank: Mad User | | I managed to figure out how to do it, but the problem now is that every time I try to run it it immediately overwhelms my computer and crashes it, making it necessary to reboot.
here's the code:
Code <?php
if (isset($_POST['month']) && is_numeric($_POST['month']) && ((int)$_POST['month'] >= 1 && (int)$_POST['month'] <= 12))
{
$month = (int)$_POST['month'];
}
else
{
$month = date('n');
}
if (isset($_POST['year']) && is_numeric($_POST['year']) && ((int)$_POST['year'] >= 1971 && (int)$_POST['year'] <= 2010))
{
$year = (int)$_POST['year'];
} else {
$year = date('Y');
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?> ">
<select name="day"><?php
$maxdays = date('t', mktime(12, 0, 0, $month, 1, $year));
for ($i = 1; $i <= $maxdays; $i++) {
if (isset($_POST['day']) && $_POST['day'] == $i)
{
$sel = 'selected';
}
elseif ($i == date('j'))
{
$sel = 'selected';
}
else
{
$sel = '';
}
echo "<option value=\"$i\"$sel>$i</option\>\n";
}€
?></select>
<select name="month" onchange="this.form.submit();"><?php
for ($i = 1; $i <= 12; $i++) {
if ($month == $i) {
$sel = ' selected';
} else {
$sel = '';
}
$monthname = date('F', mktime(12, 0, 0, $i, 1, 2005));
echo "<option value=\"$i\"$sel>$monthname</option>\n";
}
?></select>
<select name="year" onchange="this.form.submit();"><?php
for ($i = 1971; i <= 2010; $i++) {
if ($year == $i) {
$sel = ' selected';
} else {
$sel = '';
}
echo "<option value=\"$i\"$sel>$i</option>\n";
}
?></select>
now can anyone figure out what's wrong, or does this happen only to me?






[url=http://userbarz.com/][img]ht |
 |
Author | RE: PHP help | ynori7 Member

Posts: 1486 Location: #valhalla
Joined: 08.10.07 Rank: God | | The final for loop you have is:
for ($i = 1971; i <= 2010; $i++)
Missing "$". Should be:
for ($i = 1971; $i <= 2010; $i++)
Runs perfectly fine for me now.
|
 |
Author | RE: PHP help | fuser Member

Posts: 960 Location: in front of a computer (duh)
Joined: 05.04.07 Rank: Mad User | | yeah, after I posted it, only then did I check the script again only did I noticed what I did wrong, and it worked fine. should've checked first before posting it at the forums.
I have several questions. Firstly, I'm still stuck with the age limit restriction, and I've already figured out how to make one, except that I have to store the birthday in the array, and that I can't make it include leap years.
my plan is to make the date selected by the user being stored into an array, and from there subtract it with the date of registration, and that he has to be at least 13.
here's the code I found from PHP Builder:
Code
<?php
function validateUserAge($bday, $min = 13)
{
list($day, $month, $year) = explode('-', $bday);
$minYear = date('Y') - $min;
if ($year >= $minYear)
{
$age = calculateAge($bday);
if ($age < $min)
return false;
}
return true;
}
?>
<?php
function calculateAge($bday)
{
$birth = strtotime($bday);
$ageStamp = time() - $birth;
$year = 60 * 60 * 24 * 365; // not accounting leap years
return floor($ageStamp / $year);
}
?>
<?php
$minAge = 13;
$bdays = array("10-09-1990");
foreach ($bdays as $bday)
{
if(validateUserAge($bday, $minAge))
echo "user is old enough to enter<br />\n";
else
echo "you have to be at least 13 to enter";
}
?>
[addedon]February 2, 2010, 1:43 pm[/addedon]the second problem is that I've also made another PHP file that basically adds data into the database I've created, but it seems I've also hit a wall with this one.
Code
<?php
$connect = mysql_connect("localhost", "fuser", "projectpass") or die ("connection could not be established.");
mysql_select_db("mp3site");
//insert data into musician table
$insert = "INSERT INTO musician(musician_id, musician_type, musician_name, musician_genre)" .
"VALUES (1, 1, 'Carburetor Dung', 1), " .
"(2, 1, 'Instake', 2), " .
"(3, 1, 'Second Combat', 3), " .
"(4, 2, 'T.I.M.', 1), " .
"(5, 1, 'Sarjan Hassan', 4), " .
"(6, 1, 'LAW', 5), ";
$results = mysql_query($insert) or die (mysql_error());
//insert data into musician type table
$type = "INSERT INTO musiciantype(musiciantype_id, musiciantype_type) " .
" VALUES (1, 'Band'), " .
" (2, 'Solo artist') " .
$results = mysql_query($type) or die (mysql_error());
//insert data into genre table
$type = "INSERT INTO genre (genre_id, genre_type) " .
" VALUES (1, 'Punk Rock'), " .
" (2, 'Rock'), " .
" (3, 'Hardcore') " .
" (4, 'Metal') " .
" (5, 'Indie') " .
$results = mysql_query($type) or die (mysql_error());
echo "Data inserted ";
?>
the error I got was:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
but what was there in line 1? This problem happens on both WAMP and XAMPP, so do I have to add the data into the site through PHPMyAdmin?






[url=http://userbarz.com/][img]ht |
 |
Author | RE: PHP help | AldarHawk Member

Posts: 1690 Location: Canada
Joined: 26.01.06 Rank: Hacker Level 1 | | okay, you need to make sure you open the connection to the database.
$connection = mysql_connect( $host,$user,$password ) or die( "Cannot connect to DB: ".mysql_error() );
$data = mysql_select_db( $dbname, $connection );
you forgot to tell SQL that you NEED the database connection.
Try that and let me know.
|
 |
Author | RE: PHP help | Member

Posts: Location:
Joined: 01.01.70 Rank: Guest | | AldarHawk wrote:
okay, you need to make sure you open the connection to the database.
$connection = mysql_connect( $host,$user,$password ) or die( "Cannot connect to DB: ".mysql_error() );
$data = mysql_select_db( $dbname, $connection );
you forgot to tell SQL that you NEED the database connection.
Try that and let me know.
... He wouldn't be getting a SQL error if the connection wasn't open. The second argument to select_db (the connection reference) is optional. He got an "error in his SQL statement" because he has an error in his SQL statement:
fuser wrote:
$insert = "INSERT INTO musician(musician_id, musician_type, musician_name, musician_genre)" .
"VALUES (1, 1, 'Carburetor Dung', 1), " .
"(2, 1, 'Instake', 2), " .
"(3, 1, 'Second Combat', 3), " .
"(4, 2, 'T.I.M.', 1), " .
"(5, 1, 'Sarjan Hassan', 4), " .
"(6, 1, 'LAW', 5), ";
You're missing a space between ) and VALUES, and you have some sort of lingering comma at the end of the SQL statement.
I know this is hard to believe, but a web server will often tell you what's wrong if:
1. You allow it to by showing errors.
2. You pay attention to the error messages it shows you.
|
 |
Author | RE: PHP help | fuser Member

Posts: 960 Location: in front of a computer (duh)
Joined: 05.04.07 Rank: Mad User | | yeah, it seems to work now, thanks define. but I guess I just didn't understand the error message at that time.






[url=http://userbarz.com/][img]ht |
 |
Author | RE: PHP help | SySTeM Member

Posts: 1524 Location: England, UK
Joined: 27.07.05 Rank: HBH Guru | | Use MySQLi instead of MySQL, it's so much nicer to work with.
|
 |
|