Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
creating two rows in separate tables when registering a user
Old 06-16-2010, 05:50 PM creating two rows in separate tables when registering a user
Skilled Talker

Posts: 83
Trades: 0
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:
Code:
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:
Code:
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:

PHP Code:
$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 Code:
<?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($email0$atIndex);
      
$localLen strlen($local);
      
$domainLen strlen($domain);
      if (
$localLen || $localLen 64)
      {
         
// local part length exceeded
         
$isValid false;
      }
      else if (
$domainLen || $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>
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-16-2010, 08:51 PM Re: creating two rows in separate tables when registering a user
badams's Avatar
Skilled Talker

Latest Blog Post:
Dum Skype Firefox plugin
Posts: 69
Name: Barry Adams
Location: London
Trades: 0
Bear in mind that, it would mean, having to do a table join, when want to find say, the users level (in one table), from his first name (in the other table), nothing wrong will have very long table rows, you don't have to read every item in the database at each request.
__________________

Please login or register to view this content. Registration is FREE
@
Please login or register to view this content. Registration is FREE
(320+ Subjects)
badams is offline
Reply With Quote
View Public Profile Visit badams's homepage!
 
Old 06-17-2010, 11:21 PM Re: creating two rows in separate tables when registering a user
Experienced Talker

Posts: 35
Trades: 0
Wouldn't it be easier to just keep all he user info on one table? I am making a game and have found that is the easiest way ro do things
Direct is offline
Reply With Quote
View Public Profile
 
Old 06-18-2010, 01:35 PM Re: creating two rows in separate tables when registering a user
BmPotts's Avatar
Novice Talker

Posts: 4
Name: Billy Potts
Location: Telford, UK
Trades: 0
the user ID'S might be screwing it up if you've got AUTO_INCREMENT on both tables.

place the auto increment on the users table only. insert it then get the ID back with "mysql_insert_id()". and then manually insert the id into the userstats table and then insert that one.

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

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

Please login or register to view this content. Registration is FREE
- A Social Network & Community System like no other!
BmPotts is offline
Reply With Quote
View Public Profile Visit BmPotts's homepage!
 
Reply     « Reply to creating two rows in separate tables when registering a user
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13849 seconds with 12 queries