...

View Full Version : creating two rows in separate tables when registering a user



Smudly
06-16-2010, 10:50 PM
MySQL Version: 5.0.19

I am attempting to create two rows in separate tables when a user registers.
I am doing this, because there is way too much information I am trying to keep track of for every user. So I will have the users table with basic information, and the userstats table with additional stuff.

The first row will be created in the table called users:

id int(11) No
username varchar(25) No
email varchar(64) No
fname varchar(25) No
lname varchar(25) No
member tinyint(1) No
referrer varchar(25) No
joindate date No
password varchar(32) No
ip varchar(15) No

The second row will be created in the table called userstats:

id int(11) No
username varchar(25) No
level int(2) No
exp int(6) No
credits decimal(9,3) No
todaysurfed int(4) No
totalsurfed int(11) No
timesurfed time No
lastsurfed date No

The code is supposed to insert the information into 2 separate rows.
It inserts fine if I just try doing the insert for users, but it will not work for userstats. It is not giving any errors.

Here is the section of code that we are talking about:


$queryreg = mysql_query("INSERT INTO users VALUES ('','$username','$email','$fname','','','','$joindate','$password','$ip')");

$statsreg = mysql_query("INSERT INTO userstats VALUES ('','$username','$level','','$credits','','','','')");



Full code listed below if you need more information:

<?php

echo "<h1>Register</h1>";

$submit = $_POST['submit'];

$username = ucfirst(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$confpassword = strip_tags($_POST['confpassword']);
$email = strip_tags($_POST['email']);
$fname = ucfirst(strip_tags($_POST['fname']));
//$ref = strip_tags($_POST['ref']);
$joindate = date("Y-m-d");
//I should only give the 50 credits once the user has surfed OR activated email?
$credits = 50.000;
$ip = $_SERVER['REMOTE_ADDR'];
$level = 1;



if ($submit)
{
include('inc/connect.php');

$namecheck = mysql_query("SELECT username FROM users WHERE username='$username'");
$usernamecount = mysql_num_rows($namecheck);
$emailcheck = mysql_query("SELECT email FROM users WHERE email='$email'");
$emailcount = mysql_num_rows($emailcheck);

if ($usernamecount!=0)
{
die("Username already taken!");
}
if ($emailcount!=0)
{
die("E-mail already being used");
}

function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}

// Check for filled out form
if ($username&&$password&&$confpassword&&$email&&$fname)
{

//Encrypt password

if ($password==$confpassword)
{
if (strlen($username)>25)
{
echo "Max limit for Username is 25 characters";
}
if (strlen($password)>32||strlen($password)<6)
{
echo "Password must be between 6 and 32 characters";
}
else
{
//Register the user
$password = md5($password);
$confpassword = md5($confpassword);
echo "Success!";



$queryreg = mysql_query("

INSERT INTO users VALUES ('','$username','$email','$fname','','','','$joindate','$password','$ip')

");

$statsreg = mysql_query("INSERT INTO userstats VALUES ('','$username','$level','','$credits','','','','')");

die("You have been registered! Return to <a href='index.php'>Login</a> page.");

}
if (strlen($email)>25)
{
echo "Max limit for E-mail is 64 characters";
}

if (strlen($fname)>25)
{
echo "Max limit for First Name is 25 characters";
}
/*if (strlen($lname)>25)
{
echo "Max limit for Last Name is 25 characters";
}*/
}
else
echo "Your passwords do not match!";
}
else
echo "Please fill in <strong>all</strong> fields!";




}



?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" language="javascript">
function inputLimiter(e,allow) {
var AllowableCharacters = '';

if (allow == 'FirstNameChar'){AllowableCharacters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';}
if (allow == 'UsernameChar'){AllowableCharacters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890' ;}

var k;

k=document.all?parseInt(e.keyCode): parseInt(e.which);

if (k!=13 && k!=8 && k!=0){

if ((e.ctrlKey==false) && (e.altKey==false)) {

return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);

} else {

return true;

}

} else {

return true;

}

}

</script>
</head>
<body>

<form action="register.php" method="POST">
<div id="register">
Username: <input type="text" id="UsernameChar" onkeypress="return inputLimiter(event,'UsernameChar')" name="username" maxlength="25" value="<?php echo $username ?>"><br />
Password: <input type="password" maxlength="32" name="password" ><br />
Confirm Password: <input type="password" maxlength="32" name="confpassword"><br />
First Name: <input type="text" id="FirstNameChar" onkeypress="return inputLimiter(event,'FirstNameChar')" name="fname" maxlength="25" value="<?php echo $fname ?>"><br />
E-mail: <input type="text" name="email" maxlength="64" value="<?php echo $email ?>"><br />

<input type="submit" name="submit" value="Register">
</div>
</form>

</body>
</html>

xGIHavoc
06-16-2010, 11:10 PM
Try simply merging the two queries.


$queryreg = mysql_query("INSERT INTO users VALUES('','$username','$email','$fname','','','','$joindate','$password','$ip'); INSERT INTO userstats VALUES ('','$username','$level','','$credits','','','','')");

Smudly
06-16-2010, 11:52 PM
Tried, and it inserted all the data correctly into users.

But it didn't do anything inside userstats.

Didn't get any errors.

xGIHavoc
06-17-2010, 07:06 AM
Have you tried other queries on the userstats table? What is the code to connect to the database?

Zoic
06-17-2010, 07:47 AM
If something isn't being inserted into the DB that your trying to, then its probably going to toss an error with mysql.


$queryreg = mysql_query("INSERT INTO users VALUES ('','$username','$email','$fname','','','','$joindate','$password','$ip')") or die(mysql_error());

$statsreg = mysql_query("INSERT INTO userstats VALUES ('','$username','$level','','$credits','','','','')") or die(mysql_error());

Simply, put " or die(mysql_error())" at the end of your mysql_query() function (like in my code above), but before the ending semicolon (;).

See what that gives you, hopefully something.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum