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

  • SEARCH
  • Home
  • 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 8474601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:40:21+00:00 2026-06-10T17:40:21+00:00

I recently asked a question about writing to multiple tables: PHP/MySQL insert into multiple

  • 0

I recently asked a question about writing to multiple tables: PHP/MySQL insert into multiple data tables on submit

I have now tried out this code and there are no errors produced in the actual code but the results I am getting are strange. When a user clicks register this ‘insert.php’ page is called and the code can be found below.

<?php

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

$conn = pg_connect("database connection information"); //in reality this has been filled
$result = pg_query($conn, "INSERT INTO institutions (i_id, name) VALUES (null, '$institution') RETURNING i_id");
$insert_row = pg_fetch_row($result);
$insti_id = $insert_row[0];

// INSTITUTION SAVED AND HAS ITS OWN ID BUT NO MEMBER OF STAFF ID

$resultTwo = pg_query($conn, "INSERT INTO staff VALUES (NULL, '$username', '$password', '$insti_id'");
$insert_rowTwo = pg_fetch_row($resultTwo);
$user_id = $insert_rowTwo[0];
// USER SAVED WITH OWN ID AND COMPANY ID
// ASSIGN AN INSTITUTION TO A STAFF MEMBER IF THE STAFF'S $company_id MATCHES THAT OF THE
// INSTITUION IN QUESTION
$update = pg_query($conn, "UPDATE institutions SET u_id = '$user_id' WHERE i_id = '$insti_id'");  

pg_close($conn);

?>

What the result of this is just the browser waiting for a server response but there it just constantly waits. Almost like an infinite loop I’m assuming. There are no current errors produced so I think it may be down to a logic error. Any ideas?

  • 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-10T17:40:23+00:00Added an answer on June 10, 2026 at 5:40 pm

    The errors:

    • RETURNING clause is missing in the second INSERT statement.

    • Provide an explicit list of columns for your second INSERT statement, too.

    • Don’t supply NULL in the INSERT statements if you want the column default (serial columns?) to kick in. Use the keyword DEFAULT or just don’t mention the column at all.

    The better solution:

    Use data-moidifying CTE, available since PostgreSQL 9.1 to do it all in one statement and save a overhead and round trips to the server. (MySQL knows nothing of the sort, not even plain CTEs).

    Also, skip the UPDATE by re-modelling the logic. Retrieve one id with nextval(), and make do with just two INSERT statements.

    Assuming this data model (you should have supplied that in your question):

    CREATE TABLE institutions(i_id serial, name text, u_id int);
    CREATE TABLE staff(user_id serial, username text, password text, i_id int);
    

    This one query does it all:

    WITH x AS (
        INSERT INTO staff(username, password, i_id) -- provide column list
        VALUES ('$username', '$password', nextval('institutions_i_id_seq'))
        RETURNING user_id, i_id
        )
    INSERT INTO institutions (i_id, u_id, name)
    SELECT x.i_id, x.user_id, '$institution'
    FROM   x
    RETURNING u_id, i_id; -- if you need the values back, else you are done
    

    Data model

    You might think about changing your data model to a classical n:m relationship.
    Would include these tables and primary keys:

    staff (u_id serial PRIMARY KEY, ...)
    institution (i_id serial PRIMARY KEY, ...)
    institution_staff (i_id, u_id, ...,  PRIMARY KEY(i_id, u_id)) -- implements n:m
    

    You can always define institution_staff.i_id UNIQUE, if a user can only belong to one institution.

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

Sidebar

Related Questions

Recently I have asked a question about what I should use to create self-contained
I recently asked a question about committing a new file but now I am
I recently asked a question about formatting JavaScript code in Vim. And I've also
I've recently asked a question about clipping an image via path at view's drawRect
Note: I originally asked this question about an hour ago but only recently realized
So recently, I asked a question about a class I created that extended SQLiteOpenHelper
I recently asked this question: MS SQL share identity seed amongst tables (Many people
I was recently asked this question in an interview. Lets suppose I have 2000
I recently asked a question about some problems I was having getting MIT Kerberos
I recently asked a question about creating elements with jquery. Specifically I needed input

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.