gpt4 book ai didi

php - 如何使用 MySQL 制作 PHP/HTML 表单?

转载 作者:行者123 更新时间:2023-11-29 12:50:30 24 4
gpt4 key购买 nike

我有一个 HTML 表单,用于将信息存储在 MySQL 数据库上。有用!但我想要一个唯一可以完成所有 PHP 扩展的文件。我在单击按钮时填充了数据库,但转而部署 PHP 文件,我不希望发生这种情况。

这是我的 HTML 代码:

<html>
<head></head>
<body>
<form id="form" name="form" action="insert.php" method="POST">
<label id="lbluser">Name:</label>
<input type="text" name="name" id="name" /><br/>
<label id="lbllastaname">Lastname:</label>
<input type="text" name="lstname" id="lstname" /><br/>
<label id="lblmail">E-mail:</label>
<input type="text" name="email" id="email" /><br/>
<label id="lblpassword">Password:</label>
<input type="password" name="password" id="password" /><br/>
<label id="lblpassword">Repeat password:</label>
<input type="password" name="passwordr" id="passwordr" /><br/>
<button type=”submit” name=”button” value=”insert”>OK</button>
</form>
</body>
</html>

这是我的 PHP 代码:

<?php
$mysqli = mysqli_connect("localhost", "user", "12345", "own_bd");

if (mysqli_connect_errno()) {
printf("Problem with connection: %s\n", mysqli_connect_error());
exit();
}
else {
$var_name = mysqli_real_escape_string($mysqli, $_POST['name']);
$var_lst = mysqli_real_escape_string($mysqli, $_POST['lstname']);
$var_mail = mysqli_real_escape_string($mysqli, $_POST['email']);
$var_pwd = mysqli_real_escape_string($mysqli, $_POST['password']);
$var_pwdr = mysqli_real_escape_string($mysqli, $_POST['passwordr']);
$sql = "INSERT INTO users_tbl (Name,Lastname,Mail,Pwd,PwdR) VALUES ('".$var_name."','".$var_lst."','".$var_mail."','".$var_pwd."','".$var_pwdr."')";
$res = mysqli_query($mysqli, $sql);

if ($res === TRUE) {
echo "User added.";
}
else {
printf("Error: %s\n", mysqli_error($mysqli));
}

mysqli_close($mysqli);
}
?>

我该怎么做?希望您的帮助。

isset 函数在此脚本中对我不起作用,但如果我尝试此代码,它会起作用!

<form action="" method="post">
<input type="submit" value="Click" name="addImg" />
</form>
<?
if (isset($_POST['addImg'])) {echo "haaallloooo";}
?>

救命!!!

最佳答案

如果我理解你的要求,HTML 和 PHP 可以在同一页面上。

请记住,更好的做法是将 processnig 代码放在文件顶部。

<?php
if(isset($_POST['button'])){
$mysqli = mysqli_connect("localhost", "user", "12345", "own_bd");

if (mysqli_connect_errno()) {
printf("Problem with connection: %s\n", mysqli_connect_error());
exit();
}
else {
$var_name = mysqli_real_escape_string($mysqli, $_POST['name']);
$var_lst = mysqli_real_escape_string($mysqli, $_POST['lstname']);
$var_mail = mysqli_real_escape_string($mysqli, $_POST['email']);
$var_pwd = mysqli_real_escape_string($mysqli, $_POST['password']);
$var_pwdr = mysqli_real_escape_string($mysqli, $_POST['passwordr']);
$sql = "INSERT INTO users_tbl (Name,Lastname,Mail,Pwd,PwdR) VALUES ('".$var_name."','".$var_lst."','".$var_mail."','".$var_pwd."','".$var_pwdr."')";
$res = mysqli_query($mysqli, $sql);

if ($res === TRUE) {
echo "User added.";
exit();
}
else {
printf("Error: %s\n", mysqli_error($mysqli));
}

}
mysqli_close($mysqli);
}
?>
<html>
<head></head>
<body>
<form id="form" name="form" action="" method="POST">
<label id="lbluser">Name:</label>
<input type="text" name="name" id="name" /><br/>
<label id="lbllastaname">Lastname:</label>
<input type="text" name="lstname" id="lstname" /><br/>
<label id="lblmail">E-mail:</label>
<input type="text" name="email" id="email" /><br/>
<label id="lblpassword">Password:</label>
<input type="password" name="password" id="password" /><br/>
<label id="lblpassword">Repeat password:</label>
<input type="password" name="passwordr" id="passwordr" /><br/>
<button type="submit" name="button" value="insert">OK</button>
</form>
</body>
</html>

关于php - 如何使用 MySQL 制作 PHP/HTML 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24815107/

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