2017-01-03 167 views
0

我是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/> &copy; 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> 
?> 
+2

是否啓用'display_errors'? –

+2

剛剛開始'<?php'寫入'error_reporting(E_ALL); ini_set('display_errors',1)''並通過發佈表單數據再次檢查 –

+0

另外...你應該散列你的用戶密碼和使用參數化查詢。這對SQL注入是開放的。 – chris85

回答

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: [email protected] 
      <br /> For any issue with the web site please send an email to [email protected] 
      <br/> &copy; Copyright All rights reserved. 
      <br /> </footer> 
    </body> 
</html> 
+0

我試過這段代碼,但不起作用。看起來有一個不同的問題。我試圖在PHP頁面上做一個簡單的Echo,但是我沒有看到任何東西。你可以查看這個鏈接[鏈接](http://lynkuz.com/demo/index.html)我已經在這裏上傳了代碼。 –

+0

@SumanMummaneni請立即檢查,我已更新此代碼 –

+0

只需複製並過去並更改連接字符串即可。 –

-1

您正在使用3周的Fileds(用戶ID,USER_NAME,user_paswd),但只有2個值信息(uname和PWD)。其不會插入到用戶標識表

值應該像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')"; 
+0

他如何獲得$ userid? –

+0

@dhruv jadia我沒有發佈確切的答案。我只是建議他,他的查詢中有錯誤。沒有清除這個錯誤.values不會插入到表中。 – Assen

+0

是的,但您不必爲自動遞增字段設置值,只是將其傳遞給空值 –

0

在你regusr.php文件創建嵌套的PHP。你檢查一下嗎?在寫html之前,你應該關閉你的php標籤。如果你不這樣做,那麼你應該在回聲裏面寫html。例如,

echo "<h1>Hello, World</h1>" 
0

下面是答案...

一旦嘗試

<?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 ('".$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); 
    } 
} 

?> 

<h1>Basic Info</h1> 
<table> 
    <tr>  
     <th>First Name:</th> 
     <th>Middle Name:</th> 
     <th>Last Name:</th>  
    </tr> 
    <?php 
    $sqlquery = "SELECT * from userid where user_name=".$uname; 
    $result = mysqli_query($db, $sqlquery); 
    while ($row = mysqli_fetch_array($result)) { 
     ?> 
     <td> <?php echo $row['first']; ?> </td> 
     <td> <?php echo $row['middel']; ?> </td> 
     <td> <?php echo $row['last']; ?> </td> 
    <?php } ?> 
</table> 

<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" name="register" 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/> &copy; Copyright All rights reserved. 
      <br /> </footer> 
    </body> 
</html> 
+0

我不想混用我的PHP和HTML代碼,除非有必要這樣做。 –