2016-05-11 83 views
-1
<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(); 
    } 
    } 
} 

測試插入,而不功能和它的工作。然後進入功能。 創建表似乎不工作。保持添加用戶進行測試並看到沒有添加新數據。不知道我錯了哪裏。欣賞幫助

+0

你說 「似乎不起作用」。你有任何錯誤消息?你調試過,以確保你沒有捕捉並忽略錯誤?你是否直接連接到數據庫來檢查表的存在和/或狀態? – MatBailie

+2

看起來你的addUser函數接受'$ link',你後面引用'$ con'(在if語句中) – Dale

+0

@MatBailie - 我試過try,catch和finally,但是它給了我錯誤try try finally finally can not be used。並且連接到數據庫並檢查其是否存在。我學習PHP,我看到空白頁,並沒有錯誤打印 – joe

回答

1

在您的函數嘗試修改此

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

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

的情況下運行查詢,它根本不會創建表。不知道爲什麼 – joe

相關問題