【问题标题】:Php page not displayingphp页面不显示
【发布时间】:2016-02-03 23:55:17
【问题描述】:

我在显示 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);
?>

【问题讨论】:

  • 查看以下链接 php.net/manual/en/mysqli.error.phpphp.net/manual/en/function.error-reporting.php 并将其应用于您的代码。
  • 我敢打赌两张桌子都有一个id 列。您需要使用WHERE tablename.id = 指定要检查的表的ID。
  • 如果您有@Fred-ii- 推荐的错误检查,您会看到一条错误消息,告诉您列名不明确。
  • 您还应该使用带有 bind_param 的准备好的查询,而不是将变量连接到查询中,以防止 SQL 注入。
  • 当发生 MySQL 错误时,当对 mysqli_query 的调用评估为 FALSE 时,teaminfo.php 代码没有 else 操作。代码将它的小指放在嘴角,Dr.Evil 风格“我只是假设一切都按计划进行。什么?”。鉴于 SQL 注入漏洞,很容易发生 SQL 错误。如果意图是在发生错误时“什么也不做”,则 IMO 代码应该有一个 else 并且至少有一个代码将忽略错误的注释。

标签: php mysql join where


【解决方案1】:

如果您的表都有 ID 字段,则您必须指定要从哪个表中获取数据。

WHERE pitscouting.id = " . $team_id;

WHERE fieldscouting.id = " . $team_id;

【讨论】:

  • 你的意思是WHERE pitscouting.id =
  • 成功了,非常感谢您的帮助。祝你有美好的一天。 @Beans
  • 我很高兴。那你能把我的答案设置为正确的吗? @kaydrae
【解决方案2】:

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

$team_id = $_GET['id'];

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

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

【讨论】:

    【解决方案3】:

    尝试添加别名。

    $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      • 2015-10-20
      • 2013-03-03
      • 1970-01-01
      相关资源
      最近更新 更多