0

I just started working with sqlite3 and my page keeps refreshing on me and keeps inserting data into the database. If I comment out the insert execute command the page does not refresh. I am thinking that it may have to do with my connection to the database but I am not sure. Thanks in advance. Here is the complete code:

<?php

class create {

    function makeDB() {
        $pdo = new PDO("sqlite:db/SSDB");

        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $mainDB = "CREATE TABLE IF NOT EXISTS pass (
                    'rowID' INTEGER,
                    'username' CHAR(256) NOT NULL,
                    'pass' CHAR(256) NOT NULL,
                    'iv' CHAR(256) NOT NULL
                    )";

        $pdo->query($mainDB);
    }    
}

class connectDB {

    public function connect() {
        try {
            $dbh = new PDO("sqlite:db/SSDB");
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $dbh;
        } catch (PDOException $ex) {
            echo $ex->getMessage();
            die();
        }
    }    
}

$connDb = new connectDB();
$conn = $connDb->connect();
$createDB = new create();
$createDB->makeDB();
$string = "hi1";
$salt = 'salt';
$encrypted_string = 'test';
$iv = 4;

$sql = "INSERT INTO pass VALUES(1,:encrypted,:salt,:iv)";

$sqlPrepare = $conn->prepare($sql);

$sqlPrepare->execute(array(':encrypted' => $encrypted_string, ':salt' => $salt,:iv'=> $iv));
7
  • How are you calling this script? Is it an AJAX server? Commented Nov 1, 2014 at 0:18
  • 2
    You have a typo at the end of your execute line, you are missing a '. Commented Nov 1, 2014 at 0:19
  • move that code INTO the class connectDB? Commented Nov 1, 2014 at 0:20
  • @jeroen True, but 99% of the time, they're typos. +1 nonetheless ;) Commented Nov 1, 2014 at 0:30
  • You're using quotes for your columns in your table creation code. Use backticks ` Commented Nov 1, 2014 at 0:36

1 Answer 1

1

As per your originally posted code/question that you edited and overwrote without marking it as an edit, should anyone see this and ask why the answer with the same ticks/quote fix:

Firstly, you're using (single) quotes for your columns in your table creation code.

Use backticks or remove the quotes.

$mainDB = "CREATE TABLE IF NOT EXISTS `pass` (
            `rowID` INTEGER,
            `username` CHAR(256) NOT NULL,
            `pass` CHAR(256) NOT NULL,
            `iv` CHAR(256) NOT NULL
            )";

You're also missing a quote for $salt,:iv' => $salt,':iv'

Add $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened to find errors, which your original code should have thrown.

Or, $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); depending on the one you are using.

As for "my page keeps refreshing on me" - I can't see how your page would refresh. You will need to elaborate on that.

  • There is no code in your posted question to support a page refresh, or redirection.
Sign up to request clarification or add additional context in comments.

2 Comments

Looks like the refreshing is an issue with the Netbeans connector and chrome. I can't reproduce the refresh problem with firefox or running chrome without the connector. I want to do some more tests just to be sure. Isn't this calling it right after the connection or am I missing something public function connect() { try { $dbh = new PDO("sqlite:db/SSDB"); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@Kobaltic Using error exceptions helps to get the errors out into plain sight, which you should get. Now, you've edited and overwritten your question/code with what I've got in my answer without marking it as an "edit"; that's not how things are done here on Stack. People who will visit your question and compare your code to mine, stand at telling themselves "there's nothing wrong with this, so why is there an answer with the same code?" - and stand at downvoting my answer, which I plan on deleting for two reasons; because of what I wrote, and because it hasn't solved the problem; obviously.

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.