gpt4 book ai didi

php - 无法插入PHP到MYSQL数据库

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:43 25 4
gpt4 key购买 nike

我可以从数据库中读取数据,但是当我向数据库中插入数据时,它说“注册成功!”,然后我检查我的数据库,它没有插入任何数据。

注册.php

    <!DOCTYPE HTML>
<html>
<head>
<title>Register page</title>
<br>
<font size="5" color="black"><b>&nbsp;Register Page<br><br></b></font>
</head>

<body background=" images/background.jpg">
<form action="index.php">
<input type="submit" value="Home">
</form>
<br>
<form action="Register.php" method="POST">
<fieldset>
<legend>Enter your information</legend>
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" required="required"/><br></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password" required="required"/><br></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" required="required"/><br></td>
</tr>
<tr>
<td>First Name:</td>
<td><input type="text" name="firstname" required="required"/><br></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastname" required="required"/><br></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><input type="date" name="dateofbirth" required="required" ><br></td>
</tr>
<tr>
<td><input type="submit" value="Register"/></td>
</tr>

</table>
</fieldset>
</form>


</body>
</html>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = mysql_real_escape_string($_POST['email']);
$dateofbirth= $_POST['dateofbirth'];
echo "user is :".$username;
$bool = true;
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("userdata") or die(mysql_error("Cannot Connect to Database"));
$query = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($query))
{
$table_users = $row['username'];
if($username == $table_users)
{
$bool = false;
Print '<script>alert("username is used!");</script>';
Print '<script>window.location.assign("Register.php");</script>';

}
}
if($bool)
{
mysql_query("INSERT INTO users ('username','password','email','firstname','lastname','dateofbirth') VALUES ('$username','$password','$email','$firstname','$lastname','$dateofbirth')");
Print '<script>alert("Successfully Registered!");</script>';
Print '<script>window.location.assign("Register.php");</script>';
}


}
?>

已解决:我使用 PDO 而不是 mysql_ 和 mysqli_。

最佳答案

纠正SQL:

mysql_query("INSERT INTO users (username','password','email','firstname','lastname','dateofbirth') VALUES ('$username','$password','$email','$firstname','$lastname','$dateofbirth')");

mysql_query("INSERT INTO users (username,password,email,firstname,lastname,dateofbirth) VALUES ('$username','$password','$email','$firstname','$lastname','$dateofbirth')");

解释:

在 MySQL 中,字段名没有用单引号括起来。相当它们可以用反引号 (`) 括起来以避免与保留关键字。

注意:不要使用mysql_* 函数。它们已被弃用,您可以使用 mysqli_*PDO 代替。

关于php - 无法插入PHP到MYSQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32970110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com