When I am registering on the site it is inserting the firstname input into the lastname row in the sql database and the lastname input into the firstname row? Any help
Printable View
When I am registering on the site it is inserting the firstname input into the lastname row in the sql database and the lastname input into the firstname row? Any help
Can you please post the HTML and PHP you are using currently?
I caught it. You mixed up the two fields in your HTML originally. You had the "lastname" field where it was labeled "First name:" and the "firstname" field where it said "Last name:".
Here's the revised code:
PHP Code:<html>
<head>
<body style="background-color:lightgreen">
<?php
mysql_connect("localhost", "****", "***") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());
//This code runs if the form has been submitted
if (isset($_POST['submit'])) {
//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.');
}
// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}
// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}
if($_POST[firstname] != '' && $_POST[firstname] != '' && $_POST[firstname] != '' && $_POST[firstname] != '') {
// now we insert it into the database
//Check if typeSelector is set.
if($_POST['typeSelector'] != '') {
$type = $_POST['typeSelector'];
} else {
$type = $_POST[shade];
}
$insert = "INSERT INTO users (username, password, firstname, lastname, age, gender, email, actor)
VALUES ('".$_POST['username']."', '".$_POST['pass']."', '$_POST[firstname]', '$_POST[lastname]', '$_POST[age]', '$_POST[gender]', '$_POST[email]', '$type')";
$add_member = mysql_query($insert);
echo <<<EOF
<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.</p>
EOF;
} else {
echo "Please fill in all of the required fields.";
}
}
else
{
?>
<?php include("links.php"); ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td colspan="2"><h1>Register</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>First name:</td><td>
<input name="firstname" type="text" maxlength="10">
</td></tr>
<tr><td>Last name:</td><td>
<input name="lastname" type="text" maxlength="10">
</td></tr>
<tr><td>Age:</td><td>
<select name="age">
<?php $i = 1; while($i <= 110) { echo "<option value=\"$i\">$i</option>"; $i++; } ?>
</select>
</td></tr>
<tr><td>Email:</td><td>
<input name="email" type="text" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><td>Actor/Director?:</td><td>
<input type="radio" name="shade" value="Actor" id="actor" onclick="chgType()">Actor
<input type="radio" name="shade" value="Backstageworker" id="backstage" onclick="chgType()">Backstage worker <br />
<select name="typeSelector" id="typeSelector" style="visibility: hidden">
<option>Set mover</option>
<option>Sound</option>
</select>
</td></tr>
<tr><th colspan="2"><input type="submit" name="submit" value="Register"></th></tr> </table>
</form>
<script type="text/javascript">
function chgType() {
var actor = document.getElementById('actor');
var backstage = document.getElementById('backstage');
if(backstage.checked === true) {
document.getElementById('typeSelector').style.visibility = 'visible';
} else {
document.getElementById('typeSelector').style.visibility = 'hidden';
}
}
</script>
<?php
}
?>
</body>
</html>
Thanks so much for everything JShor. You've been really helpfull.
On login.php
PHP Code:<?php
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("keyboard_test") or die(mysql_error());
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: member.php");
}
}
}
//if the login form is submitted
if (isset($_POST['submit'])) {
$george = $_POST['username'];
if ("$george == george")
{
die ('error');
}
// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database
if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=database.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);
//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('I am sorry, the information entered was incorrect');
}
else
{
// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
//then redirect them to the members area
header("Location: member.php");
}
}
}
else
{
// if they are not logged in
?>
<?php include("links.php"); ?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>
<html>
<head>
<body style="background-color:lightgreen">
</head>
<body>
</body>
</html>
I've added in this bit
I want it to die if they enter george as the username but it is dying for everything. It dosen't matter what you put as the username it still dies? Any help?PHP Code:$george = $_POST['username'];
if ("$george == george")
{
die ('HELLO GEORGE');
}
("$george == george")will always evaluate to true.
($george == "george")is what you're trying to do.
Thanks Traq
Okay, I just noticed a problem.
Thats the current working code.PHP Code:<html>
<head>
<script>
var checkobj
function agreesubmit(el){
checkobj=el
if (document.all||document.getElementById){
for (i=0;i<checkobj.form.length;i++){ //hunt down submit button
var tempobj=checkobj.form.elements[i]
if(tempobj.type.toLowerCase()=="submit")
tempobj.disabled=!checkobj.checked
}
}
}
function defaultagree(el){
if (!document.all&&!document.getElementById){
if (window.checkobj&&checkobj.checked)
return true
else{
alert("Please read/accept terms to submit form")
return false
}
}
}
</script>
<script language="javascript">
function disableField()
{
document.getElementById('checky').disabled = false;
}
</script>
</head>
<body>
<body style="background-color:lightgreen">
<?php
require "database.php";
//This code runs if the form has been submitted
if (isset($_POST['submit'])) {
//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] | !$_POST['firstname'] | !$_POST['lastname'] | !$_POST['age'] | !$_POST['gender'] | !$_POST['email'] )
{
die('You did not complete all of the required fields');
}
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.');
}
// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}
// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}
if($_POST[firstname] != '' && $_POST[firstname] != '' && $_POST[firstname] != '' && $_POST[firstname] != '') {
// now we insert it into the database
//Check if typeSelector is set.
if($_POST['typeSelector'] != '') {
$type = $_POST['typeSelector'];
} else {
$type = $_POST[shade];
}
$insert = "INSERT INTO users (username, password, firstname, lastname, age, gender, email, actor)
VALUES ('".$_POST['username']."', '".$_POST['pass']."', '$_POST[firstname]', '$_POST[lastname]', '$_POST[age]', '$_POST[gender]', '$_POST[email]', '$type')";
$add_member = mysql_query($insert);
echo <<<EOF
<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.</p>
EOF;
} else {
echo "Please fill in all of the required fields.";
}
}
else
{
?>
<?php include("links.php"); ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="agreeform" onSubmit="return defaultagree(this)" method="post">
<table border="0">
<tr><td colspan="2"><h1>Register</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="20">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="20">
</td></tr>
<tr><td>First name:</td><td>
<input name="firstname" type="text" maxlength="15">
</td></tr>
<tr><td>Last name:</td><td>
<input name="lastname" type="text" maxlength="15">
</td></tr>
<tr><td>Age:</td><td>
<select name="age">
<?php $i = 1; while($i <= 110) { echo "<option value=\"$i\">$i</option>"; $i++; } ?>
</select>
<tr><td>Gender:</td><td>
<input type="radio" name="gender" value="Male" id="Male">Male
<input type="radio" name="gender" value="Female" id="Female">Female <br />
</td></tr>
<tr><td>Email:</td><td>
<input name="email" type="text" maxlength="30">
</td></tr>
<tr><td>Actor/Backstage?:</td><td>
<input type="radio" name="shade" value="Actor" id="actor" onclick="chgType()">Actor
<input type="radio" name="shade" value="Backstageworker" id="backstage" onclick="chgType()">Backstage worker <br />
<select name="typeSelector" id="typeSelector" style="visibility: hidden">
<option>Set mover</option>
<option>Sound</option>
</select>
<br /><br /><br />
<input name="agreecheck" type="checkbox" onClick="agreesubmit(this)" id="checky" disabled="true"><b>I agree to the <a href="terms.php" onclick="disableField()" target="_blank">terms and conditions</a></b><br>
</td></tr>
<tr><th colspan="2"><input type="submit" name="submit" value="Register" disabled></th></tr> </table>
</form>
<script type="text/javascript">
function chgType() {
var actor = document.getElementById('actor');
var backstage = document.getElementById('backstage');
if(backstage.checked === true) {
document.getElementById('typeSelector').style.visibility = 'visible';
} else {
document.getElementById('typeSelector').style.visibility = 'hidden';
}
}
</script>
<?php
}
?>
</body>
</html>
In this bit here
If they select backstage worker - sound, it inserts Sound.PHP Code:<tr><td>Actor/Backstage?:</td><td>
<input type="radio" name="shade" value="Actor" id="actor" onclick="chgType()">Actor
<input type="radio" name="shade" value="Backstageworker" id="backstage" onclick="chgType()">Backstage worker <br />
If they select backstage worker - set mover, it inserts set mover.
But if I select actor and no subcatagory(because their isn't one) it inserts backstage worker?
Any help?
There's a function calledchgType()in your HTML radio buttons, which is the event that is triggered. The code you provided does not have the functionchgType()listed anywhere.
Um it's right after the submit button.
PHP Code:<script type="text/javascript">
function chgType() {
var actor = document.getElementById('actor');
var backstage = document.getElementById('backstage');
if(backstage.checked === true) {
document.getElementById('typeSelector').style.visibility = 'visible';
} else {
document.getElementById('typeSelector').style.visibility = 'hidden';
}
}
</script>