Reputation: 31
I am having trouble with a query on a form I am working on in a development environment with Windows 7, WAMP 2.2, MySQL 5.5.16, Apache 2.2.21, & PHP 5.3.8. Every time I execute the query it returns me an error:
".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 'INERT IGNORE INTO users(username, hashed_password, email)VALUES('username','' at line 1 "
I decided to put my questions at the top for better reference while looking at my code, that is if you intend to help and I listed each source code and file name below if you need any more information please let me know and I will submit it ASAP. Thank you all very much.
Question #1: The database.php contains the only object class MySQLDataBase() I refer to in this form and when I try to submit the form data it inserts it into the database, however the mysql_query function returns false instead of true, which is what it should be doing. I've been doing a TON of research on this and cannot figure out where my syntax is bad nor any other thing that could be wrong. Any help would be greatly appreciated and any tips as well. I'm a moderate-level PHP programmer and have a lot more to learn. I was told to come here for advice.
Question #2: If you take care to notice in the database.php, under class MySQLDatabase, there is an instance function called query(). When I try to use that with the static function register_user in user.php it says MySQL Query failed: No Database Selected, when it is obviously selected because the constructor is run in the datbase.php file that declares it. So if I write User::register_user($username, $password, $email), it will return the "MySQL Query Failed: No Database Selected" error. If anyone would be graceful enough to help me out I would appreciate it very much so.
Here is the form, register.php, that is causing issues:
<?php
require_once("../../includes/initialize.php");
f(isset($_POST['register'])){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$message = "";
$errors = 0;
if(empty($username)){
$errors++;
$message = "Please enter your username." . "<br />";
} if (empty($password)){
$errors++;
$message .= "Please enter your password." . "<br /";
} if (empty($email)){
$errors++;
$message .= "Please enter your email address." . "<br />";
} if (strlen($username) > 15 || strlen($password) > 15 || strlen($email) > 32 ){
$errors++;
$message .= "You entered too many characters for your username/password/email";
} if ($errors > 0){
echo $message;
}
if($errors == 0){
$username = $database->escape_value(trim(htmlentities($_POST['username'])));
$password = $database->escape_value(trim(htmlentities($_POST['password'])));
$email = $database->escape_value(trim(htmlentities($_POST['email'])));
$hashed_password = hash('whirlpool', $password);
$sql = "INSER INTO users(username, hashed_password, email)";
$sql .= "VALUES('{$username}','{$hashed_password}','{$email}')";
$resource = mysql_query($sql);
if( $resource ){
//log_action('Registered', "{$found_user->username} registered.");
echo "User successfully registered " . "<br />" . "<a href = \"login.php\">Login Now!</a>";
} else {
echo "Registration failed please try again later." . mysql_error();
}
} // end if($errors == 0)
}//end if(isset($_POST['resiter']))
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Resister</title>
</head>
<body>
<div>
<p>Please enter your registration information:</p><br />
<form id = "loginForm" action = "register.php" method = "POST" /><br />
<input type = "username" name = "username" value = "" maxlength = "15" /><br />
<input type = "password" name = "password" value = "" maxlength = "15" /><br />
<input type = "email" name = "email" value = "" maxlength = "32" /><br />
<input type = "submit" name = "register" value = "Register!" /><br />
</form>
<a href = "login.php">Login Now!</a>
</div>
</body>
</html>
<?php
$database->close_connection();
?>
Here is the database.php that is included in register.php but only used to open the connection, select the DB and close the connection:
<?php
require(LIB_PATH.DS."config.php");
class MySQLDatabase{
private $connection;
public $last_query;
private $magic_quotes_active;
private $real_escape_string_exists;
function __contruct(){
$this->open_connection();
$this->magic_quotes_active = get_magic_quotes_gpc();
$this->real_escape_string_exists = function_exists("mysql_real_escape_string");
}
public function open_connection(){
$this->connection = mysql_connect(DB_SERVER,DB_USER,_DB_PASS);
if(!$this->connection){
die("There was an error connecting to the database: " . mysql_error());
}
$db_select = mysql_select_db(DB_NAME, $this->connection);
if(!$db_select){
die("There was an error selecting the database");
}
}
public function close_connection() {
if(isset($this->connection)){
mysql_close($this->connection);
unset($this->connection);
}
}
public function query($sql="") {
$this->last_query = $sql;
$result = mysql_query($sql);
$this->confirm_query($result);
return $result;
}
public function escape_value( $value ) {
if($this->real_escape_string_exists){
if($this->magic_quotes_active){
$value = stripslashes( $value );
}
$value = mysql_real_escape_string ( $value );
} else {
if (!$this->magic_quotes_active){
$value = addslashes ( $value );
}
}
return $value;
}
public function fetch_array( $result_set ){
return mysql_fetch_array( $result_set );
}
public function num_rows( $result_set ){
return mysql_num_rows( $result_set );
}
public function insert_id(){
return mysql_insert_id($this->connection);
}
public function affected_rows(){
return mysql_affected_rows($this->connection);
}
private function confirm_query( $result ){
if ( !$result ){
die ("MySQL Query failed: " . mysql_error());
}
}
}
$database = new MySQLDatabase();
?>
Here is user.php that I am having the query() function problem with:
require_once(LIB_PATH.DS."database.php");
class User extends DatabaseObject {
protected static $table_name="users";
public $id;
public $username;
public $user;
public $password;
public $email;
private $hashed_password;
public $first_name;
public $last_name;
public $value;
public static function authenticate($username="", $password=""){
global $database;
$username = $database->escape_value(htmlentities($username));
$password = $database->escape_value(htmlentities($password));
$hashed_password = hash('whirlpool', $password);
$sql = "SELECT * FROM users";
$sql .= "WHERE username = '{$username}' ";
$sql .= "AND hashed_password = '{$hashed_password}' ";
$sql .= "LIMIT 1";
$result_array = parent::find_by_sql($sql);
return !empty($result_array) ? array_shift($result_array) : false;
}
public function register_user($username="", $password="", $email="") {
global $database;
$username = $database->escape_value(htmlentities($username));
$password = $database->escape_value(htmlentities($password));
$hashed_password = hash('whirlpool', $password);
$email = $database->escape_value(htmlentities($email));
$sql = "INSERT INTO users(username, hashed_password, email)";
$sql .= " VALUES ('{$username}', '{$hashed_password}', '{$email}')";
$database->query($sql);
}
public function full_name() {
if(isset($this->first_name) && isset($this->last_name)) {
return $this->first_name . " " . $this->last_name;
} else {
return "";
}
}
public static function user_exists($user="") {
global $database;
$user = $database->escape_value($user);
$sql = "SELECT * FROM users";
$sql .= "WHERE username = {$username}";
$result_set = parent::find_by_sql($sql);
if( $database->num_rows($result_set) >=1 ) {
return true;
}
}
}
?>
Upvotes: 3
Views: 1594
Reputation:
Seems you have typo mistake in your insert query. change INSER INTO
to INSERT INTO
and try.
Upvotes: 0
Reputation: 3086
".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 'INERT IGNORE INTO users(username, hashed_password, email)VALUES('username','' at line 1 "
You have just to change INERT
to INSERT
Upvotes: 3