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