簡體   English   中英

通過php / mysql從表中讀取變量

[英]reading a variable through php/mysql from a table

我從手機中插入玩家的名字和他的等級。 這部分正在工作。 但是在那之后,我想找回一個名為player_id的變量的int值,它是主鍵之一。

所以首先我執行插入查詢,將數據插入。 完成此操作后,我將運行選擇查詢以獲取用戶插入行的player_id。 這是代碼。

<?php
require "init.php";
header('Content-type: application/json');

$id_isSet = isset($_POST['player_id']);
$user_id_isSet = isset($_POST['User_Id']);
$best_player_isSet = isset($_POST['player']);
$rate_isSet = isset($_POST['rating']);

if ($id_isSet && $user_id_isSet && $best_player_isSet && $rate_isSet) {

    $id = $_POST['player_id'];
    $user_id = $_POST['User_Id'];
    $best_player = $_POST['player'];
    $rate = $_POST['rating'];

    $sql_query = "INSERT INTO rating_players_table    
    VALUES('$id','$best_player','$rate','$user_id');";

    if (mysqli_query($con, $sql_query)) {

        $query = "select * from rating_players_table  LIMIT 1";

        $result = mysqli_query($con, query);

        $row = mysqli_fetch_array($result);

        if ($row) {

            $post_id = $row['player_id'];
            $don = array('result' => 'success', 'message' => $post_id);
        } else {

            $don = array('result' => 'fail', 'message' => 'player was not found');
        }
    }
} else if (!$best_player) {

    $don = array('result' => "fail", "message" => "Insert player name");
} else if (!$rate) {

    $don = array('result' => "fail", "message" => "Rate player");
}

echo json_encode($don);
?>

我得到這個回應。

{"result":"fail","message":"player was not found"}

因此,即使我可以通過Android手機添加數據,select查詢也不起作用:(。任何想法為什么會發生這種情況?我從來沒有在php / mysql中玩那么深。但是我想到達那里。

謝謝,

西奧

編輯

<?php
 require "init.php";
 header('Content-type: application/json');

$id_isSet = isset($_POST['player_id']);
$user_id_isSet = isset($_POST['User_Id']);
$best_player_isSet = isset($_POST['player']);
$rate_isSet = isset($_POST['rating']);

if($id_isSet && $user_id_isSet && $best_player_isSet && $rate_isSet){

$id = $_POST['player_id'];
$user_id = $_POST['User_Id'];
$best_player = $_POST['player'];
$rate = $_POST['rating'];

$sql_query = "INSERT INTO rating_players_table    
VALUES('$id','$best_player','$rate','$user_id');";

if(mysqli_query($con,$sql_query)){

    mysqli_insert_id($con);

    $don = array('result' =>"success","message"=>"Έγινε");
 }       
 }else if(!$best_player){

    $don = array('result' =>"fail","message"=>"Insert player name");

 }else if(!$rate){

    $don = array('result' =>"fail","message"=>"Rate player");

}

  $query = "select player from rating_players_table where player_id ='".mysqli_real_escape_string($con, $id)."' LIMIT 1";       

            $result = mysqli_query($con,query);

            $row = mysqli_fetch_array($result);

            if($row){

                $post_id = $row['player_id'];
                mysqli_insert_id($post_id);
                $don = array('result' =>'success','message'=>$post_id);

            }else{

                $don = array('result' =>'fail','message'=>'player was not      
  found');
            }
  echo json_encode($don);

?>

您可以為此使用mysqli_insert_id($con); 在插入查詢語句之后。

好。 問題已解決。 最后,我可以讀取player_id變量。

<?php
require "init.php";
header('Content-type: application/json');

$id_isSet = isset($_POST['player_id']);
$user_id_isSet = isset($_POST['User_Id']);
$best_player_isSet = isset($_POST['player']);
$rate_isSet = isset($_POST['rating']);

if($id_isSet && $user_id_isSet && $best_player_isSet && $rate_isSet){

$id = $_POST['player_id'];
$user_id = $_POST['User_Id'];
$best_player = $_POST['player'];
$rate = $_POST['rating'];

$sql_query = "INSERT INTO rating_players_table    
VALUES('$id','$best_player','$rate','$user_id');";

if(mysqli_query($con,$sql_query)){

        $last_player_id = mysqli_insert_id($con);
        $don = array('result' =>'success','message'=>$last_player_id);
    }else{
        $don = array('fail' =>'success','message'=>'Κάτι πήγε λάθος');
    }
       echo json_encode($don);
    }

 ?>

我添加了這兩行:)。

    $last_player_id = mysqli_insert_id($con);
    $don = array('result' =>'success','message'=>$last_player_id);

我騎了一個小時的自行車后,我解決了這個問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM