2016-12-05 152 views
0

即時通訊仍在開發android我的android項目我真的需要幫助,我的問題是我無法獲得在我的系統登錄的用戶的用戶ID,所以當他們把一個用戶ID將附加到它的數據記錄..我想要在我的系統中輸出他們自己的數據。希望有人能幫助。它唯一的PHP代碼謝謝有人幫助。在如何在php中獲取用戶的用戶名?

<?php 
    // Connection Details altered to hide actual values. 
    $con = mysqli_connect("localhost", "db_user", "db_password", "db_name"); 

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

    $sql = "SELECT * FROM tbl_userinfo WHERE username = '$username' AND password='$password' LIMIT 1"; 
    $res = mysqli_query($con,$sql); 


    $response = array(); 
    $response["success"] = false; 

    $row = mysqli_fetch_array($res); 


    if(mysqli_num_rows($res)> 0){ 

     $response["success"] = true; 
     session_start(); 
     $_SESSION['user_id'] =$userID; 
    } 

    echo json_encode($response); 

?> 

這就是日誌,這裏有保存數據..

<?php 
    session_start(); 
    $userID =""; 
    // Connection Details altered to hide actual values. 
    $con = mysqli_connect("localhost", "db_user", "db_password", "db_name"); 

    if(!isset($_SESSION['user_id'])){ 

    $userID = $_SESSION['user_id']; 
    $checkdate = $_POST["checkdate"]; 
    $checkno = $_POST["checkno"]; 
    $datepaid = $_POST["datepaid"]; 
    $clientname = $_POST["clientname"]; 
    $bank = $_POST["bank"]; 
    $amount = $_POST["amount"]; 
    $status = "UNFINISHED"; 
    $statement = mysqli_prepare($con, "INSERT INTO tbl_checkinfo (user_id,checkno, checkdate, datepaid, clientname, bank, amount, status) VALUES (?,?, ?, ?, ?,?,?,?)"); 
    mysqli_stmt_bind_param($statement, "iissssis", $userID, $checkno, $checkdate, $datepaid, $clientname, $bank, $amount, $status); 
    mysqli_stmt_execute($statement); 

    $response = array(); 
    $response["success"] = false; 

    if($statement){ 
     $response["success"] = true; 
    } 



    echo json_encode($response); 

} 

?> 

和用於顯示用戶數據。

<?php 
    // Connection Details altered to hide actual values. 
    $con = mysqli_connect("localhost", "db_user", "db_password", "db_name"); 

    $checkdate = $_POST["checkdate"]; 
    $checkno = $_POST["checkno"]; 
    $datepaid = $_POST["datepaid"]; 
    $clientname = $_POST["clientname"]; 
    $bank = $_POST["bank"]; 
    $amount = $_POST["amount"]; 
    $status = "UNFINISHED"; 
    $sql = "Select * from tbl_checkinfo"; 
    $result = mysqli_query($con, $sql); 
    // $statement = mysqli_prepare($con, "Select * from tbl_checkinfo"); 
    // mysqli_stmt_execute($statement); 
    // mysqli_stmt_store_result($statement); 
    // mysqli_stmt_bind_result($statement, $user_id, $checkdate, $checkno,  $datepaid, $clientname, $bank, $amount, $status); 


    $response = array(); 
    $info=array(); 
    $flag = array(); 
    $response["success"] = false; 


if(mysqli_num_rows($result) > 0) { 
    while($row = mysqli_fetch_array($result)) 
    { 
     $flag[checkdate]=$row[checkdate]; 
     $flag[checkno]=$row[checkno]; 
     $flag[datepaid]=$row[datepaid]; 
     $flag[clientname]=$row[clientname]; 
     $flag[bank]=$row[bank]; 
     $flag[amount]=$row[amount]; 
     $flag[status]=$row[status]; 
     array_push($info, $flag); 
    } 
     $response["success"] = true; 
     $response["message"] = $info; 
     echo json_encode($response); 
} 
else 
{ 
     $response["success"] = 0; 
     $response["message"] = "No entries yet"; 
     echo json_encode($response); 
} 

?> 

回答

0

首先,張貼在公共論壇的問題時,請從代碼中刪除您的主機,數據庫名,密碼等。 :)

其次,儘量print_r($row),看哪個指標是可用的用戶ID,然後在代碼中添加此行:

if(mysqli_num_rows($res)> 0){ 
     $response["success"] = true; 
     $response["user_id"] = $row[USER_ID_INDEX]; 
     session_start(); 
     $_SESSION['user_id'] =$row[USER_ID_INDEX]; 
    } 
0

在那裏你定義$用戶ID的變量,你必須妥善分配值會話變量,

if(mysqli_num_rows($res)> 0){ 

     $response["success"] = true; 
     session_start(); 
     $_SESSION['user_id'] =$row[USER_ID_INDEX]; 
    } 
+0

我怎樣才能使它動態? –

+0

使其動態替換USER_ID_INDEX與數據庫表的列名稱。 –

+0

我在保存數據php中發現一個錯誤,它不再保存了?幫幫我? –

0

$row['user_id_in_table']應該給你ID