我正在创建一个音乐网站,用户应该可以在其中将艺术家、专辑和歌曲添加到数据库并进行讨论。但是,我无法将用户输入注册表单的数据传输到数据库中的“用户”表。我不太确定出了什么问题,但我怀疑这是我的语法问题,因为我没有任何数据库连接问题。我一直在寻找几个小时,但我还没有找到解决方案。谁能帮帮我?
澄清一下:错误是在用户点击“提交”后,注册表单中的任何信息都没有输入到我的数据库中
注册.php:
<?php include 'header.php'; ?>
<form class="form" action="register.php" method="POST">
<fieldset>
<label for="firstName">First Name:</label>
<input type="text" name="firstName" placeholder="John Smith">
<br>
<label for="lastName">Last Name:</label>
<input type="text" name="lastName" placeholder="John Smith">
<br>
<label for="email">Email:</label>
<input type="email" name="email" value="some@one.com">
<br>
<label for="username">Username:</label>
<input type="text" name="username" value="helloitsme">
<br>
<label for="password">Password:</label>
<input type="password" name="password">
<br>
<label for="confirmPassword">Confirm Password:</label>
<input type="password" name="confirmPassword">
<br>
<label for="age">Age:</label>
<input type="number" name="age">
<br>
</fieldset>
<fieldset>
<input type="submit" name="submit" value="Register">
</fieldset>
</form>
注册.php:
<?php
session_start();
if ($_POST["password"] != $_POST["confirmPassword"]) {
$_SESSION['signUpBadPassword'] = true;
header('Location: signup.php');
}
// Re use connection stuffs
include "db.php";
// get connection
$conn = DB::connect();
$stmt = $conn->prepare("INSERT INTO users (firstName, lastName, email, username, password, age) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param(
"sssssi",
$_POST["firstName"],
$_POST["lastName"],
$_POST["email"],
$_POST["username"],
$_POST["password"],
$_POST["age"]
);
$stmt->execute();
// Close the connection
$conn->close();
header('Location: index.php');
?>
和 db.php:
<?php
class DB
{
public static function connect()
{
$hostname = 'localhost';
$username = 'user';
$password = 'password';
$dbName = 'dbname';
$connection = new mysqli($hostname, $username, $password, $dbName);
if ($connection->connect_error) {
die('Failed to connect');
}
return $connection;
}
}
最佳答案
绑定(bind)参数时请检查是否设置了 post 值,即检查以下代码:
$stmt = $conn->prepare("INSERT INTO users (firstName, lastName, email, username, password, age) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param(
"sssssi", $firstName, $lastName, $email, $userName, $passWord, $age
);
//check firstname is set or not
if(isset($_POST['firstName']))
{
$firstName= $_POST['firstName'];
}
else
{
$firstName= NULL;
}
//check lastname is set or not
if(isset($_POST['lastName']))
{
$lastName= $_POST['lastName'];
}
else
{
$lastName= NULL;
}
//check email is set or not
if(isset($_POST['email']))
{
$email= $_POST['email'];
}
else
{
$email= NULL;
}
//check username is set or not
if(isset($_POST['username']))
{
$userName= $_POST['username'];
}
else
{
$userName= NULL;
}
//check password is set or not
if(isset($_POST['password']))
{
$passWord= $_POST['password'];
}
else
{
$passWord= NULL;
}
//check age is set or not
if(isset($_POST['age']))
{
$age= $_POST['age'];
}
else
{
$age= 0;
}
关于php - 无法使用 PHP 将数据插入我的 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36976409/