 |
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
 | This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

March 14th, 2012, 11:55 AM
|
Authorized User
|
|
Join Date: Feb 2012
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
<select><option> Date of Birth</option></select>
any1 familiar with the selection option
database code
Code:
<?php
$db= mysql_connect('localhost', 'bp6am', 'bp6ampass') or
die ('Unable to connect. Check your connection parameters.');
//create the main database if it doesn't already exits
$query = 'CREATE DATABASE IF NOT EXISTS namelist';
mysql_query($query, $db) or die(mysql_error($db));
//make sure our recently created databse is the active one
mysql_select_db('namelist', $db) or die(mysql_error($db));
// create the user table
$query = 'CREATE TABLE IF NOT EXISTS palgo_user (
user_id INTEGER NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
password CHAR(41) NOT NULL,
PRIMARY KEY (user_id)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));
// create the user infomation table
$query = 'CREATE TABLE IF NOT EXISTS palgo_info (
user_id INTEGER NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
country VARCHAR(25),
************ VARCHAR(20),
d_o_b CHAR(20),
FOREIGN KEY (user_id) REFERENCES palgo_info(user_id)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));
// populate the user table
$query = 'INSERT IGNORE INTO palgo_user
(user_id, username, password)
VALUES
(1, "john", PASSWORD("secret")),
(2, "sally", PASSWORD("password"))';
mysql_query($query, $db) or die (mysql_error($db));
// populate the user infomation table
$query = 'INSERT IGNORE INTO palgo_info
(user_id, first_name, last_name, email, ************, d_o_b, country)
VALUES
(1, "john", "Doe", "jdoe@example.com", NULL, NULL, NULL),
(2, "sally", "Smith", "ssmith@example.com", NULL, NULL, NULL)';
mysql_query($query, $db) or die (mysql_error($db));
echo 'Success!';
?>
code for form:-
Code:
<?php
session_start();
$db = mysql_connect('localhost', 'bp6am', 'bp6ampass') or
die ('Unable to connect. Check your parameters.');
mysql_select_db('palgo', $db) or die (mysql_error($db));
$country_list = array('kk', 'yy', 'mm');
$************_list = array('male', 'female');
$d_o_b_list = array('wats to b added here ? ');
// filter incoming values
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
$first_name = (isset($_POST['first_name'])) ? trim($_POST['first_name']) : '';
$last_name = (isset($_POST['last_name'])) ? trim($_POST['last_name']) : '';
$email = (isset($_POST['email'])) ? trim($_POST['email']) : '';
$country = (isset($_POST['country']) && is_array($_POST['country'])) ? $_POST['country'] : array();
$************ = (isset($_POST['************']) && is_array($_POST['************'])) ? $_POST['************'] : array();
$d_o_b = (isset($_POST['d_o_b']) && is_array($_POST['d_o_b'])) ? $_POST['d_o_b'] : array();
if (isset($_POST['submit']) && $_POST['submit'] == 'Register') {
$errors = array();
// make sure manditory fields have been entered
if (empty($username)) {
$errors[] = 'Username cannot be blank.';
}
// check if username already is registered
$query = 'SELECT username FROM mem_info WHERE username = "' .
$username . '"';
$result = mysql_query($query, $db) or die (mysql_error());
if (mysql_num_rows($result) > 0) {
$errors[] = 'Username ' . $username . ' is already registered.';
$username = '';
// check if email already is registered
$query = 'SELECT username FROM palgo_info WHERE username = "' .
$email . '"';
$result = mysql_query($query, $db) or die (mysql_error());
if (mysql_num_rows($result) > 0) {
$errors[] = 'Username ' . $email . ' is already registered.';
$email = '';
}
mysql_free_result($result);
if (empty($password)) {
$errors[] = 'Password cannot be blank.';
}
if (empty($first_name)) {
$errors[] = 'First name cannot be blank.';
}
if (empty($last_name)) {
$errors[] = 'Last name cannot be blank.';
}
if (empty($email)) {
$errors[] = 'Email address cannot be blank.';
}
if (empty($country)) {
$errors[] = 'Email address cannot be blank.';
}
if (empty($d_o_b)) {
$errors[] = 'Email address cannot be blank.';
}
if (empty($************)) {
$errors[] = 'Email address cannot be blank.';
}
if (count($errors) > 0) {
echo '<p><strong style="color:#FF000;">Unable to process your ' .
'registration.</strong></p>';
echo '<p>Please fix the following:</p>';
echo '<ul>';
foreach ($errors as $error) {
echo '<li>' . $error . '</li>';
}
echo '</ul>';
} else {
// No errors so enter the information into the database.
$query = 'INSERT INTO palgo_user
(user_id, username, password)
VALUES
(NULL, "' . mysql_real_escape_string($username, $db) . '", ' .
'PASSWORD("' . mysql_real_escape_string($password,
$db) . '"))';
$result = mysql_query($query, $db) or die(mysql_error());
$user_id = mysql_insert_id($db);
$query = 'INSERT INTO palgo_info
(user_id, first_name, last_name, email, country, ************, d_o_b)
VALUES
(' . $user_id . ', ' .
'"' . mysql_real_escape_string($first_name, $db) . '", ' .
'"' . mysql_real_escape_string($last_name, $db) . '", ' .
'"' . mysql_real_escape_string($email, $db) . '", ' .
'"' . mysql_real_escape_string(join(', ', $country), $db) . '")';
'"' . mysql_real_escape_string(join(', ', $************), $db) . '")';
'"' . mysql_real_escape_string(join(', ', $d_o_b), $db) . '")';
$result = mysql_query($query, $db) or die(mysql_error());
$_SESSION['logged'] = 1;
$_SESSION['username'] = $username;
header('Refresh: 5; URL=login.php');
?>
<?php
die();
}
}
?>
<head>
<title>Registration</title>
</head>
<body>
<form action="sign_up.php" method="post">
<table>
<tr>
<td ><label for="first_name">First Name:</label></td>
<br />
<td><input type="text" name="text" maxlength="20" size="20"value="<?php echo $first_name; ?>" /></td>
</tr>
<tr>
<td><label for="last_name">Last Name:</label></td>
<td><input type="text" name="text" maxlength="20" size="20" value="<?php echo $last_name; ?>"/></td>
</tr>
<tr>
<td ><label for="username">User Name:</label></td>
<td><input type="text" name="text" maxlength="20" size="20" value="<?php echo $username; ?>"/></td>
</tr>
<tr>
<td ><label for="email">Email:</label></td>
<td><input type="text" name="text" maxlength="20" size="20" value="<?php echo $email; ?>"/></td>
</tr>
<tr>
<td><label for="password">Password:</label></td>
<td><input type="password" name="password" maxlength="20" value="<?php echo $password; ?>"/></td>
</tr>
<tr>
<td ><label for="counrty">Counrty:</label></td>
<td><select name="counrty[]" id="counrty" >
<option>--select country--</option>
<option value="yy">YY</option>
<option value="kk">KK</option>
<option value="mm">MM</option>
</select></td>
</tr>
<tr>
<td ><label for="************">************:</label></td>
<td><select name="************[]" id="************" >
<option>Select</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select></td>
</tr>
<tr>
<td ><label for="d_o_b">Date of birth:</label></td>
<td><select name="d_o_b[]" id="d_o_b" >
<option >Month</option>
<option value="jan">January</option>
<option value="feb">February</option>
<option value="mar">March</option>
</select>
<select name="d_o_b[]" id="d_o_b" >
<option>Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="d_o_b[]" id="d_o_b" >
<option>Year</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
</select>
</td>
</tr>
<tr>
<td><input type="reset" value="reset" /></td>
<td><input type="submit" name="" value="Register"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
|

March 14th, 2012, 12:26 PM
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
As you failed to specify exactly what the problem is I am guessing it's a problem with the date of birth selects... you should name them;
dob_year
dob_month
dob_day
as each select will only return a single value there is no need to use the array (d_o_b[]) you are using and the fact that you have 3 items with the same name would mean you only get the value of the last one on the form, which would be an array.
Like so;
Code:
<select name="dob_month" id="dob_month" >
<option value="0">Month</option>
<option value="jan">January</option>
<option value="feb">February</option>
<option value="mar">March</option>
</select>
<select name="dob_day" id="dob_day" >
<option value="0">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="dob_year" id="dob_year" >
<option value="0">Year</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
</select>
You would then check each item to see if it's not zero (nothing selected) and process as normal, if the value passed is zero then display a message informing the user to select something.
Last edited by UseLess; March 14th, 2012 at 12:29 PM..
|

March 15th, 2012, 12:18 AM
|
Authorized User
|
|
Join Date: Feb 2012
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
hi @useless
thankz for the reply, i'm sorry that i din't specify in da earlier post
can u plz check this line for me
Code:
$country_list = array('kk', 'yy', 'mm');
$s*x_list = array('male', 'female');
$dob_list = array('wats to b added here ? ');
and i'm getting error in dis line for the 'country'
Code:
$country = (isset($_POST['country']) && is_array($_POST['country'])) ? $_POST['country'] : array();
$s*x = (isset($_POST['s*x]) && is_array($_POST['s*x'])) ? $_POST['s*x'] : array();
$d_o_b = (isset($_POST['dob']) && is_array($_POST['dob'])) ? $_POST['dob'] : array();
weather this line will work ?
Code:
<select name="************[]" id="************" >
<option value="0">Select</option>
<option value="male">Male</option>
<option value="female">Female</option>
|

March 15th, 2012, 04:19 AM
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
You do not need to use an array, so this (name changed due to the word filter);
Code:
<select name="gender[]" id="gender" >
<option value="0">Select</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
could be written as;
Code:
<select name="gender" id="gender" >
<option value="0">Select</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
as you will only ever expect a single value to be returned.
You would use an array if you expect more than one item, for example
Code:
<select name="numbers[]" id="numbers" multiple="multiple" size="10">
<option value="0">Select 6 numbers</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
In the above example you can select multiple items and when submitted it would be an array with those items in it.
|

March 15th, 2012, 11:35 AM
|
Authorized User
|
|
Join Date: Feb 2012
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
i have made the change
i fill the form n submit
it does nt show any errors but
no enrty are made into the database
or directed to the login page...
i dont know where to start agian
seem from beginning will do best...
@useless , can u update the whole code when u are free ?
thnks to u.
|

March 15th, 2012, 02:45 PM
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
So have you corrected all the other errors in the 2nd file you posted above?
If not then no wonder it's not working.
Errors I found in no particular order:
- you have 2 ) and the end of a query when there should be 1
- you are missing a closing }
- you are selecting the username from the db but you're doing it based on the users email
- you are using an array for the country when there is no need to
In the file below the db connection has been commented out as I don't have my db server on at the moment, and I have added a debug flag, currently set to true, so the POST array can be output to see the values passed from the form.
There are still some things to fix, which I'll leave for you to solve after all if I do it all for you then you won't learn anything ;)
PHP Code:
<?php
// enable debug mode? $debug = true;
/* session_start();
$db= mysql_connect('localhost', 'bp6am', 'bp6ampass') if( !$db ) { die ('Unable to connect. Check your connection parameters.'); }
// make sure our databse is the active one if( !mysql_select_db('namelist', $db) ) { die('Error Selecting db: ' . mysql_error($db)); } */
$country_list = array('kk', 'yy', 'mm'); $gender_list = array('male', 'female'); $dob_list = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); if( $debug & (isset($_POST['submit']) && ($_POST['submit'] == 'Register')) ) { echo '<pre>'; print_r($_POST); echo '</pre>'; unset($_POST['submit']); } // filter incoming values $username = (isset($_POST['username'])) ? trim($_POST['username']) : ''; $password = (isset($_POST['password'])) ? $_POST['password'] : ''; $first_name = (isset($_POST['first_name'])) ? trim($_POST['first_name']) : ''; $last_name = (isset($_POST['last_name'])) ? trim($_POST['last_name']) : ''; $email = (isset($_POST['email'])) ? trim($_POST['email']) : '';
$country = (isset($_POST['country']) && !empty($_POST['country'])) ? $_POST['country'] : ''; $gender = (isset($_POST['gender']) && !empty($_POST['gender'])) ? $_POST['gender'] : '';
$dob = (isset($_POST['dob']) && is_array($_POST['dob']) && (sizeof($_POST['dob']) == 3)) ? $_POST['dob'] : array();
if (isset($_POST['submit']) && $_POST['submit'] == 'Register') { $errors = array(); // make sure manditory fields have been entered if (empty($username)) { $errors[] = 'Username cannot be blank.'; } // check if username already is registered $query = 'SELECT username FROM palgo_info WHERE username = "' . $username . '"'; $result = mysql_query($query, $db) or die (mysql_error()); if (mysql_num_rows($result) > 0) { $errors[] = 'Username: <em>' . $username . '</em> is already registered.'; $username = ''; } // check if email already is registered $query = 'SELECT email FROM palgo_info WHERE email = "' . $email . '"'; $result = mysql_query($query, $db) or die (mysql_error()); if (mysql_num_rows($result) > 0) { $errors[] = 'Email Address: <em>' . $email . '</em> is already in use by a registered user.'; $email = ''; } mysql_free_result($result); if (empty($password)) { $errors[] = 'Password cannot be blank.'; } if (empty($first_name)) { $errors[] = 'First name cannot be blank.'; } if (empty($last_name)) { $errors[] = 'Last name cannot be blank.'; } if (empty($email)) { $errors[] = 'Email address cannot be blank.'; }
if (count($errors) > 0) { echo '<p><strong style="color:#FF000;">Unable to process your registration.</strong></p>'; echo '<p>Please fix the following:</p>'; echo '<ul>'; foreach ($errors as $error) { echo '<li>' . $error . '</li>'; } echo '</ul>'; } else { // No errors so enter the information into the database.
$query = 'INSERT INTO palgo_user (username, password) VALUES ("' . mysql_real_escape_string($username, $db) . '", PASSWORD("' . mysql_real_escape_string($password, $db) . '")'; $result = mysql_query($query, $db) or die(mysql_error());
$user_id = mysql_insert_id($db);
$query = 'INSERT INTO palgo_info (user_id, first_name, last_name, email, country, gender, d_o_b) VALUES (' . $user_id . ', ' . '"' . mysql_real_escape_string($first_name, $db) . '", ' . '"' . mysql_real_escape_string($last_name, $db) . '", ' . '"' . mysql_real_escape_string($email, $db) . '", ' . '"' . mysql_real_escape_string(join(', ', $country), $db) . '")'; '"' . mysql_real_escape_string(join(', ', $gender), $db) . '")'; '"' . mysql_real_escape_string(join(', ', $dob), $db) . '")'; $result = mysql_query($query, $db) or die(mysql_error());
$_SESSION['logged'] = 1; $_SESSION['username'] = $username;
header('Refresh: 5; URL=login.php');
// die(); } } ?>
<html> <head> <title>Registration</title> </head>
<body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table> <tr> <td ><label for="first_name">First Name:</label></td> <td><input type="text" name="text" maxlength="20" size="20"value="<?php echo $first_name; ?>" /></td> </tr> <tr> <td><label for="last_name">Last Name:</label></td> <td><input type="text" name="text" maxlength="20" size="20" value="<?php echo $last_name; ?>"/></td> </tr> <tr> <td ><label for="username">User Name:</label></td> <td><input type="text" name="text" maxlength="20" size="20" value="<?php echo $username; ?>"/></td> </tr> <tr> <td ><label for="email">Email:</label></td> <td><input type="text" name="text" maxlength="20" size="20" value="<?php echo $email; ?>"/></td> </tr> <tr> <td><label for="password">Password:</label></td> <td><input type="password" name="password" maxlength="20" value="<?php echo $password; ?>"/></td> </tr> <tr> <td><label for="counrty">Counrty:</label></td> <td> <select name="country" id="country" > <option>--select country--</option> <option value="yy">YY</option> <option value="kk">KK</option> <option value="mm">MM</option> </select> </td> </tr> <tr> <td><label for="gender">Gender:</label></td> <td> <select name="gender" id="gender"> <option value="0">Select</option> <?php $option = ''; foreach($gender_list as $value) { $selected = ( ($value == $gender) ? ' selected="selected"' : ''); $option .= '<option value="' . $value . '"' . $selected . '>' .ucfirst($value) . '</option>' . "\n"; } echo $option; ?> </select> </td> </tr> <tr> <td><label for="dob">Date of birth:</label></td> <td> <select name="dob[]" id="dob" > <option >Month</option> <?php $option = ''; foreach($dob_list as $key => $value) { $selected = ( (($key + 1) == $dob[0]) ? ' selected="selected"' : ''); $option .= '<option value="' . ($key + 1) . '"' . $selected . '>' .$value . '</option>' . "\n"; } echo $option; ?> </select>
<select name="dob[]" id="dob" > <option value="0">Day</option> <?php $option = ''; $range = range(1,31); foreach($range as $value) { $selected = ( ($value == $dob[1]) ? ' selected="selected"' : ''); $option .= '<option value="' . $value . '"' . $selected . '>' .$value . '</option>' . "\n"; } echo $option; ?> </select>
<select name="dob[]" id="dob" > <option>Year</option> <?php $option = ''; $range = range(2000, 1950); foreach($range as $value) { $selected = ( ($value == $dob[2]) ? ' selected="selected"' : ''); $option .= '<option value="' . $value . '"' . $selected . '>' .$value . '</option>' . "\n"; } echo $option; ?> </select> </td> </tr> <tr> <td><input type="reset" value="reset" /></td> <td><input type="submit" name="submit" value="Register"/></td> </tr> </table> </form>
</body> </html>
Last edited by UseLess; March 15th, 2012 at 02:48 PM..
|
The Following User Says Thank You to UseLess For This Useful Post:
|
|

March 16th, 2012, 05:34 AM
|
Authorized User
|
|
Join Date: Feb 2012
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
hi thnkz for the code
when i open in my browser
with debug mode i works fine just that the inputs
gives with an array as
Code:
Array
(
[text] => hghg
[password] => ghghg
[country] => kk
[gender] => male
[dob] => Array
(
[0] => 2
[1] => 3
[2] => 1998
)
[submit] => Register
)
and without a debug mode
it give a error as
Code:
Unknown column 'username' in 'field list'
|

March 16th, 2012, 08:52 AM
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
Change this;
Code:
// check if username already is registered
$query = 'SELECT username FROM palgo_info WHERE username = "' . $username . '"';
to this;
Code:
// check if username already is registered
$query = 'SELECT username FROM palgo_user WHERE username = "' . $username . '"';
|

March 16th, 2012, 11:42 AM
|
Authorized User
|
|
Join Date: Feb 2012
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
hi,
even though i fill all the fields i still got thes message below:
i ve gone through many time could nt fig. wats wrong with the codes
Code:
Unable to process your registration.
Please fix the following:
Username cannot be blank.
First name cannot be blank.
Last name cannot be blank.
Email address cannot be blank.
|

March 16th, 2012, 03:20 PM
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
Simple.
You copied the code and have not changed the names of the textboxes, they are all called 'text'. Therefore none of the following are passed;
- username
- first name
- last name
- email address
and as they are not in the POST array they are set to empty strings.
|
The Following User Says Thank You to UseLess For This Useful Post:
|
|
|
 |