通过 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"}

因此,即使我可以通过我的安卓手机添加数据,选择查询也不起作用:(。任何想法为什么会这样?我从来没有玩过那么深的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);
    }
 ?>

我添加了这 2 行:)。

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

解决了这个问题,在我骑自行车一个小时后:)!!!