1

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?

2 Answers 2

1

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.

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

Comments

0

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.

1 Comment

sorry yeah that's just a type in this submit form not in the actual code, I did have to check though!

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.