Forum Moderators: coopster
First the MySQL table (exported so it can be imported if you want to try this locally like I am)...
MySQL Database
CREATE TABLE `users` (
`userid` mediumint(16) NOT NULL,
`usertype` tinyint(1) NOT NULL,
`username` varchar(16) collate utf8_unicode_ci NOT NULL,
`password` varchar(16) collate utf8_unicode_ci NOT NULL,
`email` varchar(64) collate utf8_unicode_ci NOT NULL,
`namefirst` varchar(16) collate utf8_unicode_ci NOT NULL,
`namelast` varchar(32) collate utf8_unicode_ci NOT NULL,
`audio` tinyint(1) NOT NULL,
`bandwidth` tinyint(1) NOT NULL,
`dhtml` tinyint(1) NOT NULL,
`dtd` tinyint(1) NOT NULL,
`ieccss` tinyint(1) NOT NULL,
`powerkeys` tinyint(1) NOT NULL,
`theme` varchar(16) collate utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Next the registration page itself...
registration.php
<?php echo '<?xml version="1.0" encoding="UTF-8"?>
';?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP to MySQL Form</title>
<link href="style.css" media="screen" rel="stylesheet" title="city" type="text/css" />
<!-- saved from url=(0014)about:internet -->
</head><body>
<div class="body">
<div style="background: #069; opacity: .6;">
<?php
$referer = basename($_SERVER['HTTP_REFERER']);
echo $referer . '<br />';if(isset($_SERVER['HTTP_REFERER'])) {
if ($_post['audio'] == 1) {$au = "Enabled";} else {$au = "Disabled";}
if ($_post['bandwidth'] == 1) {$bw = "Broadband";} else {$bw = "Dial-Up";}
if ($_post['dhtml'] == 1) {$dh = "Enabled";} else {$dh = "Disabled";}
if ($_post['dtd'] == 0) {$dtd = "XHTML 1.0 Transitional";} else if ($_post['dtd'] == 1) {$dtd = "XHTML 1.0 Strict";} else if ($_post['dtd'] == 2) {$dtd = "XHTML 1.1";}
if ($_post['ieccss'] == 1) {$ie = "Enabled";} else {$ie = "Disabled";}
if ($_post['powerkeys'] == 1) {$pk = "Enabled";} else {$pk = "Disabled";}$namefirst = $_post['namefirst'];
$namelast = $_post['namelast'];
$username = $_post['username'];
$password = $_post['password'];
$email = $_post['email'];
$audio = $_post['audio'];
$bandwidth = $_post['bandwidth'];
$dhtml = $_post['dhtml'];
$dtd = $_post['dtd'];
$ieccss = $_post['ieccss'];
$powerkeys = $_post['powerkeys'];
$theme = $_post['theme'];echo 'First Name: ' . $_post['namefirst'] . '<br />';
echo 'Last Name: ' . $_post['namelast'] . '<br />';
echo 'Username: ' . $_post['username'] . '<br />';
echo 'Password: (Not Shown) <br />';
echo 'Email: ' . $_post['email'] . '<br />';
echo 'Audio: ' . $au . '<br />';
echo 'Connection: ' . $bw . '<br />';
echo 'DHTML Effects: ' . $dh . '<br />';
echo 'Doctype DTD: ' . $dh . '<br />';
echo 'IECCSS: ' . $ie . '<br />';
echo 'Power Keys: ' . $pk . '<br />';
echo 'Theme: ' . $_post['theme'] . '<br />';DEFINE ('DB_USER', '****');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', '****');
DEFINE ('DB_NAME', '****');$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );$query = "INSERT INTO users (namefirst, namelast, username, password, email, audio, bandwidth, dhtml, ieccss, powerkeys, theme) VALUES ('$namefirst', '$namelast', '$username', '$password', '$email', '$audio', '$bandwidth', '$dhtml', '$dtd', '$ieccss', '$powerkeys', '$theme', NOW() )";
$result = @mysql_query ($query);
}
?>
<form action="register.php" method="post">
<fieldset>
<input name="counter" value="252" type="hidden" /><label for="namefirst">First Name: <input id="namefirst" name="namefirst" type="text" value="" /></label>
<br />
<label for="namelast">Last Name: <input id="namelast" name="namelast" type="text" value="" /></label>
<br />
<label for="username">Username: <input id="username" name="username" type="text" value="" /></label>
<br />
<label for="password">Password: <input id="pass" name="password" type="password" value="" /></label>
<br />
<label for="passwordconfirm">Confirm: <input id="passwordconfirm" name="passwordconfirm" type="password" value="" /></label>
<br />
<label for="email">E-mail Address: <input id="email" name="email" type="text" value="" /></label>
<br />
<br /><label for="audio1"><input type="radio" checked="checked" id="audio1" name="audio" value="1" />Audio Enabled</label>
<br />
<label for="audio0"><input type="radio" id="audio0" name="audio" value="0" />Audio Disabled</label>
<br />
<br /><label for="bandwidth1"><input type="radio" checked="checked" id="bandwidth1" name="bandwidth" value="1" />Broadband Connection</label>
<br />
<label for="bandwidth0"><input type="radio" id="bandwidth0" name="bandwidth" value="0" />Dial-Up Connection</label>
<br />
<br /><label for="dhtml1"><input type="radio" checked="checked" id="dhtml1" name="dhtml" value="1" />DHTML Effects Enabled</label>
<br />
<label for="dhtml0"><input type="radio" id="dhtml0" name="dhtml" value="0" />DHTML Effects Disabled</label>
<br />
<br /><label for="dtd2"><input type="radio" checked="checked" id="dtd2" name="dtd" value="2" />XHTML 1.1</label>
<br />
<label for="dtd1"><input type="radio" id="dtd1" name="dtd" value="1" />XHTML 1.0 Strict</label>
<br />
<label for="dtd0"><input type="radio" id="dtd0" name="dtd" value="0" />XHTML 1.0 Transitional</label>
<br />
<br /><label for="ieccss1"><input type="radio" checked="checked" id="ieccss1" name="ieccss" value="1" />IECCSS Enabled</label>
<br />
<label for="ieccss0"><input type="radio" id="ieccss0" name="ieccss" value="0" />IECCSS Disabled</label>
<br />
<br /><label for="powerkeys1"><input type="radio" checked="checked" id="powerkeys1" name="powerkeys" value="1" />Power Keys Enabled</label>
<br />
<label for="powerkeys0"><input type="radio" id="powerkeys0" name="powerkeys" value="0" />Power Keys Disabled</label>
<br />
<br /><label for="themeclassic"><input type="radio" checked="checked" id="themeclassic" name="theme" value="classic" />Classic Theme</label>
<br />
<label for="themecity"><input type="radio" id="themecity" name="theme" value="city" />City Blue Theme</label>
<br />
<label for="themelavender"><input type="radio" id="themelavender" name="theme" value="lavender" />Lavender Theme</label>
<br />
<label for="themematrix"><input type="radio" id="themematrix" name="theme" value="matrix" />Matrix Theme</label>
<br /><input class="button" type="submit" value="Register" />
</fieldset>
</form></div>
</body>
</html>
Again I don't get any errors but when I go to export the entire database I'm not seeing my submissions included in the export.
Next is there an easy way to dump the database in to a basic (X)HTML table?
- John
Notice: Undefined index: audio...
...and on all the other variables I set to capture the POST(ed) clientside fields.
*Edit* It should be noted I have no clue why the "index"-es are undefined! : - \
*Edit2* My table name is "users" for clarification if I haven't already.
*Edit3* When information is successfully submitted to the DB the main display of tables (like users) should show at least 1 or more records correct?
- John
[edited by: JAB_Creations at 8:29 pm (utc) on Mar. 7, 2007]
In fact, on production sites you should have your errors logged to begin with, and unless you don't want to be warned about a specific error, I'd keep them out of your code. :)
INSERT INTO users (namefirst, namelast, username, password, email, audio, bandwidth, dhtml, ieccss, powerkeys, theme) VALUES ('John', 'Bilicki', 'JAB Creations', 'passwordhere', 'email@address.com', '1', '1', '1', '2', '1', '1', 'classic')
Ron, here we go! :)
Column count doesn't match value count at row 1
I did forget how the heck I'm going to have userids assigned to each user in numerical order. I have just tried this though without success...
$query = "INSERT INTO users (userid, namefirst, namelast, username, password, email, audio, bandwidth, dhtml, ieccss, powerkeys, theme) VALUES ('','$namefirst', '$namelast', '$username', '$password', '$email', '$audio', '$bandwidth', '$dhtml', '$dtd', '$ieccss', '$powerkeys', '$theme')";
I know a buddy of mine has all the PHP errors shut off though stuffed in to a log file (but he hasn't been around for a couple weeks). I'd imagine mine would become a bit heavier then his more quickly though! :-D
Thanks for the help so far!
- John
Your count is still off. You are inserting 13 values into 12 columns. It seems like
$dtd is the culprit. :) Try something like this:
$query = "INSERT INTO users (userid, namefirst, namelast, username, password, email, audio, bandwidth, dhtml, [b]dtd[/b], ieccss, powerkeys, theme) VALUES ('','$namefirst', '$namelast', '$username', '$password', '$email', '$audio', '$bandwidth', '$dhtml', '$dtd', '$ieccss', '$powerkeys', '$theme')";
One last thing to note is in regard to security. You are not adding slashes to the POST variables. Use mysql_real_escape_string [us2.php.net] for that purpose. If you want to save some typing, you can do something like this before you define all the variables:
$_POST = [url=http://us2.php.net/manual/en/function.array-map.php]array_map[/url]("mysql_real_escape_string",$_POST);
#make sure this comes after you open the db connection, but before you define the variables
Good luck! :)
Gah, I missed dtd! After looking up the error I read that it's commonly made by experts in a rush and newbs, wonder which category I fall in to right now?! ;)
Please clarify what you're talking about with slashes? Are you saying I should use this because I do not want to have slashes inserted in to the database? I know I'll have to refine the script a bit especially for security. Thanks for everyone's help!
Now that I have a registration page I'll have to figure out logging in and sessions as well as creating a type column and manually setting myself as an admin type to later handle content such as the referer editor I was working on just before this thread.
[webmasterworld.com...]
I do have a last question in this thread, how do I spit out an HTML table of the database or database's "user" table?
- John
As far as SELECTing the info from the table, we have a great thread on extracting data from mysql [webmasterworld.com]. Jatar does a nice job with the basics.
Hopefully that will get you started ;)
We're here if you need us! :)
Best of luck!
- John
*Edit* Well ok I found it in PHPMyAdmin though after I attempt to change it I get this error...
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
*Edit 2* Well I figured the key thing out (MySQL seems easier in and of itself then PHP!). I do have one problem though! The script keeps inserting blank rows in to users table! User 4 was preceded by a blank user 3 (when there should only be 3 users total).
*Edit3!* - Ok strange, it's remembering the last known userid. So it appeared to be some sort of bug but it seems normal in a certain way. Also I think I've answered my own question on this. ;)
<?php echo '<?xml version="1.0" encoding="UTF-8"?>
'; error_reporting(E_ALL);?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Registration</title>
<link href="style.css" media="screen" rel="stylesheet" title="default" type="text/css" />
<!-- saved from url=(0014)about:internet -->
</head><body>
<div class="body">
<div style="background: #069; opacity: .6;">
<?php
$referer = basename($_SERVER['HTTP_REFERER']);
echo $referer . '<br />';if(isset($_SERVER['HTTP_REFERER'])) {
include("mysql.php");
$_POST = array_map("mysql_real_escape_string",$_POST); #make sure this comes after you open the db connection, but before you define the variablesif ($_POST['audio'] == 1) {$au = "Enabled";} else {$au = "Disabled";}
if ($_POST['bandwidth'] == 1) {$bw = "Broadband";} else {$bw = "Dial-Up";}
if ($_POST['dhtml'] == 1) {$dh = "Enabled";} else {$dh = "Disabled";}
if ($_POST['dtd'] == 0) {$dtd = "XHTML 1.0 Transitional";} else if ($_POST['dtd'] == 1) {$dtd = "XHTML 1.0 Strict";} else if ($_POST['dtd'] == 2) {$dtd = "XHTML 1.1";}
if ($_POST['ieccss'] == 1) {$ie = "Enabled";} else {$ie = "Disabled";}
if ($_POST['powerkeys'] == 1) {$pk = "Enabled";} else {$pk = "Disabled";}$namefirst = $_POST['namefirst'];
$namelast = $_POST['namelast'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$audio = $_POST['audio'];
$bandwidth = $_POST['bandwidth'];
$dhtml = $_POST['dhtml'];
$dtd = $_POST['dtd'];
$ieccss = $_POST['ieccss'];
$powerkeys = $_POST['powerkeys'];
$theme = $_POST['theme'];echo 'First Name: ' . $_POST['namefirst'] . '<br />';
echo 'Last Name: ' . $_POST['namelast'] . '<br />';
echo 'Username: ' . $_POST['username'] . '<br />';
echo 'Password: (Not Shown) <br />';
echo 'Email: ' . $_POST['email'] . '<br />';
echo 'Audio: ' . $au . '<br />';
echo 'Connection: ' . $bw . '<br />';
echo 'DHTML Effects: ' . $dh . '<br />';
echo 'Doctype DTD: ' . $dh . '<br />';
echo 'IECCSS: ' . $ie . '<br />';
echo 'Power Keys: ' . $pk . '<br />';
echo 'Theme: ' . $_POST['theme'] . '<br />';$query = "INSERT INTO users (userid, namefirst, namelast, username, password, email, audio, bandwidth, dhtml, dtd, ieccss, powerkeys, theme) VALUES ('','$namefirst', '$namelast', '$username', '$password', '$email', '$audio', '$bandwidth', '$dhtml', '$dtd', '$ieccss', '$powerkeys', '$theme')";
// $result = @mysql_query ($query);
$result = @mysql_query ($query) or die(mysql_error());
}echo $query;
?>
<form action="register.php" method="post">
<fieldset>
<input name="counter" value="252" type="hidden" /><label for="namefirst">First Name: <input id="namefirst" name="namefirst" type="text" value="" /></label>
<br />
<label for="namelast">Last Name: <input id="namelast" name="namelast" type="text" value="" /></label>
<br />
<label for="username">Username: <input id="username" name="username" type="text" value="" /></label>
<br />
<label for="password">Password: <input id="pass" name="password" type="password" value="" /></label>
<br />
<label for="passwordconfirm">Confirm: <input id="passwordconfirm" name="passwordconfirm" type="password" value="" /></label>
<br />
<label for="email">E-mail Address: <input id="email" name="email" type="text" value="" /></label>
<br /><br />
<label for="audio1"><input type="radio" checked="checked" id="audio1" name="audio" value="1" />Audio Enabled</label>
<br />
<label for="audio0"><input type="radio" id="audio0" name="audio" value="0" />Audio Disabled</label>
<br />
<br /><label for="bandwidth1"><input type="radio" checked="checked" id="bandwidth1" name="bandwidth" value="1" />Broadband Connection</label>
<br />
<label for="bandwidth0"><input type="radio" id="bandwidth0" name="bandwidth" value="0" />Dial-Up Connection</label>
<br />
<br /><label for="dhtml1"><input type="radio" checked="checked" id="dhtml1" name="dhtml" value="1" />DHTML Effects Enabled</label>
<br />
<label for="dhtml0"><input type="radio" id="dhtml0" name="dhtml" value="0" />DHTML Effects Disabled</label>
<br />
<br /><label for="dtd2"><input type="radio" checked="checked" id="dtd2" name="dtd" value="2" />XHTML 1.1</label>
<br />
<label for="dtd1"><input type="radio" id="dtd1" name="dtd" value="1" />XHTML 1.0 Strict</label>
<br />
<label for="dtd0"><input type="radio" id="dtd0" name="dtd" value="0" />XHTML 1.0 Transitional</label>
<br />
<br /><label for="ieccss1"><input type="radio" checked="checked" id="ieccss1" name="ieccss" value="1" />IECCSS Enabled</label>
<br />
<label for="ieccss0"><input type="radio" id="ieccss0" name="ieccss" value="0" />IECCSS Disabled</label>
<br />
<br /><label for="powerkeys1"><input type="radio" checked="checked" id="powerkeys1" name="powerkeys" value="1" />Power Keys Enabled</label>
<br />
<label for="powerkeys0"><input type="radio" id="powerkeys0" name="powerkeys" value="0" />Power Keys Disabled</label>
<br />
<br /><label for="themeclassic"><input type="radio" checked="checked" id="themeclassic" name="theme" value="classic" />Classic Theme</label>
<br />
<label for="themecity"><input type="radio" id="themecity" name="theme" value="city" />City Blue Theme</label>
<br />
<label for="themelavender"><input type="radio" id="themelavender" name="theme" value="lavender" />Lavender Theme</label>
<br />
<label for="themematrix"><input type="radio" id="themematrix" name="theme" value="matrix" />Matrix Theme</label>
<br /><input class="button" type="submit" value="Register" />
</fieldset>
</form></div>
</body>
</html>
[edited by: JAB_Creations at 7:11 am (utc) on Mar. 8, 2007]
userid out of your insert query. If the column is set to autoincrement, it will do it by itself. By the way, as a little bonus, I'll show you a new way to do the following: :)
if ($_POST['powerkeys'] == 1) {$pk = "Enabled";} else {$pk = "Disabled";}
It can get a little messy, huh? Now consider the following:
$pk = ($_POST['powerkeys'])? "Enabled" : "Disabled";
A little cleaner. It's called the ternary operator [us2.php.net]. It's a nice little feature :)
I like your basic human-readable examples Eelix! I do not suppose I can use this for $dtd as it will scale as newer versions of XHTML are released plus that I already cover three versions (1.0 Transitional, 1.0 Strict, and 1.1) (so the values would be 1,2,3+)?
Is there some sort of website that breaks PHP down in to basic real world examples or am I doomed to be at the mercy of "here is all is but only all at once" tutorials? ;)
Today I'll be working on the login script with sessions (to avoid using cookies). Once I get that done I'll figure out how to translate cookies in to the preferences (that I assume won't be hard). Once I have that figured out, I'll try to serve content to logged in visitors. Next I'll try to serve content to admin types only. Once I get that far I'll revise the script at every part for security and such (for example the script does not check to see if you created a password) but that's excess fluff that will only confuse me right now. Thanks for all the help!
- John
* Edit (like this is new for me ha!) - I have a question, is the ternary operator specifically made to set a variable to 1 or 0? I am guessing that lots of developers use 0 and 1 as yes/no on/off options?
[edited by: JAB_Creations at 5:16 pm (utc) on Mar. 8, 2007]
No. The ternary operator simply works like this:
(expression 1)?(expression 2):(expression 3);
If
expression 1 is true, expression 2 gets "executed", otherwise, expression 3 does. As for the dtd, no, this won't work. But, you still can simplify it a little bit:
$types = array('XHTML 1.0 Transitional','XHTML 1.0 Strict','XHTML 1.1');
$dtd = $types[$_POST['dtd']];
:)
In this thread...
[webmasterworld.com...]
...there is talk of md5 password encryption and on top of that doing it clientside. That means the password is encrypted before it's sent to the server, with JavaScript I imagine? Or are we talking about encrypting it after post but before it gets sent to the database? I'm not seeing any JavaScript (right now) in the thread and my time to read all this has become rather limited.
If we're talking about encrypting it serverside but before sending the password to the database then is this...
$userpass = md5($_POST['userpass']);
...basically the same thing as this...
$referer = basename($_SERVER['HTTP_REFERER']);
...as in it's application to get the desired effects?
- John
No, md5 and basename [us3.php.net] don't have anything to do with each other.
Both only require what you have in the quoted examples above. I guess the "easy" part can only be answered by you ;) But as it was said, neither have anything to do with each other.
I've found a JavaScript that can encode to MD4, MD5, and SHA1. If the password is encrypted clientside then there would be no need to encrypt it serverside, correct? Also would there be any gain in encrypting the clientside encrypted password at the server as to reinforce this security measure?
- John