在循环中使用PHP和MySQL的动态链接

时间:2016-09-07 13:23:08

标签: php mysql loops hyperlink

我正在学习PHP和MySQL的自我项目,显然我遇到了困难。我想要制作的网站包括提交表单和审核表单。我设法创建一个循环,以显示已提交但尚未处理的所有表单(因此,易于审阅),并且每个表单都有自己的链接,重定向到所述表单的数据包括

    $query = mysqli_query ( $conn, "SELECT * FROM members WHERE username = '$myrealusername' LIMIT 1" );
    $result = $query->fetch_assoc ();
    $id = $result ['id'];
    echo 'Welcome ' . $id . '. <br>';

    $quer = mysqli_query ( $conn, "select count(*) as total from carforms where aboveid='$id' and processed='0'" );
    $data = $quer->fetch_assoc ();
    $total = $data ['total'];
    $quer = mysqli_query ( $conn, "select fullname as CurrentName, formid as CurrentForm from carforms where aboveid='$id' and processed='0'" );
    for ($x=1; $x<=$total; $x++) {
        $data = $quer->fetch_assoc ();
        $forms = $data ['CurrentName'];
        $forma = $data ['CurrentForm'];
    echo  'Form No.'. $forma .' by: ' .$forms .'<a href="reviewform.php">Click here to review</a>.<br>'; 
    }
    ?>

这就是它所显示的:

 Welcome 2. 
Form No.1 by: John Miller Click here to review.
Form No.2 by: John Miller Click here to review.
Form No.3 by: Mike Tywell Click here to review.
Form No.4 by: John Miller Click here to review.
Form No.5 by: Eric Paton Click here to review.

问题是,我不知道如何将不同的数据传递给MySQL carform表中显示的每个链接,这些链接还包括汽车类型,汽车颜色等数据,并且每个人都不同。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您需要将其添加为查询字符串,例如

  echo  'Form No.'. $forma .' by: ' .$forms .'<a href="reviewform.php?id='.$row['id'].'">Click here to review</a>.<br>'; 

你可以让这更简单

 $query = mysqli_query ( $conn, "SELECT * FROM members WHERE username = '$myrealusername' LIMIT 1" );
    $result = $query->fetch_assoc ();
    $id = $result ['id'];
    echo 'Welcome ' . $id . '. <br>';

    $quer = mysqli_query ( $conn, "select id,fullname as CurrentName, formid as CurrentForm from carforms where aboveid='$id' and processed='0'" );
    while($data= mysqli_fetch_array($quer)) {

        $forms = $data ['CurrentName'];
        $forma = $data ['CurrentForm'];
    echo  'Form No.'. $forma .' by: ' .$forms .'<a href="reviewform.php?id='.$data['id'].'">Click here to review</a>.<br>'; 
    }
    ?>

或类似的东西。

答案 1 :(得分:1)

您只需要向锚添加参数

for ($x=1; $x<=$total; $x++) {
    $data = $quer->fetch_assoc ();
    $forms = $data ['CurrentName'];
    $forma = $data ['CurrentForm'];
    echo  "Form No. $forma by: $forms <a href='reviewform.php?formid=$forma'>Click here to review</a><br>"; 
}

reviewform.php中,您可以使用$_GET数组查看formid参数中传递的值

<?php
if ( isset($_GET['formid'] ) {
     // we have a parameter passed

您可以像这样添加多个参数

echo  "Form No. $forma by: $forms <a href='reviewform.php?formid=$forma&user=$id'>Click here to review</a><br>";