dtdt0454 2012-08-23 15:50
浏览 39
已采纳

PHP / MySQL INSERT功能逻辑错误

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条回答 默认 最新

  • duanbipu7601 2012-08-23 17:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题