duanmianhong4893 2016-07-17 09:25
浏览 34

从表中通过php / mysql读取变量

I insert from my phone a player's name and his rating. This part is working. But after that I want to get back a variable's int value called player_id which is the primary key one.

So first I do the insert query,the data are inserted. Once this is done,then I run a select query to get the player_id of the inserted row by the user. This is the code.

<?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);
?>

and I get this response.

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

So even if I can add the data through my android phone,the select query doesn't work:(. Any ideas why is this happening? I never played in php/mysql that deep. But I am getting there,I think.

Thanks,

Theo.

EDIT

<?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);

?>
  • 写回答

2条回答 默认 最新

  • du6333137 2016-07-17 09:34
    关注

    You can use for this mysqli_insert_id($con); after your insert query statement.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向