stevenb
11-15-2011, 07:38 PM
Total beginner here. Trying to write to a database I created using phpMyAdmin.
I have 2 files. Here's the first, which calls the second on submit.
<form method='post' action='register2.php'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
Confirm Password: <input type='password' name='confirm' /><br />
<input type='submit' value='Register!' />
</form>
and here's the register2.php file
<?php
if($_POST) {
$username = $_Post['username'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];
if($password != $confirm) { ?>
<span style='color:red'>Error: Passwords do not match!</span>
<?php } else {
$dbhost = 'localhost';
$dbuser = 'root@localhost';
$dbpass = '';
$dbname = 'gamedb';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
mysql_real_escape_string($_POST['username']));
$result = mysql_query($query);
list($count) = mysql_fetch_row($result);
if($count >= 1) { ?>
<span style='color:red'>Error: that username is taken.</span>
<?php } else {
$query = sprintf("INSERT INTO users(username,password) VALUES ('%s','%s');",
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string(md5($password)));
mysql_query($query) or die ('Error updating database');
?>
<span style='color:green'>Congratulations, you registered successfully!</span>
<?php
}
}
}
?>
<form method='post' action='register2.php'>Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
Confirm Password: <input type='password' name='confirm' /><br />
<input type='submit' value='Register!' />
I successfully connect to the database, but neither query is working.
This code always produces a 0, even if I create an entry by hand in my database.
$query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
mysql_real_escape_string($_POST['username']));
$result = mysql_query($query);
and this code always returns 'Error updating database'
$query = sprintf("INSERT INTO users(username,password) VALUES ('%s','%s');",
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string(md5($password)));
mysql_query($query) or die ('Error updating database');
I'm lost. Please help me find my way :).
I have 2 files. Here's the first, which calls the second on submit.
<form method='post' action='register2.php'>
Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
Confirm Password: <input type='password' name='confirm' /><br />
<input type='submit' value='Register!' />
</form>
and here's the register2.php file
<?php
if($_POST) {
$username = $_Post['username'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];
if($password != $confirm) { ?>
<span style='color:red'>Error: Passwords do not match!</span>
<?php } else {
$dbhost = 'localhost';
$dbuser = 'root@localhost';
$dbpass = '';
$dbname = 'gamedb';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
mysql_real_escape_string($_POST['username']));
$result = mysql_query($query);
list($count) = mysql_fetch_row($result);
if($count >= 1) { ?>
<span style='color:red'>Error: that username is taken.</span>
<?php } else {
$query = sprintf("INSERT INTO users(username,password) VALUES ('%s','%s');",
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string(md5($password)));
mysql_query($query) or die ('Error updating database');
?>
<span style='color:green'>Congratulations, you registered successfully!</span>
<?php
}
}
}
?>
<form method='post' action='register2.php'>Username: <input type='text' name='username' /><br />
Password: <input type='password' name='password' /><br />
Confirm Password: <input type='password' name='confirm' /><br />
<input type='submit' value='Register!' />
I successfully connect to the database, but neither query is working.
This code always produces a 0, even if I create an entry by hand in my database.
$query = sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
mysql_real_escape_string($_POST['username']));
$result = mysql_query($query);
and this code always returns 'Error updating database'
$query = sprintf("INSERT INTO users(username,password) VALUES ('%s','%s');",
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string(md5($password)));
mysql_query($query) or die ('Error updating database');
I'm lost. Please help me find my way :).