Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having problems writing data from a form to multiple datatables on my PostgreSQL database.

Here is my data model

CREATE TABLE institutions(i_id PK, name text, memberofstaff REFERENCES staff u_id);

CREATE TABLE staff(u_id PK, username text, password text, institution REFERENCES institutions i_id);

So its a 1:1 relationship. These tables have been set up fine. It's the PHP script I'm having difficult with. I'm using CTE-datamodifying or at least trying to but I keep receiving errors on submit.

The PHP:

 $conn = pg_connect('database information filled out in code');

       $result = pg_query("WITH x AS (

       INSERT INTO staff(username, password, institution)

       VALUES('$username', '$password', nextval('institutions_i_id_seq'))

       RETURNING u_id, i_id)

       INSERT INTO institutions (i_id, name, memberofstaff)

       SELECT x.i_id, x.u_id, '$institution'

       FROM x");

       pg_close($conn);

So that's the code and the error I get is:

Warning: pg_query() [function.pg-query]: Query failed: ERROR: relation "institutions_i_id_seq" does not exist LINE 3: VALUES('AberLibrary01', '', nextval('institutions_i_id_se... ^ in DIRECTORY LISTING(I replaced this) on line 22

Anyone got any ideas?

share|improve this question

2 Answers 2

Did you actually create the table with the column types of institutions.i_id and staff.u_id set to serial? Or create the sequence manually?

If the former, you don't need to explicitly use nextval anyway. If the latter, double-check the sequence name.

share|improve this answer

This would ideally be a comment, but I think it might have something to do with:

$'password' which should be '$password' on the fourth line of that example.

share|improve this answer
    
sorry yeah that's just a type in this submit form not in the actual code, I did have to check though! –  DanielD Sep 4 '12 at 10:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.