Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8488317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:29:55+00:00 2026-06-10T21:29:55+00:00

I have a PostGreSQL database with the following data model: CREATE TABLE staff (s_id

  • 0

I have a PostGreSQL database with the following data model:

CREATE TABLE staff (s_id serial UNIQUE, name, username, password, email)
CREATE TABLE institution (i_id serial UNIQUE, name);
CREATE TABLE isStaffOf (i_id, s_id); //foreign key references

When a user submits the form I have a PHP script which writes the information to first two data tables and that automatically generates the s_id and i_id values. Great!

I’ve tried out a few PHP modifications to get the system writing both the s_id and i_id into the isStaffOf relation so it can enforce the explicit 1..1 relationship required for my project but on submit it says I have an insufficient data type. See below for the PHP code.

    $name = $_POST["name"];
    $username = $_POST["username"];
    $password = $_POST["password"];
    $email = $_POST["email"];
    $institution = $_POST["institution"];

    $conn = pg_connect("host=***** port=**** dbname=****** user=**** password=******");
    $staffWrite = pg_query("INSERT INTO staff(name, username, password, email) VALUES ('$name', '$username', '$password', '$email')");
    $instiWrite = pg_query("INSERT INTO institution(name) VALUES ('$institution')");

    $instiFK=pg_query("SELECT i_id FROM institution WHERE name='$institution'");
    $staffFK=pg_query("SELECT s_id FROM staff WHERE name='$username'");

    $sql=("INSERT INTO isstaffof(i_id, s_id) VALUES ('$instiFK', '$staffFK')");
    $result = pg_query($sql);

That is the script I have at the moment but its not working. Any ideas on how to fix this so that when a user submits all the data tables will be filled and the referential integrity enforced? I’m almost there and still trying things out but to no avail.

The error message:

 ERROR: invalid input syntax for integer: "Resource id #4" LINE 1: INSERT INTO isstaffof(i_id, s_id) VALUES ('Resource id #4', ...

Please let me know if you need more explanation but I’m sure its clear what I want to achieve and I’m convinced I can do it without so many pg_query calls.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:29:57+00:00Added an answer on June 10, 2026 at 9:29 pm

    As of PostgreSQL version 9.1, you can use a single query to INSERT in all 3 tables, a writable common table expression:

    WITH 
    step_1 AS(
        INSERT INTO staff (name) VALUES('Frank') RETURNING s_id
    ),
    step_2 AS (
        INSERT INTO institution (name) VALUES('PostgreSQL') RETURNING i_id
    )
    INSERT INTO isStaffOf (i_id, s_id) SELECT i_id,s_id FROM step_1, step_2;
    

    Simple, fast and reliable.

    You could do some other tricks as well, when some records already exists and just want to select the primary key value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table and sequence in my postgresql-8.4 database: CREATE TABLE complexobjectpy
I have the following table in my postgreSQL 8.3.14 database timestamp status 2012-03-12 19:15:01
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
Suppose we have a PostgreSQL database with two tables A, B. table A columns:
I have a Ruby/Rails app. I have an artists table in my postgresql database
I have a log table with the following format (and sample data): date |
I have a database in PostgreSQL which is named DATA in all caps. When
I have the following code to insert do an insert into my postgresql database
I have a postgresql database and am using it for a project which handles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.