javascript - PHP get URL value in mysql SELECT

in my current project I want to show statistics in a pie chart with chart.js, but they are different for each team on my page.
I tried to give the WHERE value in the Select in playerOne.php via the URL, but it doesn't seem to work because this PHP file is triggered by a JS function. I have passed the team_id on the URL via isset get, but this doesn't seem to work.
How can I pass the team_id value on the URL to the query?
playerOne.php
if(isset($_GET['team_id'])) {
$team_id = $_GET['team_id'];
}
$query = "SELECT * FROM user WHERE team_id = 7 LIMIT 1";
$select_team = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($select_team);
$user_id = $row['user_id'];
$query = "SELECT SUM(game_stats.match_stats_kills) AS Kills, SUM(game_stats.match_stats_deaths) AS Deaths FROM game_stats WHERE game_stats.user_id = $user_id";
$select_kd = mysqli_query($connection, $query);
$data = array();
foreach($select_kd as $row) {
$data[] = $row;
}
mysqli_close($connection);
echo json_encode($data);
stats.js
$(document).ready(function() {
showData();
});
function showData() {
{
($.post("includes/stats/playerOne.php",
function(data) {
var kills = [];
var deaths = [];
for(var i in data) {
kills.push(data[i].Kills)
deaths.push(data[i].Deaths);
}
var pieChartData = {
labels: [
'Kills', 'Deaths'
],
datasets: [
{
backgroundColor: ['#f56954', '#00c0ef'],
data: [kills, deaths]
}
]
};
var pieChartTarget = $('#playerKD').get(0).getContext('2d');
var pieChart = new Chart(pieChartTarget, {
type: 'pie',
data: pieChartData
});
}));
}
}
URL
http://localhost/r6team/team/team.php?team=stats&team_id=7
Answer
Solution:
In your JS, you need to send a GET request and leave room for the ID to be dynamic. stats.js should look like this:
$(document).ready(function() {
showData(7);
});
function showData(teamId) {
{
($.get("includes/stats/playerOne.php?team_id=" + teamId,
function(data) {
var kills = [];
var deaths = [];
for(var i in data) {
kills.push(data[i].Kills)
deaths.push(data[i].Deaths);
}
var pieChartData = {
labels: [
'Kills', 'Deaths'
],
datasets: [
{
backgroundColor: ['#f56954', '#00c0ef'],
data: [kills, deaths]
}
]
};
var pieChartTarget = $('#playerKD').get(0).getContext('2d');
var pieChart = new Chart(pieChartTarget, {
type: 'pie',
data: pieChartData
});
}));
}
}
playerOne.php should then look like this:
$team_id = 0;//initialize team ID
if(isset($_GET['team_id'])) {
$team_id = $_GET['team_id'];
}
//double quotes are important so the $team_id variable will be interpreted to the value it holds
$query = "SELECT * FROM user WHERE team_id = $team_id LIMIT 1";
$select_team = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($select_team);
$user_id = $row['user_id'];
$query = "SELECT SUM(game_stats.match_stats_kills) AS Kills, SUM(game_stats.match_stats_deaths) AS Deaths FROM game_stats WHERE game_stats.user_id = $user_id";
$select_kd = mysqli_query($connection, $query);
$data = array();
foreach($select_kd as $row) {
$data[] = $row;
}
mysqli_close($connection);
echo json_encode($data);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: err_ossl_pem_no_start_line
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.