Php 页面未显示

Php page not displaying

我在显示 JOIN 语句时遇到问题。当我添加

 WHERE id = " . $team_id; 

数据库中的信息不会显示,但当我删除该行时,信息将正确加入并显示在 "teaminfo.php " 页面上,但它会显示所有数据而不是数据这是该 ID 所独有的。此外,当我删除 JOIN 时,将显示 id 独有的数据。谁能告诉我这里出了什么问题。任何帮助都会很棒。比你。

teaminfo.php

<html>
<head>
<title>Team Info page</title>
</head>
<body>
<?php

    include 'connect.php';
$team_id = $_GET['id'];

// SQL query
$query = " SELECT *
FROM pitscouting 
JOIN fieldscouting 
ON pteam_number = fteam_number
WHERE id = " . $team_id;


if ($result = mysqli_query($mysqli, $query)) {

/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {

   // Write the data of the team
    echo "<br />";
    echo "Pit scouting";
    echo "<dt>Team:</dt><dd>" . $row["pteam_number"] . " " . $row["pteam_name"] . "</dd>";
    echo "<dt>Auto:</dt><dd>" . $row["pauto"] . "</dd>";
    echo "<dt>Drive:</dt><dd>" . $row["pdrive"] . "</dd>";
    echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["pobjNoProblem"] . "</dd>";
    echo "<dt>Objects They have a problem with?</dt><dd>" . $row["pobjWithProblem"] . "</dd>";
    echo "<dt>Can they shoot? If yes from where and how acc</dt><dd>" . $row["pshoot"] . "</dd>";
    echo "<dt>Extra Notes about their robot?</dt><dd>" . $row["pdrive"] . "</dd>";

    echo"<br />";

    echo "Field Scouting ";
    echo "<dt>Team Number:</dt><dd>" . $row["fteam_number"] . "</dd>";
    echo "<dt>Auto:</dt><dd>" . $row["fauto"] . "</dd>";
    echo "<dt>Drive:</dt><dd>" . $row["fdrive"] . "</dd>";
    echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["fobjNoProblem"] . "</dd>";
    echo "<dt>Objects They have a problem with?</dt><dd>" . $row["fobjWithProblem"] . "</dd>";
    echo "<dt>Shots taken</dt><dd>" . $row["fshots_taken"] . "</dd>";
    echo "<dt>Shorts made</dt><dd>" . $row["fshots_made"] . "</dd>";
    echo "<dt>Extra Notes</dt><dd>" . $row["fnotes"] . "</dd>";


  }

  mysqli_free_result($result);

 }


// Close the database connection
mysqli_close($mysqli);
?>


<p><a href="palmetto.php">Return to the list</a></p>
</body>
</html>

Palmetto.php

<?php

include 'connect.php';

// SQL query
$query = "SELECT * FROM pitscouting ORDER BY pteam_number";

if($result = mysqli_query($mysqli, $query)){
if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_array($result)){
        $name = $row['pteam_number'] . " " . $row['pteam_name'];

        // Create a link to teaminfo.php with the id-value in the URL
   $strLink = "<a href = 'teaminfo.php?id= " . $row['id'] . "'>" . $name . "</a>";

    // List link
   echo "<li>" . $strLink . "</li>";
    }
    echo "</table>";
    // Close result set
    mysqli_free_result($result);
    } else{
    echo "No records matching your query were found.";
  }
  } else{
  echo "ERROR: Could not able to execute $query. " . mysqli_error($mysqli);
   }
// Close connection
mysqli_close($mysqli);
?>

如果您的 table 都有一个 ID 字段,您将必须指定要从哪个 table 获取数据。

WHERE pitscouting.id = " . $team_id;

WHERE fieldscouting.id = " . $team_id;

请在您的代码中提及 sql 注入

$team_id = $_GET['id'];

// SQL query
$query = " SELECT *
FROM pitscouting 
JOIN fieldscouting 
ON pteam_number = fteam_number
WHERE id = " . $team_id;

请查看准备好的语句,以防止 sql 在您的代码中注入

尝试输入别名。

$team_id = $_GET['id'];

// SQL query
$query = " SELECT *
FROM pitscouting p
JOIN fieldscouting f
ON p.pteam_number = f.fteam_number
WHERE p1.id = " . $team_id;