我是PHP的新手,在HTML表單上提交數據到PHP頁面,該頁面應該將數據插入到MySQL中。該表格是一個簡單的註冊表單。當我點擊提交時,網址被重定向,但我在網頁上看不到任何內容。它是空白的。我檢查它顯示爲空白的視圖頁面源。HTML表單POST到PHP顯示一個空白頁面
請讓我知道這是否是將數據傳遞給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: [email protected]
<br /> For any issue with the web site please send an email to [email protected]
<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>
?>
是否啓用'display_errors'? –
剛剛開始'<?php'寫入'error_reporting(E_ALL); ini_set('display_errors',1)''並通過發佈表單數據再次檢查 –
另外...你應該散列你的用戶密碼和使用參數化查詢。這對SQL注入是開放的。 – chris85