PHP: what is wrong with this query? username database

Discussion in 'PHP' started by GSto, May 31, 2008.

  1. #1
    I am trying to make a page where people can register to be users on my site. the connection is fine (labled as $cxn here) so the problem must be with the query. I've tried typing in queries with the exact same syntax into mySQL, so I must have some problem with the PHP here. can someone help me out?

    Also, the part that shows the mysqli_error isn't working for some reason either.


    //the variables
    	$username = $_GET['username'];
    	$password = $_GET['password'];
    	$email = $_GET['email'];
    	$first = $_GET['firstname'];
    	$last = $_GET['lastname'];
    	$date = date("Y-m-d");
    	//checks to make sure username isn't taken
    	$query = "Select * from $t_user WHERE username = $username";
    	$result = mysqli_query($query,$cxn);
    	if($result){
    		echo "Username is already taken!";
    		$_GET = null;
    		echo "<a href=\"register.php\"> Try Again? </a>";	
    	}
    	//adds them to database.
    	else{
    		$query = "INSERT INTO users (username, password, email, first_name, last_name, create_date) VALUES ('$username', '$password', '$email', '$first', '$last', '$date')";	
    		$result = mysqli_query($query,$cxn); 
    		if($result){
    			echo "Welcome to Athens Bar Hopper!";	
    		}
    		else{
    			echo "my bad, something didn't go right";
    			$problem =  mysqli_error($cxn);
    			echo $problem;
    		}
    	}	
    PHP:
     
    GSto, May 31, 2008 IP
  2. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    //the variables
        $username = $_GET['username'];
        $password = $_GET['password'];
        $email = $_GET['email'];
        $first = $_GET['firstname'];
        $last = $_GET['lastname'];
        $date = date("Y-m-d");
        //checks to make sure username isn't taken
        $query = "Select * from $t_user WHERE username = $username";
        $result = mysqli_query($cxn,$query);
        if($result){
            echo "Username is already taken!";
            $_GET = null;
            echo "<a href=\"register.php\"> Try Again? </a>";   
        }
        //adds them to database.
        else{
            $query = "INSERT INTO users (username, password, email, first_name, last_name, create_date) VALUES ('$username', '$password', '$email', '$first', '$last', '$date')";   
            $result = mysqli_query($query,$cxn);
            if($result){
                echo "Welcome to Athens Bar Hopper!";   
            }
            else{
                echo "my bad, something didn't go right";
                $problem =  mysqli_error($cxn);
                echo $problem;
            }
        }
    
    PHP:
     
    cornetofreak, May 31, 2008 IP
  3. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    give the mysqli errors if not working
     
    cornetofreak, May 31, 2008 IP
  4. Karin_Elliott

    Karin_Elliott Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try adding some escaped quotes around $username like:
    WHERE username = \"$username\"";

    So the whole query looks like:
    $query = "Select * from $t_user WHERE username = \"$username\"";

    By the way, is the Georgia Bar still there? :D
     
    Karin_Elliott, May 31, 2008 IP
  5. GSto

    GSto Peon

    Messages:
    218
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ...
    ok, I really don't understand some of the people in the forum anymore.
    cornetofreak, so spend one post just quoting my code, and another telling me to use mysqli_error, when I already said:
    in my first post, and in the code there is a call to the mysqli_error function.

    Karin, thanks for pointing out that typo. but the problem is that when I fill out all of the forums correctly, it still will not execute the INSERT query, and I don't know why.

    oh, and Georgia Bar is still there.
     
    GSto, Jun 1, 2008 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    It looks like he made a slight modification to your code :
    $result = mysqli_query($cxn,$query);

    And he was asking for the error message, not asking to you to use it.
    My only question would be, did you mean to use a variable to select the table (it isn't defined in what you posted).
    $query = "Select * from $t_user WHERE username = $username";
     
    shallowink, Jun 1, 2008 IP