gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:46:14 24 4
gpt4 key购买 nike

我从手机中插入玩家的姓名和评级。这部分正在发挥作用。但之后我想取回一个名为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 手机添加数据,选择查询也不起作用:(。有什么想法为什么会发生这种情况吗?我从来没有玩过那么深的 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);

关于php - 通过 php/mysql 从表中读取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38419762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com