2016-12-14 154 views
0

我面臨着困境,我創建的存儲varibale(用戶名)的會話未顯示。當用戶在index.php中註冊時,我想在maindash.html中顯示用戶名。會話不在PHP中存儲變量?

maindash.html代碼:

<!DOCTYPE html> 
    <html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <title> 
     MY SITE PLANNER | DASHBOARD 
     </title> 
     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"\> 
     <link href="custom.css" rel="stylesheet" type="text/css" /> 
    </head> 
    <body class="formbg"> 
     <div class="container-fluid pi"> 
     <div class="row"> 
      <div class="col-md-6 col-sm-6 col-xs-6"> 
       <p class="ppp"> 
        <img src="image/my-site-planner-logo.png" class="ppp" /> 
       </p> 
      </div> 
      <div class="col-md-6 col-sm-6 col-xs-6"> 
       <p class="up"> 
      <!--  Hi, <a href="#">user</a> --> 
       <a href="#">Hello, <?php echo $_SESSION["name"]; ?></a> 
         <a href="#">settings</a> 
         <a href="#">logout</a> 
       </p> 
       <p id="dt"> 
        System: 
       </p> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid fill"> 
     <div class="row"> 
      <div class="col-md-12 col col-sm-12 col-xs-12"> 
      <div class="navbar"> 
       <ul class="nalink"> 
       <li> 
        <a href="trans.html">ViewIncomeStatement</a> 
       </li> 
       <li> 
        <a href="siteform.html">View/AddSite</a> 
       </li> 
       <li> 
        <a href="vendor.html">View/AddVendor</a> 
       </li> 
       <li> 
        <a href="item.html">View/AddItem</a> 
       </li> 
       </ul> 
      </div> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-6 col-sm-6 col-xs-12"> 
      <h3 class="side"> 
       Summary of your site 
      </h3> 
      </div> 
      <div class="col-md-6 col-sm-6 col-xs-12"> 
      <button class="btn btn-success view" name="submit" type="submit" > 
       View Income Statement 
      </button> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-12 col-sm-12 col-xs-12"> 
      <div class="table-responsive line"> 
       <table class="table table-bordered dem"> 
       <thead> 
        <tr class="hd"> 
        <th width="3">Sr.</th> 
        <th width="6">Site Name</th> 
        <th width="6">Incomes</th> 
        <th width="6">Expenses</th> 
        <th width="6">Payables</th> 
        <th width="6">Balance</th> 
        <th width="10">Last Week Details</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
        <td>1</td> 
         <td><a href="#">SITE A</a> </td> 
         <td><a href="#">200000</a></td> 
         <td><a href="#">110000</a></td> 
         <td><a href="#">36000</a></td> 
         <td><a href="#">79000</a></td> 
         <td>Completed kitchen tiles work</td> 
        </tr> 
        <tr> 
         <td>2</td> 
         <td><a href="#">SITE B</a></td> 
         <td><a href="#">500000</a></td> 
         <td><a href="#">425000</a></td> 
         <td><a href="#">88000</a></td> 
         <td><a href="#">-13000</a></td> 
         <td>completed kitchen tiles work</td> 
        </tr> 
        <tr> 
         <td></td> 
         <td>TOTAL</td> 
         <td>700000</td> 
         <td>555000</td> 
         <td>124000</td> 
         <td>66000</td> 
         <td></td> 
        </tr> 
       </tbody> 
      </table> 
      </div> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-6 col-sm-6 col-xm-12"> 
      <h3 class="side"> 
       Your to do list 
      </h3> 
      </div> 
      <div class="col-md-6 col-sm-6 col-xm-12"> 
      <button class="btn btn-success view" name="submit" type="submit"> 
       Add Item 
      </button> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-12 col-sm-12 col-xm-12"> 
      <div class="table-responsive line"> 
       <table class="table table-bordered dem"> 
        <thead> 
        <tr class="hd"> 
         <th width="3">Sr.</th> 
         <th width="15">Task</th> 
         <th width="15">Site</th> 
         <th width="15">Added</th> 
         <th width="20">Action</th> 
        </tr> 
        </thead> 
       <tbody> 
        <tr> 
         <td>1</td> 
         <td>Need to co-ordinate with shahid bhai for comment bags</td> 
         <td>Site A</td> 
         <td>23-02-2016</td> 
         <td> 
          <a href="#">Delete Task</a>&nbsp;<a href="#">Mark as complete</a> 
         </td> 
        </tr> 
        <tr> 
         <td>2</td> 
         <td>Get registrations done</td> 
         <td>Site B</td> 
         <td>23-02-2016</td> 
         <td><a href="#">Delete Task</a> &nbsp; <a href="#">Mark as complete</a></td> 
        </tr> 
       </tbody> 
       </table> 
      </div> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-6 col-sm-6 col-xm-12"> 
      <h3 class="side"> 
       Quick Navigation 
      </h3> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-12 col-sm-12 col-xm-12"> 
      <div class="table-responsive line"> 
       <table class="table table-bordered dem"> 
       <tr class="hd"> 
        <th> 
        <span>Select Site</span> 
        <select name="site" class="hig"> 
         <option value="" selected="selected">Fetch site list</option> 
         <option value="site A">site A</option> 
         <option value="site B">site B</option> 
        </select> 
        <span class="dis">Select Operations</span> 
         <select name="site" class="hig"> 
         <option value="" selected="selected">select your option</option> 
         <option value="option 1">View Site-wise imcome statement</option> 
         <option value="option 2">View Site-wise vendors</option> 
         <option value="option 3">View Site-wise items list</option> 
         <option value="option 4">List of Vendors Vs Expenses</option> 
         <option value="option 5">List of Item Vs Expenses</option> 
         <option value="option 6">List of Labours Vs Expenses</option> 
         <option value="option 7">List of Service Vs Expenses</option> 
         <option value="option 8">Payables List</option> 
         <option value="option 9">To-do List</option> 
         <option value="option 10">Work Done List</option> 
        </select> 
        <button class="btn btn-success viewl" name="submit" type="submit" >GO</button> 
        </th> 
       </tr> 
       </table>   
      </div> 
      </div> 
     </div> 
     </div> 


     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-6 col-sm-6 col-xm-12"> 
      <h3 class="side"> 
       Reporting Definations 
      </h3> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-md-12 col-sm-12 col-xm-12"> 
      <div class="table-responsive line"> 
       <table class="table table-bordered dem"> 
       <tbody> 
        <tr class="info"> 
        <td>View Site-wise imcome statement</td> 
        <td>Show Income Statement of a particular site</td> 
        </tr> 
        <tr> 
        <td>View Site-wise vendors</td> 
        <td>Show all the vandors of a perticular site</td> 
        </tr> 
        <tr class="info"> 
        <td>View Site-wise items list</td> 
        <td>Show all the Items registerd in a particular</td> 
        </tr> 
        <tr> 
        <td>List of Vendors Vs Expenses</td> 
        <td>List of all vandors registerd in a perticular site vs amount paid to them</td> 
        </tr> 
        <tr class="info"> 
        <td>List of Item Vs Expenses</td> 
        <td>List of all the Items used/registerd in a perticular site and the amount utilized on them</td> 
        </tr> 
        <tr> 
        <td>List of Labours Vs Expenses</td> 
        <td>List of the labours used in a perticular site and the amount paid to them</td> 
        </tr> 
        <tr class="info"> 
        <td>List of Service Vs Expenses</td> 
        <td>List of the Service used in a perticular site and the amount paid to them</td> 
        </tr> 
        <tr> 
        <td>Payables List</td> 
        <td>List of Payables in perticula site</td> 
        </tr> 
        <tr class="info"> 
        <td>To-do List</td> 
        <td>To-do List of a perticular site</td> 
        </tr> 
        <tr> 
        <td>Work Done List</td> 
        <td>List of Works Done on a perticular site</td> 
        </tr> 
        <tr class="info"> 
        <td></td> 
        <td></td> 
        </tr> 
       </tbody> 
       </table> 
      </div> 
      </div> 
     </div> 
     </div> 

     <script> 
     document.getElementById("dt").innerHTML = Date(); 
     </script> 

     <script src="https://code.jquery.com/jquery-2.2.0.js"></script> 
    </body> 
    </html> 

的index.php代碼:

<?php 
session_start(); 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "index"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 



$email  = $_POST['email']; 
$password = $_POST['password']; 


$sql = "INSERT INTO form (email, password) 
VALUES ('$email', '$password')"; 



       if ($conn->query($sql) === TRUE) { 


       header('Location: sign-up.html'); 

        } else { 
        echo "Error: " . $sql . "<br>" . $conn->error; 
       } 

       $conn->close(); 



if($count == 1) 
    { 
    $_SESSION['logged'] = true; 
    $_SESSION['username'] = $myusername; 
    echo "Login worked"; 
    exit(); 
    } 
else 
    { 
    $_SESSION['logged']=false; 
    echo "Login failed"; 
    exit(); 
    } 

?> 

<li class='active' style='float:right;'> 
    <?php 
    if($_SESSION['logged'] == true) 
    { 
     echo $_SESSION["name"]; 
     echo '<a href="logout.php"><span>Logout</span></a></li>'; 
    } 
    elseif($_SESSION['logged'] == false) 
    { 
     echo '<a href="sign-up.html"><span>Login/Register</span></a></li>'; 
    } 



//Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT * FROM form where email = $email and password = $password"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo " Name: " . $row["name"]. ".<br>"; 
    } 
} else { 
    echo "0 results"; 
} 
$conn->close(); 
    ?> 

這裏,用戶登錄後,它不會顯示用戶名。我想把它作爲用戶名應該從數據庫文件中取出並顯示在maindash.html。但它只顯示我寫在我的代碼中的Hello。

+0

你需要有擴展名的網頁'.php'與'maindash.html'的'session'工作。 –

+0

看起來你的maindash.html頁面沒有session_start初始化。 Maindash頁面應該是一個.php頁面,以<?php標記開頭,並在開頭處包含session_start()函數。 –

+0

我有這個index.php我存儲會話。你在上面看到。 –

回答

0

將您的maindash.html頁面重命名爲.php擴展名。並在頁面(maindash.php即)的頂部確保你把

<?php 
session_start(); 
?> 
+0

我創建了php會話並將擴展名maindash.html更改爲php,但仍然沒有從數據庫獲取用戶名。用戶使用電子郵件ID和密碼註冊。 –

+0

mysql查詢有沒有錯誤? –

+0

查看您的查詢,您將看到SQL注入,但這是另一個問題。你的代碼結構看起來有點可疑,你正在設置'$ _SESSION ['username'] = $ myusername;'但是'$ myusername'變量沒有被創建/定義。出於這個原因'$ _SESSION ['username']'將永遠是未定義的。如果您在從您的登錄查詢中獲取結果集後設置了'$ _SESSION ['username']',則代碼將更有意義。在你的while($ row = $ result-> fetch_assoc())''循環中設置'$ _SESSION ['username'] = $ row ['name'];' – Milli