0

I'm trying to set up a simple database with Heroku/PGSQL. So far I've made a connection and created the table I want, but whenever I try and insert data to the table nothing happens.

For testing purposes, I'm using the code

$dbconn = pg_connect(pg_connection_string());
if (!$dbconn) {
   echo "Database connection error. ";
}
else {
// Create table
    $create="CREATE TABLE IF NOT EXISTS users (
        id INT PRIMARY KEY NOT NULL, 
        gender CHAR(30), 
        age INT, 
        location CHAR(30), 
        timestamp CHAR(30)
        )";

// Execute query
    if (pg_query($dbconn,$create))  {
        echo "Table users created successfully. ";
    }
    else  {
        echo "Error creating table. ";
    }
}

function insert() {
    $dbconn = pg_connect(pg_connection_string());
    if (!$dbconn) {
        echo "Database connection error 2. ";
    }
    else {
        # Insert query
        $insert = "INSERT INTO users (id, gender, age, location, timestamp) VALUE (1234, 'male', 99, 'UK', '31/05/2013')";
        # Execute query
        if (pg_query($dbconn,$insert)) {
            echo "Data entered successfully. ";
        }
        else {
            echo "Data entry unsuccessful. ";
        }
    }

}

When run, it returns "Table users created successfully." However, when I call the $$insert$$ function (I will later use this to insert different values into the table) it always returns unsuccessful.

What am I doing wrong?

2 Answers 2

3
$insert = "INSERT INTO users (id, gender, age, location, timestamp)
VALUES (1234, 'male', 99, 'UK', '31/05/2013')";

maybe you shouldn't use char for timestamp

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

3 Comments

that was just an advice, the main error was VALUES like i posted
+1 It wasn't clear in your answer, but you saw and solved the mistake.
Thank you steven and eternay, you've no idea how silly I feel for missing that. I trawled other threads for examples and simply couldn't see it! Thanks again
3

In the insert statement, VALUE should be VALUES.

Anyway, you should try to recover the error message given by database. I think it's possible in PHP. You'll have much more information about the reason of your error.

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.