#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Error in sql syntax. near ''users' WHERE 'username'='(testing)'' at line 1


    Basically everytime I add a user I get the following message.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'username'='(testing)'' at line 1

    This is the code that is causeing the problem.
    Code:
    $sql = "SELECT * FROM 'users' WHERE 'username'='($username)';";   
    	$query = mysql_query($sql) or die(mysql_error());

    I appreciate any help, I'm new to database and php programming. Going to college for it in 2 weeks and want a head start.

    Here is the whole page
    Code:
    <?php
    require "database.php"; 
    
    if (!$_POST['submit']) {
    	
    } else {
    	$errors = false;  
    	
    	$first = protect($_POST['first']); 
    	$last = protect($_POST['last']); 
    	$username = protect($_POST['username']); 
    	$password = protect($_POST['password']); 
    	$pass_conf = protect($_POST['pass_conf']); 
    	$email = protect($_POST['email']);
    	$about = protect($_POST['about']);  
    
    	if (!$first || !$last || !$username || !$password || !$pass_conf || !$email || !		$about) {
    			echo "You didn't fill out all the forms, just saying!"; 
    			$errors = true; 
    	}
    	if ($password != $pass_conf) {
    		echo "The passwords you entered don't match, really dude!"; 
    		$errors = true;
    	}
    	
    	$sql = "SELECT * FROM 'users' WHERE 'username'='($username)';";   
    	$query = mysql_query($sql) or die(mysql_error()); 
    	
    	if (mysql_num_rows($query) > 0) {
    		echo "Username is taken, be a little more creative ehh!";
    		$errors = true; 
    	}
    	
    if ($errors == false) {
    	$sql = 
    	"INSERT into 
    	'users'('first',
    	 'last',
    	 'username',
    	 'password',
    	 'email',
    	 'about') 
    	VALUES 
    	('$first',
    	 '$last',
    	 '$username',
    	 '".md5($password)."',
    	 '$email',
    	 '$about');";
    	
    	$query = mysql_query($sql) or die(mysql_error()); 
    	echo "Thank you for registering!"; 
    	echo "<a href='index.php'>Click here </a> to login"; 
    }
    	
    	
    	
    }
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>RiotEight - Register</title>
        
        <link type="text/css" rel="stylesheet" href="default.css" />
    </head>
    
    <body>
    <h1>RiotEight</h1>
    <p>Fill out information below to add user.</p>
    <form method="post" action="register.php">
    	First Name: <input type="text" name="first" /><br />
        Last Name: <input type="text" name="last" /><br /> 
        Desired Username: <input type="text" name="username"  /><br />
        Password: <input type="password" name="password"  /><br />
        Confirm Password: <input type="password" name="pass_conf" /><br />
        Email: <input type="text" name="email"  /><br />
        About: <br /><textarea name="about" rows="10" cols="50">Tell us about yourself (Not Required)</textarea><br />
        <input type="submit" name="submit" value="Register"  />
        or <a href="index.php">Login to RiotEight</a>
    </form>
    
    </body>
    </html>
  2. #2
  3. Nosey Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,628
    Rep Power
    9418
    Single quotes ' and double quotes " are for strings and only for strings. If you want to quote the name of databases, tables, and columns then use backticks `.
    PHP Code:
    $sql "SELECT * FROM `users` WHERE `username`='($username)';";   
        
    $query mysql_query($sql) or die(mysql_error()); 
    Also, are you sure you want to put parentheses around the username? I'm not.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0
    Thank you, I thought they were single quotes while reading the tutorial. This is the first time I've heard of backticks.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    On planet earth
    Posts
    106
    Rep Power
    11
    Try this


    PHP Code:
    $sql "SELECT * FROM users WHERE username=".$username;
    $query mysql_query($sql) or die(mysql_error()); 

    Comments on this post

    • requinix disagrees : nope

IMN logo majestic logo threadwatch logo seochat tools logo