无法在数据库中创建表并插入数据

时间:2016-05-11 10:10:24

标签: php sql

 <form id="login" name="login-form" method="POST" action="add.php">
   <div class="form-group">
      <label for="email">User Email Address</label>
      <input type="text" class="form-control" name="username" placeholder="Email address" id="username">
    </div>
    <div class="form-group">
      <label for="password">Password</label>
      <input type="password" class="form-control" name="password" placeholder="Password" id="password">
    </div>
    <button type="submit" class="btn btn-add" name="add">Add</button>

</form>

add.php

   <?php 
  include 'config/database.php';
  include 'functions.php';
  $user = $_POST['username'];
  $pass = $_POST['password'];
  addUser($user, $pass, $con);
?>

的functions.php

if (!function_exists('addUser')) {
    function addUser($user, $pass, $link){

      if(isset($_POST)){
      $user = sanitize($user, "varchar", $link);
      $pass = hash('sha256', $pass);
      $userTable = "CREATE TABLE IF NOT EXISTS 'user'(
          'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
          'email' varchar(60) DEFAULT NULL,
          'pass' varchar(60) DEFAULT NULL)";

      if(!$con->query($userTable)){
        echo "Table is created successfully";
      }
      else{
        echo "error: executing table";
      }

      $query = "INSERT INTO $userTable(email, pass) VALUES('?','?')";
      $result = $link->prepare($query);
      $result->bind_param('ss', $user, $pass);
      $result->execute();
    }
  }
}

没有功能的测试INSert,它工作。然后移动功能。 创建表似乎不起作用。继续添加用户进行测试,看不到添加新数据。不知道我哪里出错了。感谢帮助

1 个答案:

答案 0 :(得分:1)

在您的功能中尝试更改此

if(!$con->query($userTable)){
    echo "Table is created successfully";
}

if(!$link->query($userTable)){
    echo "Table is created successfully";
}