我已经能够连接到我的数据库了。手动使用我自己的输入和VALUES ('$name', '$email', '$userPassword')";
代码将数据插入表中但由于某种原因,当我尝试将上述变量传递给我的数据库时,它会将空白数据输入到我的表中。这是我的welcome.php
页面的PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "@Password";
$dbname = "accounts";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$name = mysqli_real_escape_string($link, $_POST['Ausername']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$userPassword = mysqli_real_escape_string($link, $_POST['Apassword']);
$sql = "INSERT INTO users (username, email, password)
VALUES ('$name', '$email', '$userPassword')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . mysqli_error($conn);
}
mysqli_close($conn);
?>
请注意,在欢迎页面上,以下代码将从我的表单中传递信息并正确显示,但不会将其添加到我的数据库中:
<html>
<body>
Welcome <?php echo $_POST["Ausername"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
password is <?php echo $_POST["Apassword"]; ?>
</body>
</html>
我的表单代码:
<form class="form-inline" action="welcome.php" method="post">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" id="Ausername" placeholder="Username" name="Ausername" required>
</div>
<br>
<br>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input type="text" class="form-control" id="email" placeholder="Email" name="email" required>
</div>
<br>
<br>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="text" class="form-control" id="Apassword" placeholder="Password" name="Apassword" required>
</div>
<br>
<br>
<br>
<button type="submit" class="btn btn-primary" id="button" name="submit" value="submit">
<i class="fa fa-user-plus"></i>
Sign Up
</button>
<br>
<br>
</form>
答案 0 :(得分:1)
$link
,您正在使用$conn
创建与数据库的连接,因此您需要更改
$name = mysqli_real_escape_string($link, $_POST['Ausername']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$userPassword = mysqli_real_escape_string($link, $_POST['Apassword']);
到
$name = mysqli_real_escape_string($conn, $_POST['Ausername']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$userPassword = mysqli_real_escape_string($conn, $_POST['Apassword']);