私は携帯電話からプレーヤーの名前と彼の評価を挿入します。この部分は機能しています。しかしその後、私は主要なキーであるplayer_idという変数のint値を取得したい。テーブルからphp/mysqlを介して変数を読み取る
最初に挿入クエリを実行すると、データが挿入されます。これが完了したら、selectクエリを実行して、挿入された行の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でプレイしたことがない。しかし、私はそこなって、私は思います。
おかげで、
テオ。
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);
?>
? – daremachine
player_idとuser_idの違いは何ですか?あなたの問題かもしれません – daremachine
user_idは登録ユーザーのIDです。私はこれをrating_players_tableのplayer_idに関連付けて、各ユーザがデータを投稿できるようにします。 – Theo