1

我无法让 Framework7 使用?username=User1下一页上的 URL 变量。

它在第 1 页上生成并分配给链接,但在第 2 页上的 SQL 查询或 echo 语句中都没有使用。

第 1 页在超链接中设置变量;

profile.php?username=<?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8'); ?>

第 2 页“获取”变量使用;

<?php $username = (isset($_GET['username']))? trim(strip_tags($_GET['username'])) : ""; ?>

Framework7 是一个 Web 应用程序框架 - www.idangero.us/framework7/。

编辑添加 profile.php 的完整源代码,即使用变量。

<?php 

    // First we execute our common code to connection to the database and start the session 
    require("common.php"); 

    // At the top of the page we check to see whether the user is logged in or not 
    if(empty($_SESSION['user'])) 
    { 
        // If they are not, we redirect them to the login page. 
        header("Location: index.php"); 

        // Remember that this die statement is absolutely critical.  Without it, 
        // people can view your members-only content without logging in. 
        die("Redirecting to index.php"); 
    } 

    // Everything below this point in the file is secured by the login system 



    // We can retrieve a list of members from the database using a SELECT query. 
    // In this case we do not have a WHERE clause because we want to select all 
    // of the rows from the database table. 
    $query = " 
        SELECT 
            id,
            username,
            email
        FROM users WHERE username = '$username'
    "; 

    try 
    { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt->fetchAll(); 
?> 

<?php include('header.php') ?>

<div class="pages navbar-through toolbar-through">
<div class="page" data-page="profile">

<div class="page-content">

<div class="content-block">
<div class="content-block-inner">

<?php 
print_r($_GET);
?>

<p>Profile content will go here - <?php echo '&username'; ?></p>
<?php foreach($rows as $row): ?>
   <div>Username: <?php echo $row['username'] ?></div>
   <div>Location: <?php echo $row['email'] ?></div>

<?php endforeach; ?>

<a href="private.php">Go Back</a><br />
</div>

</div>
</div>

</div>
</div>

<?php include('footer.php') ?>

我还打印了$GET变量,可以看到变量值实际上被传递了——它只是由于某种原因没有在查询中使用。

4

1 回答 1

1

你没有分配$username你应该使用这样的变量:

<?php 

    // First we execute our common code to connection to the database and start the session 
    require("common.php"); 

    // At the top of the page we check to see whether the user is logged in or not 
    if(empty($_SESSION['user'])) 
    { 
        // If they are not, we redirect them to the login page. 
        header("Location: index.php"); 

        // Remember that this die statement is absolutely critical.  Without it, 
        // people can view your members-only content without logging in. 
        die("Redirecting to index.php"); 
    } 

    // Everything below this point in the file is secured by the login system 



    // We can retrieve a list of members from the database using a SELECT query. 
    // In this case we do not have a WHERE clause because we want to select all 
    // of the rows from the database table.

    $username = (isset($_GET['username']))? trim(strip_tags($_GET['username'])) : "";

    $query = " 
        SELECT 
            id,
            username,
            email
        FROM users WHERE username = '$username'
    "; 

    try 
    { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt->fetchAll(); 
?> 

<?php include('header.php') ?>

<div class="pages navbar-through toolbar-through">
<div class="page" data-page="profile">

<div class="page-content">

<div class="content-block">
<div class="content-block-inner">

<?php 
print_r($_GET);
?>

<p>Profile content will go here - <?php echo $username; ?></p>
<?php foreach($rows as $row): ?>
   <div>Username: <?php echo $row['username'] ?></div>
   <div>Location: <?php echo $row['email'] ?></div>

<?php endforeach; ?>

<a href="private.php">Go Back</a><br />
</div>

</div>
</div>

</div>
</div>

<?php include('footer.php') ?>

您还应该在进入数据库之前转义您的输入

于 2016-02-08T11:54:50.103 回答