我是PHP的新手,在HTML表单上工作,将数据提交到PHP页面,该页面应该将数据插入MySQL。表格是一个简单的登记表格。当我点击提交时,URL被重定向,但我在页面上看不到任何内容。这是空白的。我检查了它显示空白的视图页面源。
如果这是将数据传递给PHP的正确方法,请告诉我。提前谢谢。
这是HTML页面,index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Connection">
<meta name="author" content="Suman">
<title>Login or Register </title>
<link rel="stylesheet" href="css/normalize.css" />
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Scope+One" rel="stylesheet">
<link rel="stylesheet" href="css/form.css" />
<link rel="stylesheet" href="css/home.css" /> </head>
<body>
<header>
<h1> Welcome to Connections </h1> </header>
<section>
<h2> Already a member. </h2>
<form action="login.php" method="post">
<h2> Login </h2> Username
<br/>
<input type="email" value="username" name="uname" /> Password
<br/>
<input type="password" value="password" name="passwd" />
<input type="submit" value="Login" />
</form>
</section>
<section>
<form action="regusr.php" method="post" >
<h2> Register </h2> First Name
<input type="text" value="First Name" name="fname" /> Middle Name
<input type="text" value="Middle Name" name="mname" /> Last Name
<input type="text" value="Last Name" name="lname" /> Age
<input type="number" value="18" name="age" />
Gender<br/>
Male: <input type="radio" value="1" name="gender" /> Female:<input type="radio" value="2" name="gender"/><br/>
Email
<input type="email" value="Email" name="emailid" /> Password
<input type="password" value="Password" name="passwd" />
<input type="submit" value="Register" />
</form>
</section>
<footer> For any queries please contact us
<br /> Phone: +91-40-460870000 ext:1234
<br /> email: admin@gmail.com
<br /> For any issue with the web site please send an email to webmaster@gmail.com
<br/> © Copyright All rights reserved.
<br /> </footer>
</body>
</html>
这是PHP页面 - regusr.php
<? php
//Get the user info from the form
$uname = $_POST['emailid'];
$pwd = $_POST['passwd'];
$first = $_POST['fname'];
$middle = $_POST['mname'];
$last = $_POST['lname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
echo "username:" .$uname ."passwd:" .$pwd ."first:" .$first ."middle:" .$middle ."last:" .$last ."age:" .$age ."gender:" .$gender;
include("config.php");
// Check connection
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sqlinsert = "INSERT INTO userid (userid, user_name, user_paswd) VALUES ('$uname', '$pwd')";
$insert = mysqli_query($db, $sqlinsert);
if(!$insert){
printf("Error in insert query:%s\n",$insert->error)
}
mysqli_free_result($insert);
$sqlquery = "SELECT userid from userid where user_name='$uname'"
$result = mysqli_query($db,$sqlquery);
$id = 0;
//We should be having only a single item from the query
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$id = $row[0];
break;
}
mysqli_free_result($result);
$datainsert = "INSERT INTO user_data (user_id, first_name, last_name, middle_name, age, sex, userid_fk )
VALUES ('$first', '$last', '$middle', '$age', '$gender', '$id')";
} else {
echo "Database Error could not insert data.";
}
<h1>Basic Info</h1>
<table>
<tr>
<td>First Name: </td>
<td> <?php echo $first ?> </td>
</tr>
<tr>
<td>Middle Name: </td>
<td> <?php echo $middle ?> </td>
</tr>
<tr>
<td>Last Name: </td>
<td> <?php echo $last ?> </td>
</tr>
</table>
?>
答案 0 :(得分:0)
在你的regusr.php文件中创建嵌套的php。你看看了吗?你应该在编写html之前关闭你的php标签。如果你不这样做,那么你应该在echo里写html。例如,
echo "<h1>Hello, World</h1>"
答案 1 :(得分:0)
答案 2 :(得分:0)
<强>更新强>
PHP代码
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "team44";
$db = mysqli_connect($host, $user, $password, $database);
// Check connection
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
if (isset($_POST['register'])) {
//Get the user info from the form
$uname = $_POST['emailid'];
$pwd = $_POST['passwd'];
$first = $_POST['fname'];
$middle = $_POST['mname'];
$last = $_POST['lname'];
$age = $_POST['age'];
$gender = $_POST['gender'];
echo "username:" . $uname . "passwd:" . $pwd . "first:" . $first . "middle:" . $middle . "last:" . $last . "age:" . $age . "gender:" . $gender;
//Login
$sqlinsert = "INSERT INTO userid (userid, user_name, user_paswd) VALUES (NULL, '$uname', '$pwd')";
// Registation
$datainsert = "INSERT INTO user_data (user_id, first_name, last_name, middle_name, age, sex, userid_fk ) VALUES (NULL,'".$first."', '".$last."', '".$middle."', '".$age."', '".$gender."', '".$id."')";
$insert = mysqli_query($db, $sqlinsert);
$regi = mysqli_query($db, $datainsert);
if (!$insert || !$regi ) {
printf("Error in insert query:%s\n", $insert->error);
}else{
header('location:login.php);
}
}
?>
HTML代码
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Connection">
<meta name="author" content="Suman">
<title>Login or Register </title>
<link rel="stylesheet" href="css/normalize.css" />
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Scope+One" rel="stylesheet">
<link rel="stylesheet" href="css/form.css" />
<link rel="stylesheet" href="css/home.css" />
</head>
<body>
<header>
<h1> Welcome to Connections </h1>
</header>
<section>
<h2> Already a member. </h2>
<form action="login.php" method="post">
<h2> Login </h2> Username
<br/>
<input type="email" value="username" name="uname" /> Password
<br/>
<input type="password" value="password" name="passwd" />
<input type="submit" value="Login" />
</form>
</section>
<section>
<form method="POST" >
<h2> Register </h2> First Name
<input type="text" value="First Name" name="fname" /> Middle Name
<input type="text" value="Middle Name" name="mname" /> Last Name
<input type="text" value="Last Name" name="lname" /> Age
<input type="number" value="18" name="age" />
Gender<br/>
Male: <input type="radio" value="1" name="gender" /> Female:<input type="radio" value="2" name="gender"/><br/>
Email
<input type="email" value="Email" name="emailid" /> Password
<input type="password" value="Password" name="passwd" />
<input type="submit" name="register" value="Register" />
</form>
</section>
<footer> For any queries please contact us
<br /> Phone: +91-40-460870000 ext:1234
<br /> email: admin@gmail.com
<br /> For any issue with the web site please send an email to webmaster@gmail.com
<br/> © Copyright All rights reserved.
<br /> </footer>
</body>
</html>
答案 3 :(得分:-1)
您正在使用3个文件(userid,user_name,user_paswd),但只有2个值(uname和pwd)。不会插入到用户ID表中
值应该像VALUES('$ uname','$ uname','$ pwd')“;
"INSERT INTO userid (userid, user_name, user_paswd) VALUES ('$userid','$uname', '$pwd')";
请检查一下(缺少用户ID):
"INSERT INTO user_data (user_id, first_name, last_name, middle_name, age, sex, userid_fk )
VALUES ('$userid','$first', '$last', '$middle', '$age', '$gender', '$id')";