简体   繁体   English

当在另一张表中添加行(列名=行ID)时,如何同时更改表(添加列)?

[英]How to alter a table (add a column) simultaneously when a row is added (column name = row id) in another table?

How to write code in MySQL such that when ever a row with a primary key id is added to a table, a corresponding column is added in another table such that the name of the column is equal to the id of the row just added. 如何在MySQL中编写代码,以便每当将具有主键ID的行添加到表中时,在另一表中添加相应的列,以使该列的名称等于刚添加的行的ID。

I tried the following but to no use. 我尝试了以下但没有用。

sqlQuery("INSERT INTO table1(name) VALUES('$name')");
$id = sqlQuery("SELECT id FROM table1 WHERE id = LAST_INSERT_ID()");
$id = mysqli_fetch_array($id);
$id = $id['id'];
sqlQuery("ALTER TABLE table2 ADD '$id' INT(2) NOT NULL");

sqlQuery - user defined function that return mysqli_query result. sqlQuery-返回mysqli_query结果的用户定义函数。

Any help would be great. 任何帮助都会很棒。 Also, I'm a newbie. 另外,我是新手。 Sorry if this is a silly question to ask. 抱歉,这是一个愚蠢的问题。

Make it OOP style and there is a var in the class that automatically returns the last updated item. 使其具有OOP样式,并且该类中有一个var,它会自动返回最近更新的项目。

$con = new mysqli(SQL_HOST, SQL_USER, SQL_PASSWORD, SQL_DATABASE); //do normal error checking with database connection
$sql = "INSERT INTO table1(name) VALUES('$name')";
$con->query($sql);
$sql2 = "ALTER TABLE table2 ADD '$con->insert_id' INT(2) NOT NULL"  //$con->insert_id is the parm you are looking for.
$con->query($sql2);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次添加新行时更新列表 - Update a column table everytime a new row is added 更改MySQL表列以匹配另一个表的ID - Alter MySQL table column to match id's from another table 通过将标题列名称与另一个表中的行值匹配来获取值 - get value by matching the header column name with row value in another table PHP将第一个表列ID添加到另一个表列ID - PHP add first table column id to another table column id 按行和按行列将表与另一个表进行比较 - compare table by row and per row column to another table 如何在一行中获取具有相同 id 但在另一列中具有不同值的数据并在 php 中的表中显示一个月 - How to fetch data with same id in a row but different value in another column and display one month into table in php PHP查询 - 当用户ID存储在另一个表中时,如何在表中添加新列并插入用户信息? - PHP queries - How to add new column and insert user info in table, when user id is stored in another table? 如何从javascript中的表中获取列名和第一行值? - How to get column name and first row value from table in javascript? PHP-如何根据其他行表创建某些列/字段 - PHP - how to create some column/field depending on another row table 数据表警告:表 id=DataTables_Table_0 - 为第 0 行第 7 列请求了未知参数“公司名称” - Datatables warning: table id=DataTables_Table_0 - Requested unknown parameter 'business name' for row 0, column 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM