gpt4 book ai didi

php - 将表单值提交到 MySql

转载 作者:行者123 更新时间:2023-11-29 22:24:55 25 4
gpt4 key购买 nike

我在 php 中制作了一个注册页面,其中我对新用户注册进行了验证。问题是这些值没有插入到 MySql 数据库中。我希望将数据插入到 MySql 中。还有一个问题是,单击“提交”时,数据字段应该为空。请帮忙!

<?php session_start(); ?>
<html>
<head><title>MRA</title>
<style>
#heading{ text-align:right;font-size: 25px;float:right;margin:50px 20px;}
a:hover{ color:#BEBEBC; }
a{ color:black;}
#img{margin:10px 10px;}
</style>
</head>
<body background="a9.jpg">
<img id="img" src="HITECHLOGO.jpg" width="150px" height="100px">
<h1 align="center" style="position:absolute;top:30px; left:450px;">USER REGISTRATION</h1>
<div id="heading">
<a href="adminhomepage.php" style="text-decoration:none;">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?php echo ucwords($_SESSION['usr_name']); ?></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="registration.php" style="text-decoration:none;">Add User</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="logout.php" style="text-decoration:none;">Logout</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="UserManual.pdf" target="_blank" style="text-decoration:none;">Help</a></div>
<hr width="100%">
<script type="text/javascript">
var specialKeys = new Array();
specialKeys.push(8); //Backspace
function IsNumeric(e) {
var keyCode = e.which ? e.which : e.keyCode
var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;
}
</script>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $mobErr = $uidErr = $pwdErr =$roleErr= "";
$txtname = $gender = $txtmob = $txteid = $txtuid = $txtpwd = $role= "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["txtname"]))
{
$nameErr = "Name is required";
} else
{
$txtname = test_input($_POST["txtname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$txtname)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["txteid"]))
{
$emailErr = "Email is required";
} else
{
$txteid = test_input($_POST["txteid"]);
// check if e-mail address is well-formed
if (!filter_var($txteid, FILTER_VALIDATE_EMAIL))
{
$emailErr = "Invalid email format";
}
}
if (empty($_POST["gender"]))
{
$genderErr = "Gender is required";
} else
{
$gender = test_input($_POST["gender"]);
}
if (empty($_POST["txtmob"]))
{
$mobErr = "Mobile No. is required";
} else
{
$txtmob = test_input($_POST["txtmob"]);
//check only numbers are given
if(preg_match("/^d{10}$/", $txtmob))
{
$mobErr = "Input digits (0 - 9)";
}
}
if (empty($_POST["txtuid"]))
{
$uidErr = "User Id is required";
} else
{
$txtuid = test_input($_POST["txtuid"]);
}
if (empty($_POST["txtpwd"]))
{
$pwdErr = "Password is required";
} else
{
$txtpwd = test_input($_POST["txtpwd"]);
}
if (empty($_POST["role"]))
{
$roleErr = "Role is required";
} else
{
$role = test_input($_POST["role"]);
}
}
else
{
if (!empty($_POST))
{
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="testmra"; // Database name
// Connect to server and select databse.
$conn=mysqli_connect($host,$username,$password) or die("cannot connect");
mysqli_select_db($conn,$db_name);
$name = mysqli_real_escape_string($conn, $_POST['txtname']);
$gender =$_POST['gender'];
$mobile = mysqli_real_escape_string($conn, $_POST['txtmob']);
$email = mysqli_real_escape_string($conn, $_POST['txteid']);
$username = mysqli_real_escape_string($conn, $_POST['txtuid']);
$userpass = mysqli_real_escape_string($conn, $_POST['txtpwd']);
$role= $_POST['role'];
$res=mysqli_query($conn,"SELECT username FROM newuser WHERE username='$username'");
$row=mysqli_fetch_row($res);
if($row>0)
{
echo '<script language="javascript">';
echo 'alert("Username '.$username.' already been selected")';
echo '</script>';
}
else
{
$sql="INSERT INTO newuser (name,gender,contactno,emailid,username,userpass,role)VALUES('$name','$gender','$mobile','$email','$username','$userpass','$role')";
if (mysqli_query($conn,$sql))
{
header("location:registration.php");
}
else
{
die('Error: Cannot connect to db' );
}
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<table align="center" cellpadding="5" cellspacing="5">
<tr><td colspan="2" align="left"><font color="red">All fields are mandatory</font></td></tr>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<tr><td>Full Name &nbsp; : </td><td><input type="text" name="txtname" value="<?php echo $txtname ?>">&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $nameErr; ?></td></tr>
<tr><td>Gender &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </td><td><input type="radio" name="gender" value="Male" checked="checked">Male <input type="radio" name="gender" value="Female">Female&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $genderErr;?></td></tr>
<tr><td>Mobile No. : (+91)</td><td><input name="txtmob" maxlength="10" type="text" id="text1" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" /><span id="error" style="color: Red; display: none"></span>&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $mobErr; ?></td></tr>
<tr><td>Email Id &nbsp;&nbsp;&nbsp;&nbsp; : </td><td><input type="text" name="txteid" value="<?php echo $txteid ?>">&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $emailErr;?></td></tr>
<tr><td>User Id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </td><td><input type="text" name="txtuid" value="<?php echo $txtuid ?>">&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $uidErr;?></td></tr>
<tr><td>Password &nbsp;&nbsp;&nbsp; : </td><td><input type="password" name="txtpwd" value="<?php echo $txtpwd ?>">&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $pwdErr;?></td></tr>
<tr><td>Role : </td><td><input type="radio" name="role" value="User"
checked="checked">User<input type="radio" name="role"value="Admin">Admin<font color="red"><?php echo
$roleErr;?></td></tr>
<tr><td><input type="submit" value="Submit" name="btnsave"></td></tr>
</form>
</tr>
</table>
</body>
</html>

最佳答案

在调用 header() 之前无法输出任何 HTML,因此必须在末尾而不是开头执行此操作。您还应该在 header('location:registration.php') 之后调用 exit(),因为重定向时输出会被忽略。这样就解决了插入成功时表单不被清除的问题。

我没有发现 INSERT 语句有问题,因此我无法解释为什么它没有插入。

<?php session_start(); ?>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $mobErr = $uidErr = $pwdErr =$roleErr= "";
$txtname = $gender = $txtmob = $txteid = $txtuid = $txtpwd = $role= "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["txtname"]))
{
$nameErr = "Name is required";
} else
{
$txtname = test_input($_POST["txtname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$txtname)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["txteid"]))
{
$emailErr = "Email is required";
} else
{
$txteid = test_input($_POST["txteid"]);
// check if e-mail address is well-formed
if (!filter_var($txteid, FILTER_VALIDATE_EMAIL))
{
$emailErr = "Invalid email format";
}
}
if (empty($_POST["gender"]))
{
$genderErr = "Gender is required";
} else
{
$gender = test_input($_POST["gender"]);
}
if (empty($_POST["txtmob"]))
{
$mobErr = "Mobile No. is required";
} else
{
$txtmob = test_input($_POST["txtmob"]);
//check only numbers are given
if(preg_match("/^d{10}$/", $txtmob))
{
$mobErr = "Input digits (0 - 9)";
}
}
if (empty($_POST["txtuid"]))
{
$uidErr = "User Id is required";
} else
{
$txtuid = test_input($_POST["txtuid"]);
}
if (empty($_POST["txtpwd"]))
{
$pwdErr = "Password is required";
} else
{
$txtpwd = test_input($_POST["txtpwd"]);
}
if (empty($_POST["role"]))
{
$roleErr = "Role is required";
} else
{
$role = test_input($_POST["role"]);
}
}
else
{
if (!empty($_POST))
{
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="testmra"; // Database name
// Connect to server and select databse.
$conn=mysqli_connect($host,$username,$password) or die("cannot connect");
mysqli_select_db($conn,$db_name);
$name = mysqli_real_escape_string($conn, $_POST['txtname']);
$gender =$_POST['gender'];
$mobile = mysqli_real_escape_string($conn, $_POST['txtmob']);
$email = mysqli_real_escape_string($conn, $_POST['txteid']);
$username = mysqli_real_escape_string($conn, $_POST['txtuid']);
$userpass = mysqli_real_escape_string($conn, $_POST['txtpwd']);
$role= $_POST['role'];
$res=mysqli_query($conn,"SELECT username FROM newuser WHERE username='$username'");
$row=mysqli_fetch_row($res);
if($row>0)
{
echo '<script language="javascript">';
echo 'alert("Username '.$username.' already been selected")';
echo '</script>';
}
else
{
$sql="INSERT INTO newuser (name,gender,contactno,emailid,username,userpass,role)VALUES('$name','$gender','$mobile','$email','$username','$userpass','$role')";
if (mysqli_query($conn,$sql))
{
header("location:registration.php");
exit();
}
else
{
die('Error: Cannot connect to db' );
}
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<html>
<head><title>MRA</title>
<style>
#heading{ text-align:right;font-size: 25px;float:right;margin:50px 20px;}
a:hover{ color:#BEBEBC; }
a{ color:black;}
#img{margin:10px 10px;}
</style>
</head>
<body background="a9.jpg">
<img id="img" src="HITECHLOGO.jpg" width="150px" height="100px">
<h1 align="center" style="position:absolute;top:30px; left:450px;">USER REGISTRATION</h1>
<div id="heading">
<a href="adminhomepage.php" style="text-decoration:none;">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<?php echo ucwords($_SESSION['usr_name']); ?></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="registration.php" style="text-decoration:none;">Add User</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="logout.php" style="text-decoration:none;">Logout</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="UserManual.pdf" target="_blank" style="text-decoration:none;">Help</a></div>
<hr width="100%">
<script type="text/javascript">
var specialKeys = new Array();
specialKeys.push(8); //Backspace
function IsNumeric(e) {
var keyCode = e.which ? e.which : e.keyCode
var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;
}
</script>
<table align="center" cellpadding="5" cellspacing="5">
<tr><td colspan="2" align="left"><font color="red">All fields are mandatory</font></td></tr>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<tr><td>Full Name &nbsp; : </td><td><input type="text" name="txtname" value="<?php echo $txtname ?>">&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $nameErr; ?></td></tr>
<tr><td>Gender &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </td><td><input type="radio" name="gender" value="Male" checked="checked">Male <input type="radio" name="gender" value="Female">Female&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $genderErr;?></td></tr>
<tr><td>Mobile No. : (+91)</td><td><input name="txtmob" maxlength="10" type="text" id="text1" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" /><span id="error" style="color: Red; display: none"></span>&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $mobErr; ?></td></tr>
<tr><td>Email Id &nbsp;&nbsp;&nbsp;&nbsp; : </td><td><input type="text" name="txteid" value="<?php echo $txteid ?>">&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $emailErr;?></td></tr>
<tr><td>User Id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </td><td><input type="text" name="txtuid" value="<?php echo $txtuid ?>">&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $uidErr;?></td></tr>
<tr><td>Password &nbsp;&nbsp;&nbsp; : </td><td><input type="password" name="txtpwd" value="<?php echo $txtpwd ?>">&nbsp;&nbsp;&nbsp;<font color="red"><?php echo $pwdErr;?></td></tr>
<tr><td>Role : </td><td><input type="radio" name="role" value="User"
checked="checked">User<input type="radio" name="role"value="Admin">Admin<font color="red"><?php echo
$roleErr;?></td></tr>
<tr><td><input type="submit" value="Submit" name="btnsave"></td></tr>
</form>
</tr>
</table>
</body>
</html>

关于php - 将表单值提交到 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30363974/

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