-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();
}
}
}
测试插入,而不功能和它的工作。然后进入功能。 创建表似乎不工作。保持添加用户进行测试并看到没有添加新数据。不知道我错了哪里。欣赏帮助
你说 “似乎不起作用”。你有任何错误消息?你调试过,以确保你没有捕捉并忽略错误?你是否直接连接到数据库来检查表的存在和/或状态? – MatBailie
看起来你的addUser函数接受'$ link',你后面引用'$ con'(在if语句中) – Dale
@MatBailie - 我试过try,catch和finally,但是它给了我错误try try finally finally can not be used。并且连接到数据库并检查其是否存在。我学习PHP,我看到空白页,并没有错误打印 – joe