Jump to content

creating two rows in separate tables when registering a user


Smudly

Recommended Posts

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>

 

Link to comment
Share on other sites

noticed that I didn't see you executing the queries:

<?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!";
			mysql_query("INSERT INTO users VALUES ('','$username','$email','$fname','','','','$joindate','$password','$ip')");
			mysql_query("INSERT INTO userstats VALUES ('','$username','$level','','$credits','','','','')");
			header("location:index.php");

		}
		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>

Link to comment
Share on other sites

Awesome. Thanks a lot. those lines of code worked.

 

I tested it without adding the line:

 

header("Location: index.php");

 

And it works great. But when I add that line in, I get the following error:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/s/m/u/smudlys/html/register.php:3) in /home/content/s/m/u/smudlys/html/register.php on line 140

 

How do I fix this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.