【问题标题】:Editing table data within PHP after adding a new column to MYSQL table [closed]向MYSQL表添加新列后在PHP中编辑表数据[关闭]
【发布时间】:2013-04-14 13:25:36
【问题描述】:

我正在使用 PHP 和 MYSQL 创建一个有 3 个页面的网站;插入、更新和删除。插入页面将新列添加到数据库表中。在我的更新页面中,我希望能够查看添加了新列的表中的所有数据。我该怎么办?即如果在插入之后现在有 11 列而不是之前的 10 列,我希望显示更新页面

col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 | col10 | col11

data | data | data | data | data | data | data | data | data | data | data

【问题讨论】:

  • 1) 运行选择查询 2) 检索数据 3) 输出 html。
  • 为什么在php代码中有修改程序?
  • 您要添加列 - 真的吗?还是行?
  • 因为它是一个职业网站,其中有 10 个职业和 10 个结果。评估中的每个问题的评分为 1-5,在我的插入查询中,它为新问题结果添加了一个新列
  • 为什么不将results 与id | question | result | 放在表中,并且每个问题都将是新的行,而不是列?

标签: php mysql insert col


【解决方案1】:

这是一个我的表的例子:

<table>
    <tr>

<?php
$mysqli = new mysqli("localhost", "user", "pass", "test");
/*
 * CREATE TABLE `test` (
 `col1` int(11) NOT NULL,
 `col2` int(11) NOT NULL
... // until col5
 */
$query = "SELECT column_name FROM information_schema.columns 
    WHERE table_name = 'test' AND table_schema = 'test'";
$result = $mysqli->query($query);

// INSERT INTO `test`.`test` (`col1`, `col2`, `col3`, `col4`, `col5`)
// VALUES ('10', '11', '12', '13', '14'), ('15', '16', '17', '18', '10');

$query2 = "SELECT * FROM test";
$result2 = $mysqli->query($query2);


while ($row = $result->fetch_assoc()) {

    ?>
        <td><?=$row['column_name'];?></td>

<?php
}
?>
        </tr>
        <tr>

        <?php
 while ($res = $result2->fetch_assoc()) {

    $count_columns = $result->num_rows;

        for ($i = 1; $i <= $count_columns; $i++) {
     ?>
        <td><?=$res['col'.$i.''];?></td>
        <?php
        }
        ?>
        </tr>
        <tr>
<?php

 }

输出:

col1    col2    col3    col4    col5
 10     11      12       13     14
 15     16      17       18     10

这是在不知道列名及其数量的情况下。他们有相同的前缀“col”就足够了。

添加一个新列后:

    ALTER TABLE `test` ADD `col6` INT NOT NULL 
    UPDATE test SET col6 = col1+1

相同的代码(没有任何更改)产生:

col1    col2    col3    col4    col5    col6
 10     11      12       13     14      11
 15     16      17       18     10      16

P.S.:我仍然不鼓励这种表结构,您需要动态添加列

【讨论】:

    【解决方案2】:

    即使从 PHP 添加列(更改过程)对我来说也是不好的做法,可能会有不同的解决方案。您可以从 information_schema 中检索列:

    SELECT column_name
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_name = 'yourTable';
    
    $row['column_name']; //will output the actual result of columns for that table
    

    对于实际结果,您可以将它们命名为“col1,col2”,这样您就可以遍历结果并接收变量,如

    $row['col'.$i.''];
    

    然后您可以收到每一列的结果,以及它们的名称。

    【讨论】:

    • 所以我必须从 INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'yourTable' 中执行 SELECT col1、col2 等操作; $row['col1'],$row['col2'];等等?当然,如果我这样做,它只会显示我已经拥有的列,而无需手动添加代码?
    • 否,SELECT column_name FROM information_schema.columns WHERE table_name ='yourtable' 将立即检索该表的列名。您还可以对它们进行计数,传递给变量,然后进行简单的for 循环,将数字分配给字符串col。
    • 啊,对了,我现在和你在一起.. 然后我可以用更多代码编辑其中的信息吗?
    • 我已经发布了整个示例的新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2013-06-18
    • 1970-01-01
    • 2015-08-27
    • 2020-11-19
    相关资源
    最近更新 更多