0
    <?php  

    $db_host = "localhost"; 
    $db_username = "root"; 
    $db_pass = "";
    $db_name = "onlinestores"; 

      // Run the actual connection here  
    $connect_dude=mysqli_connect("$db_host","$db_username","$db_pass","$db_name");


    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
     }
    ?>

    //creating table
    <?php
    include "connect_to_db.php";
    $sql_command="CREATE TABLE admin(
        id NOT NULL auto_increment,
        username varchar(16) NOT NULL,
        password varchar(16) NOT NULL,
        last_login date NOT NULL,
        PRIMARY KEY(id),
        UNIQUE KEY username(username)
        )";
    if(mysqli_query($connect_dude,"$sql_command")){
    echo "ookie dookie";
    }else {
    echo "it didn't work";
    }
    ?>  

The code doesn't create any table in the database and shows me echo statement "it didn't work".

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
user2943607
  • 169
  • 1
  • 9

3 Answers3

2

Your SQL has syntax errors.

  • id column misses int
  • UNIQUE KEY username(username) should be UNIQUE KEY(username)

Try this:

$sql_command = "CREATE TABLE admin 
(id int NOT NULL auto_increment,
username varchar(16) NOT NULL,
password varchar(16) NOT NULL,
last_login date NOT NULL,
PRIMARY KEY(id),
UNIQUE KEY(username))";

To catch errors in mysql calls, use mysqli_error()

else {
    echo "it didn't work because: " . mysqli_error($connect_dude);
}
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
grebneke
  • 4,414
  • 17
  • 24
1

Try like this, make sure you have proper names for variables before pushing code on production,

connect_to_db.php file:

<?php
  $db_host = "localhost"; 
  $db_username = "root"; 
  $db_pass = "";
  $db_name = "onlinestores"; 

  // Run the actual connection here  
  $mysqli=mysqli_connect($db_host, $db_username, $db_pass, $db_name);

  if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
  }
?>

create_table.php file:

<?php
  include "connect_to_db.php";

  $sql_command="CREATE TABLE admin(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(16) NOT NULL UNIQUE KEY,
    pass VARCHAR(16) NOT NULL,
    last_login DATE NOT NULL
  )";

  if($mysqli->query($sql_command) === TRUE)
    echo "ookie dookie";
  else
    echo "it didn't work";
?>
Adil Abbasi
  • 3,161
  • 1
  • 40
  • 35
0

replace your code:

$db_host = "localhost"; 
    $db_username = "root"; 
    $db_pass = "";
    $db_name = "onlinestores"; 

      // Run the actual connection here  
    $connect_dude=mysqli_connect("$db_host","$db_username","$db_pass","$db_name");


    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
     }
    ?>

    //creating table
    <?php
    include "connect_to_db.php";
    $sql_command="CREATE TABLE admin(
        id int NOT NULL auto_increment,
        username varchar(16) NOT NULL,
        password varchar(16) NOT NULL,
        last_login date NOT NULL,
        PRIMARY KEY(id),
        UNIQUE KEY username(username)
        )";
    if(mysqli_query($connect_dude,"$sql_command")){
    echo "ookie dookie";
    }else {
    echo "it didn't work";
    }
    ?>  
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Guru
  • 621
  • 1
  • 4
  • 12