用PHP登录后获取用户数据

时间:2017-07-14 19:43:28

标签: javascript php

使用用户ID和手机号码进行用户登录。登录后我想从数据库中显示有关此用户的更多数据,但是当我转到下一页时,它会显示来自表中所有用户的数据。

这是我登录的PHP:

<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbnam = "adil";
try {
    $conn = new PDO("mysql:host=$servername; dbname=$dbnam", $dbusername, $dbpassword);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }


// new data

 if (isset($_POST["patientId"]))
    {$patientId = $_POST['patientId'];} 

if (isset($_POST["mobile"]))
    {$mobile= $_POST['mobile'];} 


// query
    $result = $conn->query("SELECT `patientId`, `mobile` FROM `inpatient` 
                        WHERE patientId= '$patientId' AND mobile= '$mobile' LIMIT 1 ");

    $rows = $result->fetch(PDO::FETCH_ASSOC);

    if($result->rowCount() > 0) {
        session_start();
        $_SESSION['login'] = true;
    header("location:../patient-detail.php");
    }
    else{
        header("location:index.php");
        $errflag = true;
    }
    ?>

如何找到一些功能来控制用户的特定数据?

患者详细信息页面

<div class="row">   
<div class="col-sm-10 col-sm-offset-1"> 
<div id="patientedit">
<div class="row pHead">
<div class="col-sm-2 phBorder">ID</div>
<div class="col-sm-2 phBorder">Patient Name</div>
<div class="col-sm-1 phBorder">Gender</div>
<div class="col-sm-1 phBorder">Age</div>
<div class="col-sm-2 phBorder">Date</div>
<div class="col-sm-2 phBorder">Mobile</div>
<div class="col-sm-2 phBorder">Action</div>
</div>

<script>
$('body').on('click', 'input.deleteDep', function() {
   $(this).parents('tr').remove();  
});
function data(id){
    $.post('data.php',{ id:id },function(r){
        $('#myModal').html(r);
    });
}
</script>

            <script>
            function getData(id,file_name,div_name){
                $.post(file_name,{ id:id },function(r){
                    $(div_name).html(r);
                });
            }
            </script>
<?php

        $servername = "localhost";
        $dbusername = "root";
        $dbpassword = "";
        $dbnam = "adil";

$conn = new PDO("mysql:host=$servername;dbname=$dbnam", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM inpatient ORDER BY id DESC"); 
    $stmt->execute();

    while($result = $stmt->fetch(PDO::FETCH_ASSOC)){
    echo'
<div class="row pData" id="patient-'.$result['id'].'">
<div class="col-sm-2 pdBorder">'.$result["patientId"].'</div>
<div class="col-sm-2 pdBorder">'.$result["patientName"].'</div>
<div class="col-sm-1 pdBorder">'.$result["gender"].'</div>
<div class="col-sm-1 pdBorder">'.$result["age"].'</div>
<div class="col-sm-2 pdBorder">'.$result["date"].'</div>
<div class="col-sm-2 pdBorder">'.$result["mobile"].'</div>
<div class="col-sm-2 pdBorder">

<button type="button" class="btn btn-sm btn-info btn-block" data-toggle="modal" data-target="#myModal" onclick="data(\''.$result["patientId"].'\')">View More</button>

</div>
</div>';
    }

    ?>

    <script>
        function deletePatient(id){
            $('#patient-'+id).hide();
        }
    </script>

    <!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">


  </div>
</div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

正如@Yolo所说,为每个用户创建一个唯一ID ,并将其存储在 <?php ini_set('display_errors', '1'); $callbackUrl_1 = "http://demo.com/magento.com/admin123.php"; $callbackUrl = "http://demo.com/magento.com/sendToken.php"; $temporaryCredentialsRequestUrl = "http://demo.com/magento.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl); //$adminAuthorizationUrl = 'http://demo.com/magento.com/admin123/oAuth_authorize'; $adminAuthorizationUrl = 'http://demo.com/magento.com/oauth/authorize'; $accessTokenRequestUrl = 'http://demo.com/magento.com/oauth/token'; $apiUrl = 'http://demo.com/magento.com/api/rest'; $consumerKey = '88a6142021c1cdfed92b0954a94fc066'; $consumerSecret = 'bedc0ede692fe06d4b12821bb21f7c3b'; session_start(); //echo "SESSION state".$_SESSION['state']; if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) { $_SESSION['state'] = 0; } try { $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI; $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType); $oauthClient->enableDebug(); if (!isset($_GET['oauth_token']) && !$_SESSION['state']) { $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl); $_SESSION['secret'] = $requestToken['oauth_token_secret']; $_SESSION['state'] = 1; //echo "oauth_token ".$requestToken['oauth_token']."<br>"; //echo "oauth_token_secret ".$requestToken['oauth_token_secret'];die; header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']."&oauth_token_secret=".$requestToken['oauth_token_secret']); exit; } else if ($_SESSION['state'] == 1) { // echo "oauth_token ".$_GET['oauth_token'];die; $oauthClient->setToken($_GET['oauth_token'],$_SESSION['oauth_verifier']); $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl); $_SESSION['state'] = 2; $_SESSION['token'] = $accessToken['oauth_token']; $_SESSION['secret'] = $accessToken['oauth_token_secret']; $_SESSION['example'] = $accessToken['oauth_token']; header('Location: ' . $callbackUrl); exit; } else { $_SESSION['state'] = 0; $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']); $resourceUrl = "$apiUrl/products"; $oauthClient->fetch($resourceUrl,array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json')); $productsList = json_decode($oauthClient->getLastResponse()); print_r($productsList); } } catch (OAuthException $e) { print_r($e->getMessage()); echo "&lt;br/&gt;"; print_r($e->lastResponse); } ?> 变量中。在下一页上,从$_SESSION获取用户ID,并通过将$_SESSION子句应用于此唯一ID来从数据库获取记录。

Login.php中的更改

WHERE

患者详细信息页面中的更改

    // Create another session variable below the $_SESSION['login']
    $_SESSION['login'] = true;
    $_SESSION['patient_uid'] = $patientId;