PHP脚本未将列添加到mysql表

时间:2018-06-27 04:10:06

标签: php mysql

请帮助。以下代码根本不起作用。我也没有收到任何错误消息。我在PHPMyAdmin中检查数据库,但看不到新创建的列。我有user_info(数据库)> team-names(表)>我要制作的列...

这是我的index.php

<?php
include('connect-mysql.php');

$table = 'team-names';
$column = 'team6';

$add = mysqli_query($conn, "ALTER TABLE $table ADD $column VARCHAR(16) NOT NULL;");
?>

这是我随附的connect-mysql.php

[code]
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "player-info";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

?>
[/code]

谢谢。

2 个答案:

答案 0 :(得分:0)

在index.php中,您应该更改:

    <?php
    include('connect-mysql.php');

    $table = 'team-names';
    $column = 'team6';

    if($conn){
         $add = mysqli_query($conn, "ALTER TABLE {$table} ADD {$column} VARCHAR(16) NOT NULL;");
    }else{
         echo "Please check connection again!";
    }

    if($add){
        echo 'Add column successful!';
    }
    ?>

答案 1 :(得分:0)

您的查询未运行,因为它无法获取动态字段名称,对其进行如下更改,然后它将运行。

$add = mysqli_query($conn, "ALTER TABLE '".$table."' ADD '".$column."' VARCHAR(16) NOT NULL;");

OR

$add = mysqli_query($conn, "ALTER TABLE '$table' ADD '$column' VARCHAR(16) NOT NULL;");