2016-12-14 20 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

を入れて確実にあなたはmaindash.html' 'のsession''で動作するように拡張 '.php'でページを持っている必要があります。 –

+0

maindash.htmlページにsession_startの初期化がないようです。 Maindashページは<?phpタグで始まり、session_start()関数を始めに含む.phpページにする必要があります。 –

+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

関連する問題