1

Please help. The following code simply does not work. I get no error messages either. I check my database in PHPMyAdmin and I do not see the newly created column. I have user_info(database) > team-names(table) > the column I'm trying to make...

Here's my 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;");
?>

Here's my included connect-mysql.php

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

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

?>
[/code]

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • `if(!$add){ echo mysqli_error($conn); }` Try to debug like this .and not necessary to put `;` at the end for query string . – JYoThI Jun 27 '18 at 04:23
  • Possible duplicate of [When to use single quotes, double quotes, and back ticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql) – Qirel Jun 27 '18 at 04:25
  • You should really avoid using any names in a database with a hyphen (`-`). – Qirel Jun 27 '18 at 04:26

2 Answers2

0

In index.php you should change:

    <?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!';
    }
    ?>
binhhoang18
  • 589
  • 6
  • 9
0

Your query is not running because it is not able to get the dynamic field name , change it slightly as below and it will run .

$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;");
PHP Web
  • 257
  • 3
  • 8