gpt4 book ai didi

php - 使用 php 向 mySQL 提交一个 html 表单

转载 作者:行者123 更新时间:2023-11-29 04:35:32 25 4
gpt4 key购买 nike

对此很陌生,我目前正在尝试为我的网站创建一个登录系统。我已经创建了一个 html 登录表单,我计划将其用于用户创建帐户。我已经创建了一个 php 页面,其中包含我的代码以连接到如下所示的服务器。

当我填写表格时,我没有得到任何输出。我不确定 php 代码是否在错误的位置(它作为一个单独的文件)或者没有输出。当提交表单时,当我在测试时手动提交时,数据库似乎没有改变。

我的最终目标是能够将用户添加到我数据库中名为 users 的表中。

这是我的登录表单代码:

 <body>

<h2>Sign Up</h2>

<p></p>

<form action="Create_User.php" method="post">
<div class="imgcontainer">
<img src="http://fc05.deviantart.net/fs70/f/2012/361/1/6/albert_einstein_by_zuzahin-d5pcbug.jpg" alt="Einstein the lad" class="img" />
</div>

<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Please Enter your desired Username" name="username" required />

<label><b>Password</b></label>
<input type="password" placeholder="Please Enter Your Desired Password" name="password" required />

<label><b>Email Address</b></label>
<input type="email" placeholder="Please Enter Your Email Address" name="email" required />

<label><b>Date Of Birth</b></label>
<input type="date" name="date_of_birth" required />

<label><b>First Name</b></label>
<input type="text" placeholder="Please Enter your first name" name="first_name" required />

<label><b>Surname</b></label>
<input type="text" placeholder="Please Enter your surname" name="surname" required />

</div>

<div class="container" style="background-color: #f1f1f1">
<button type="submit">Sign Up</button>
<button class="signinbtn" onclick="location.href='/AccountRelatedPages/SignIn.aspx'">Already have an account? Sign in here</button>
</div>
</form>

</body>

这是我的 php 文件中的代码:

<?php
$servername = "localhost";
$username = "root";
$password = "rootpass";
$dbname = "synther_physics";


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO users (username, password, email, date_of_birth, first_name, surname)
VALUES ('<?php echo $_POST[$username];', '<?php echo $_POST[$password];', '<?php echo $_POST[$email], <?php echo $_POST[$date_of_birth];, <?php echo $_POST[$first_name], <?php echo $_POST[$surname];')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

再次对这一切非常陌生,所以我尽我最大的努力让我的头脑清醒,所以请记住这一点。

谢谢。

最佳答案

将所有评论、sql 注入(inject)、password_hash() 放在一起。对于 sql 注入(inject)保护,您需要使用准备好的语句。我不会说太多,很多重要的事情都在评论中说了,希望你都经历过,因为我经历过。

你的代码应该是这样的:

<?php
$servername = "localhost";
$username = "root";
$password = "rootpass";
$dbname = "synther_physics";



//Validate user inputs
$username = $_POST['username'];

$password = $_POST['password'];

$hash = password_hash($password, PASSWORD_DEFAULT);

$email = $_POST['email']; //VALIDATE the email

$dob = $_POST['date_of_birth'];
$fname = $_POST['first_name'];


$sname = $_POST['surname'];

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO users (username, password, email, date_of_birth, first_name, surname)
VALUES (?,?,?,?,?,?)";

$stmt = $conn->prepare($sql);

$stmt->bind_param("ssssss", $username, $hash, $email, $dob, $fname, $sname);

if ($stmt->execute()) {

echo "New record created successfully";
} else {

echo "Error : " . $conn->error; // on dev mode only

// echo "Error, please try again later"; //live environment
}

$conn->close();
?>

编辑:

如果你的 php 和 html 在同一个文件上,那么为了避免 undefined index 通知,你需要在处理之前检查表单是否已提交。您需要做的是为您的表单按钮添加一个名称属性。

然后检查表单是否提交。

<?php
$servername = "localhost";
$username = "root";
$password = "rootpass";
$dbname = "synther_physics";



//Validate user inputs
if(isset($_POST['buttonName'])){


$username = $_POST['username'];

$password = $_POST['password'];

$hash = password_hash($password, PASSWORD_DEFAULT);

$email = $_POST['email']; //VALIDATE the email

$dob = $_POST['date_of_birth'];
$fname = $_POST['first_name'];


$sname = $_POST['surname'];

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO users (username, password, email, date_of_birth, first_name, surname)
VALUES ('?,?,?,?,?,?')";

$stmt = $conn->prepare($sql);

$stmt->bind_param("ssssss", $username, $hash, $email, $dob, $fname, $sname);

if ($stmt->execute()) {

echo "New record created successfully";
} else {

echo "Error : " . $conn->error; // on dev mode only

// echo "Error, please try again later"; //live environment
}

$conn->close();
}
?>

您还需要检查字段是否已设置且不为空。

关于php - 使用 php 向 mySQL 提交一个 html 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43187822/

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