html 查询字符串PHP

w8ntj3qf  于 2022-12-21  发布在  PHP
关注(0)|答案(1)|浏览(129)

我试图通过单击底部表格中的名称来获取所有者的详细信息。

<html>
    <head>
        <title>Home</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>

<body>

<script type="text/javascript">
    window.onload = function()
    {
        timedHide(document.getElementById('co'), 10);
    }

    function timedHide(element, seconds)
    {
        if (element) {
            setTimeout(function() {
                element.style.display = 'none';
            }, seconds*1000);
        }
    }
</script>

<?php  
     require 'Navbar.php';
?>

        <h1>Welcome to Poppleton Dog Show! This year 50 owners entered 300 dogs in 10 events!</h1>
     
<?php
        include './connection.php';

        $sql = "SELECT owners.id AS id, owners.name AS Owner, owners.email AS Email, dogs.name AS Name, ROUND(avg(score), 1) AS avg_score, breeds.name AS breed_name, COUNT(entries.id) AS entries_count\n"

        . "FROM entries\n"
    
        . "JOIN dogs ON dogs.id = entries.dog_id\n"
    
        . "JOIN breeds ON dogs.breed_id = breeds.id\n"
    
        . "JOIN owners ON owners.id = dogs.owner_id\n"
    
        . "GROUP BY dogs.id\n"
    
        . "HAVING entries_count > 1\n"
    
        . "ORDER BY `avg_score` DESC\n"
    
        . "LIMIT 10";
    
        $result = $conn->query($sql);

      

        echo "<table '<td align='center'>

                    <tr> 
                        <th>Owner</th>

                        <th>Email</th>

                        <th>Dog</th>

                        <th>Breed</th>

                        <th>Average Score</th>

                    </tr>";
        

    while($row = mysqli_fetch_assoc($result)) {
    
    echo "<tr>";

    echo "<td>". "" ."<a href=OwnerDetails.php?id:" .$row['id']. ">". $row['Owner']. "</a></td>";
    

    echo "<td>". "" ."<a href= mailto:$row[Email]>$row[Email]</a></td>";

    echo "<td>". "" . $row["Name"] . "</td>";

    echo "<td>". "" . $row["breed_name"] . "</td>";

    echo "<td>". "" . $row["avg_score"] . "</td>";

    echo "</tr>";

    }
    echo "</table>";
           
$conn->close();
?>

<!-- connection message will fade away-->
<script>
$( document ).ready( readyFn );
$(function() {
$('#echo').fadeOut(1000);
});
</script>


</body>
</html>

我在另一个页面上的$_GET方法现在正在获取号码,但它仍然没有显示所有者的详细信息,也没有错误或任何东西。我不知道代码有什么问题。A希望得到任何关于代码、格式等方面的建议。

<html>
    <head>
        <title>OwnerDetail</title>
      
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    </head>

<body>

<script type="text/javascript">
    window.onload = function()
    {
        timedHide(document.getElementById('co'), 10);
    }

    function timedHide(element, seconds)
    {
        if (element) {
            setTimeout(function() {
                element.style.display = 'none';
            }, seconds*1000);
        }
    }
</script>

<?php  
     require 'Navbar.php';
?>

<?php
include './connection.php';
?>

<?php
$id = $_GET['id'];       // Collecting data from query string
if (!is_numeric($id)) { // Checking data it is a number or not
    echo "Data Error";
    exit;
}

$count=$dbo->prepare("SELECT * FROM owners WHERE id=:id");
$count->bindParam(":id",$id,PDO::PARAM_INT,3);

if($count->execute()){

    echo " Success ";
    
    $row = $count->fetch(PDO::FETCH_OBJ);

    echo "<table>";
}
echo "
<tr bgcolor='#f1f1f1'><td><b>Name</b></td><td>$row->name</td></tr>
<tr><td><b>Class</b></td><td>$row->class</td></tr>
<tr bgcolor='#f1f1f1'><td><b>Mark</b></td><td>$row->mark</td></tr>
<tr><td><b>Address</b></td><td>$row->address</td></tr>
<tr bgcolor='#f1f1f1'><td><b>Image</b></td><td>$row->img</td></tr>
";
echo "</table>";
?>


<script>
$( document ).ready( readyFn );
$(function() {
$('#echo').fadeOut(1000);
});
</script>

    </body>

</html>
icomxhvb

icomxhvb1#

你真的应该考虑这样做:
1.替换旧的jQuery
1.简化已经产生无效HTML的PHP
1.仅Ajax数据

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function() {
  $("#ownerTable a.owner").on("click", function(e) {
    e.preventDefault(); // stop link
    const ownerDetails = $.get(this.href, function(data) {
      $("#output").html(data)
    });
  });
});
<script>

并且具有

<table>
  <thead>
    <tr>
      <th>Owner</th>
      <th>Email</th>
      <th>Dog</th>
      <th>Breed</th>
      <th>Average Score</th>
    </tr>
  </thead>
  <tbody id="ownerTable">
  <? while($row = mysqli_fetch_assoc($result)) { ?>
    <tr>
      <td>
        <a class="owner" href="OwnerDetails.php?id=<?= $row['id'] ?>"><?= $row['Owner'] ?></a>
      </td>
      <td>
        <a href="mailto:<?= $row[Email] ?>"><?= $row[Email] ?></a>
      </td>
      <td>
        <?= $row["Name"] ?>
      </td>
      <td>
        <?= $row["breed_name"] ?>
      </td>
      <td>
        <?= $row["avg_score"] ?>
      </td>
    </tr>
  <? } ?>
  </tbody>
  <tbody id="output"></tbody>
</table>

Ownerdetails.php现在的样子

<?php
$id = $_GET['id'];       // Collecting data from query string
if (!is_numeric($id)) { // Checking data it is a number or not
    echo "<tr><td>Data Error</td></tr>";
    exit;
}

$count=$dbo->prepare("SELECT * FROM owners WHERE id=:id");
$count->bindParam(":id",$id,PDO::PARAM_INT,3);

if($count->execute()){
    $row = $count->fetch(PDO::FETCH_OBJ);
    echo "
      <tr bgcolor='#f1f1f1'><td><b>Name</b></td><td>$row->name</td></tr>
      <tr><td><b>Class</b></td><td>$row->class</td></tr>
      <tr bgcolor='#f1f1f1'><td><b>Mark</b></td><td>$row->mark</td></tr>
      <tr><td><b>Address</b></td><td>$row->address</td></tr>
      <tr bgcolor='#f1f1f1'><td><b>Image</b></td><td>$row->img</td></tr>";
}
?>

相关问题