2

Can naybody tell me what this means

INSERT INTO users
             (first_name,last_name,email,username,password,privilege) 
             VALUES ('s',1,1,1,1,1);

but when I try

INSERT INTO users
             (first_name,last_name,email,username,password,privilege) 
             VALUES (1,1,1,1,1,1);

it works. I cannot insert string ? What is wrong ? My SQLite table looks like

CREATE TABLE users (
      id          integer PRIMARY KEY AUTOINCREMENT NOT NULL,
      first_name  varchar(50),
      last_name   varchar(50),
      email       varchar(50),
      username    varchar(50),
      password    varchar(50),
      privilege   varchar(50)
    );

and my PHP function looks like

public function insert_user($first_name, $last_name
                      , $email, $username
                      , $password, $privilege) 
{
try {
    $db = new SQLite3($this->db_path);

    $db->query(
"INSERT INTO users(first_name,last_name,email,username,password,privilege) 
    VALUES($first_name,$last_name,$email,$username,$password,$privilege);");

     $last_id = $db->lastInsertRowID();
     $db->close();
     return $last_id;
   } catch (Exception $e) {
      echo 'Exception caught: ', $e->getMessage(), "\n";
   }
}
0

4 Answers 4

4

Don't you need to turn

     VALUES($first_name,$last_name,$email,$username,$password,$privilege

into

     VALUES('$first_name','$last_name','$email','$username','$password','$privilege' )

MySQL/PHP accept numbers without the ''s, but not strings. Also, you might want to escape your input using a default PHP function.

Sign up to request clarification or add additional context in comments.

Comments

1
$db->query("INSERT INTO users(first_name,last_name,email,username,password,privilege) VALUES('$first_name','$last_name','$email','$username','$password','$privilege');")

Missing a ' around the variables. Might want to escape values first. Debugging tip: Put your query into a variable, echo that variable and try it.

Comments

1

You have to use '..' with strings in VALUES

 $db->query("INSERT INTO users(first_name,last_name,email,username,password,privilege) VALUES('$first_name','$last_name','$email','$username','$password','$privilege');");

Comments

0

Did you try to insert data using SqliteManager? I mean do you know that problem was caused from php script or database. If you try to run your query on sqlitemanager. Also try to use "s" with ( " )not ( ' )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.