duangao8359 2017-04-22 08:47
浏览 65
已采纳

PHP在点击时将超链接字符串存储到变量中

I am making a page where it displays a table with staffID and staffName using PHP. When the user clicks the staffID it should then display a new table using the code from another file with extra details about that staffID such as shippingDate, OrderID, etc.

task9.php file below:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>PHP Task 9</title>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<?php

$sID= isset($_GET['staffID']) ? $_GET['staffID'] : '';

$conn = mysqli_connect('localhost', 'TWA', 'TWA_test', 'factory');

if ( !$conn ) {

die("Connection failed: " . mysqli_connect_error());

}   

$sql = "SELECT s.staffID,s.staffName
    FROM staff s";

$results = mysqli_query($conn, $sql)

or die ('Problem with query' . mysqli_error($conn));

?>

<h1> Staff Table </h1>

<table>
        <tr>

        <th>Staff ID</th>
    <th>Staff Name</th>

    </tr>   

<?php while($row = mysqli_fetch_array($results)) { ?>

    <tr>

    <td> <a href = "task8.php"><?php echo $row[0] ?></a></td>
    <td><?php echo $row[1] ?></td>

    </tr>

<?php } ?>

<?php mysqli_close($conn); ?>

</table>

</body>

</html>

task8.php asks a user to enter a staffID and it displays all of the details, if the staffID does not exist then it displays an error. This file works fine and displays everything correctly. task8.php file below:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>PHP Task 8</title>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<?php

$conn = mysqli_connect('localhost', 'TWA', 'TWA_test', 'factory');

if ( !$conn ) {

die("Connection failed: " . mysqli_connect_error());

}

//obtain the staff ID input from the $_GET array

$sID= isset($_GET['staffID']) ? $_GET['staffID'] : '';

$sql = "SELECT s.staffID, p.orderID, p.orderDate, p.shippingDate,s.staffName
    FROM purchase p
    INNER JOIN staff s
    ON p.staffID = s.staffID
    WHERE p.staffID = '$sID'";

$results = mysqli_query($conn, $sql)

or die ('Problem with query' . mysqli_error($conn));

?>

<h1> Order Details </h1>

<?php $rows = mysqli_num_rows($results); ?>
<?php if($rows <= 0){ ?>
<p><?php echo "The staff ID entered is invalid"; ?></p>
<?php } else { ?>

<table>
        <tr>

        <th>Staff ID</th>
    <th>Order ID</th>
    <th>Order Date</th>
    <th>Shipping Date</th>
    <th>Staff Name</th>

    </tr>   

<?php while ($row = mysqli_fetch_array($results))  { ?>

    <tr>

    <?php if($row[0] != ""): ?>
    <td><?php echo $row[0]; ?></td>
    <?php else: ?>
    <td><?php echo "N/A"; ?></td>
    <?php endif; ?>

    <?php if($row[1] != ""): ?>
    <td><?php echo $row[1]; ?></td>
    <?php else: ?>
    <td><?php echo "N/A"; ?></td>
    <?php endif; ?>

    <?php if($row[2] != ""): ?>
    <td><?php echo $row[2]; ?></td>
    <?php else: ?>
    <td><?php echo "N/A"; ?></td>
    <?php endif; ?>

    <?php if($row[3] != ""): ?>
    <td><?php echo $row[3]; ?></td>
    <?php else: ?>
    <td><?php echo "N/A"; ?></td>
    <?php endif; ?>

    <?php if($row[4] != ""): ?>
    <td><?php echo $row[4]; ?></td>
    <?php else: ?>
    <td><?php echo "N/A"; ?></td>
    <?php endif; ?>

    </tr>

<?php } ?>

<?php } ?>

<?php mysqli_close($conn); ?>

</table>

</body>

</html>

The problem I am facing is that when I click the staffID from the table on task9.php it just shows the error I included in task8.php "The staff ID entered is invalid". I don't know why it isn't displaying the details from task8.php

  • 写回答

2条回答 默认 最新

  • douche1936 2017-04-22 09:23
    关注

    you did not build your link correctly, it should be

    <a href = "task8.php?staffID=<?php echo $row[0] ?>"><?php echo $row[0] ?></a>
    

    also, i feel somehow uncomfortable echoing outputs without htmlentities(). Yes, it is boring to type that everytime so i usually keep a function in every of my PHP projects like this

    function e($whatToConvert){
       return htmlentities($whatToConvert); //or htmlspecialchars
    }
    

    so the above code for instance becomes

    <a href = "task8.php?staffID=<?php e(echo $row[0]) ?>"><?php echo e($row[0]) ?></a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决