使用php将表单数据发送到mysqli DTB失败(xampp环境)
下面是我的html表单代码:问题是我不知道如何使用xampp成功地将表单提交到mysql DTB。(数据不会发送到DTB)使用php将表单数据发送到mysqli DTB失败(xampp环境),php,forms,mysqli,Php,Forms,Mysqli,下面是我的html表单代码:问题是我不知道如何使用xampp成功地将表单提交到mysql DTB。(数据不会发送到DTB) 我的表格 Όνομα: 现在是我的php代码: <?php $servername = "localhost"; $username = "username"; $password = ""; $dbname = "mydb"; // Create connection $conn = new mysqli
我的表格
Όνομα:
现在是我的php代码:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Guests (firstname)
VALUES ('?')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
我的表格
Όνομα:
**test.php文件**
“?”将替换为整数、字符串、双精度或blob值
您放置了“?”,但忘了使用bind_param
准备它。更重要的是,您必须将$firstname
值传递到$stmt->bind_param(“s”,“$firstname”)代码>
更新代码
$firstname = $_POST['firstname'];
$sql = $conn->prepare("INSERT INTO Guests (firstname) VALUES (?)");
$sql->bind_param("s", $firstname);
if ($sql->execute() === TRUE) {
阅读
非常感谢您的帮助!不幸的是,我在提交时发现了这条消息:“警告:mysqli::uu construct():(HY000/1044):在C:\xampp\htdocs\NutriWebSolutions\test.php的第8行中,用户“@'localhost”对数据库“mydb”的访问被拒绝连接失败:用户“@'localhost”对数据库“mydb”的访问被拒绝。哦..,$username是root我已经编辑了我的代码,将数据库用户名更改为root,我以前没有注意到您错过了这一点。现在将授予访问权限再次感谢您的帮助!我没有投反对票其他人必须。。。现在我提交表单,blaank页面没有出现错误,但数据仍然没有传输到DTB:(很好,现在这意味着提交代码块没有执行,让我们试试else语句……再次检查代码……我只是添加了一个更新
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My Form</title>
<meta name="description" content="An interactive form">
</head>
<body>
<form action="test.php" method="post" id="Personalinfo">
<label for="fname">Όνομα:</label>
<input type="text" id="fname" name="firstname" placeholder="Όνομα
Πελάτη..">
<input type="submit" name="submitForm" value="Submit">
</body>
</html>
**test.php file**
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submitForm'])){
$firstname = $_POST['firstname'];
$sql = "INSERT INTO Guests (firstname)
VALUES ('{$firstname}')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}else{
echo "Are you sure you enter a firstname and the name of your html submit is submitForm";
}
$conn->close();
?>
$sql = "INSERT INTO Guests (firstname) VALUES ('?')";
$firstname = $_POST['firstname'];
$sql = $conn->prepare("INSERT INTO Guests (firstname) VALUES (?)");
$sql->bind_param("s", $firstname);
if ($sql->execute() === TRUE) {