duangao8359
duangao8359
2017-04-22 08:47

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 douche1936 4年前

    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>
    
    点赞 评论 复制链接分享
  • dtrpv60860 dtrpv60860 4年前

    You need to pass the parameter like this

     <a href = "task8.php?staffID=<?php echo $row[0]?>">
    
    点赞 评论 复制链接分享

相关推荐