PLEASE help! script only writing to database SOME of the time?!

Discussion in 'PHP' started by whiteblue1942, Apr 23, 2008.

  1. #1
    someone please help me. i am having people register for my website and my script writes like half the people info into my database and some people and some computers it doesnt! what the heck is wrong! here is my code


    <?php
    session_start();
    //include file to connect, this is fine
    
    $query="SELECT * FROM users WHERE firstname = '$_SESSION[firstname]'";
    $result=mysql_query($query);
    $num = mysql_num_rows($result);
    
    if ($num == 0)
    {
    
    
           
    	$one=strip_tags($_SESSION[firstname]);
    	$two=strip_tags($_SESSION[lastname]);
    	$three=strip_tags($_SESSION[email]);
    	$four=strip_tags($_SESSION[pass]);
    
    	$query = "INSERT INTO users SET firstname='$one', lastname='$two', email='$three', pass='$four', money='500', job='Peasant'";
    	
    	$result = mysql_query($query);
    		
    		
    }else{
    header("Location:index.php");
    }
    
     session_destroy();
    ?>
    Code (markup):

     
    whiteblue1942, Apr 23, 2008 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    It seems your script insert the new record if :
    there is no
    "first name" record
    which is equal with
    "$_SESSION[firstname]"
    in your database.

    Are you trying to prevent duplicate first name records or something like that ?
    How does "$_SESSION[firstname]" get filled ?
     
    xrvel, Apr 23, 2008 IP
  3. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Also, unless you user empty() to verify that the variables are not null, set the database fields to NULl instead of NOT NULL.
     
    phpl33t, Apr 23, 2008 IP
  4. TomN

    TomN Peon

    Messages:
    493
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    why don't you just check the existence with

    $query="SELECT * FROM users WHERE email= '$_SESSION[email]'";
    PHP:
    instead of the firstname?

    replace line 5 with what I gave you.

    That would be more useful since users shouldn't share an email, but many could share a firstname!
     
    TomN, Apr 23, 2008 IP