gpt4 book ai didi

php - 使用 php 保存 html 表单时出现错误 undefined variable 且所有值在 sql db 中保存为 0

转载 作者:行者123 更新时间:2023-11-29 06:57:39 27 4
gpt4 key购买 nike

HTML代码:

<html>
<head>
</head>
<body>
<p><h1>Student Information Input Form</h1></p>
<section>
<form method="POST" action="connection.php">
<p><b>Name:</b>&nbsp;<input type="text" id="name" name="name"></input></p>
<p><b>Username:</b>&nbsp;<input type="text" id="username" name="username"></input></p>
<p><b>Password:</b>&nbsp;<input type="password" id="password" name="password"></input></p>
<p><b>Email:</b>&nbsp;<input type="email" id="email" name="email"></input></p>
<p><input type="submit" id="submit" name="submit"></p>
</form>
</section>
</body>
</html>

PHP 代码:

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

//Create Connection
$conn=new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//get values from form
if (isset($_POST['name'])) {
$nameForm=$_POST['name'];
}
if (isset($_POST['username'])) {
$usernameForm=$_POST['username'];
}
if (isset($_POST['passsword'])) {
$passwordForm=$_POST['password'];
}
if (isset($_POST['email'])) {
$emailForm=$_POST['email'];
}

//Insert Values
$sql = "INSERT INTO userinfo (name, username, password, email) VALUES ('$nameForm', '$usernameForm', '$passwordForm', '$emailForm')";

//To check whether data is inserted properly or not
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

错误消息:

Notice: Undefined variable: passwordForm in C:\xampp\htdocs\Project2_php db\connection.php on line 28 New record created successfully

单击“提交”按钮时,我收到上述错误,并且在数据库中,所有值都存储为零。

最佳答案

这是一个拼写错误if (isset($_POST['passsword'])) {//密码

注意:还可以考虑切换到 PDO 语句

关于php - 使用 php 保存 html 表单时出现错误 undefined variable 且所有值在 sql db 中保存为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44955698/

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