【问题标题】:Pass a dynamic variable through URL php通过 URL php 传递动态变量
【发布时间】:2013-09-04 00:47:23
【问题描述】:

我不确定标题,我尽力了。 我有一个表格,其中显示了使用此文件的数据库中的信息

显示.php

<?php

    mysql_connect("localhost", "root", "root") or die(mysql_error());
    mysql_select_db("tournaments") or die(mysql_error());

    $result = mysql_query("SELECT * FROM tournies") 
    or die(mysql_error());  

    echo '<table id="bets" class="tablesorter" cellspacing="0" summary="Datapass">
<thead>
    <tr>
        <th>Tournament <br> Name</th> 
        <th>Pot</th> 
        <th>Maximum <br> Players</th> 
        <th>Minimum <br> Players</th> 
        <th>Host</th> 
        <th></th>
        <th></th>
    </tr>
</thead>
<tbody>';



    while($row = mysql_fetch_array( $result )) {

       $i=0; if( $i % 2 == 0 ) {
            $class = "";
        } else {
            $class = "";
        }

        echo "<tr" . $class . "><td>"; 
        echo $row['tour_name'];
        $tour_id = $row['tour_name'];
        echo "</td><td>"; 
        echo $row['pot']," Tokens";
        echo "</td><td class=\"BR\">"; 
        echo $row['max_players']," Players";
        echo "</td><td class=\"BR\">"; 
        echo $row['min_players']," Players";
        echo "</td><td class=\"BR\">";
        echo $row['host'];
        echo "</td><td>";
        echo "<input id=\"delete_button\" type=\"button\" value=\"Delete Row\" onClick=\"SomeDeleteRowFunction(this)\">";
        echo "</td><td>";
        echo "<form action=\"join.php?name=$name\" method=\"POST\" >";
        echo "<input id=\"join_button\" type=\"submit\" value=\"Join\">";
        echo "</td></tr>"; 
    } 

    echo "</tbody></table>";
?>

基本上,我希望用户从表格的一行中按下一个按钮,然后他们会转到一个名为 join.php 的新页面。我需要点击行中的人员用户名和锦标赛名称。

例如,这是我的页面:

当他们点击第一行末尾的加入按钮时,它应该将他们发送到

'join.php?name=thierusernamehere&tourname=dfgdds'

非常感谢任何帮助。谢谢。

【问题讨论】:

  • 用户名数据在哪里?
  • 只需在表单中添加一个隐藏字段(例如echo "&lt;input name=\"name\" type=\"hidden\" value=\"".$name."\"&gt;";),其中包含用户名和旅游名称。
  • PSA: mysql_* 函数是 deprecated in PHP 5.5。不建议编写新代码,因为它会阻止您将来升级。相反,请使用 MySQLiPDObe a better PHP Developer

标签: php mysql url variables


【解决方案1】:
echo '<td><a href="join.php?name='.$row['name'].'&tourname='.$row['tour_name'].'">Join</a></td>'

【讨论】:

  • name 似乎不是结果集的一部分。
  • @AndreeCrist 只需要修复一些命名错误,它就像一个魅力,谢谢。
【解决方案2】:

有很多方法可以接近。

最简单的方法就是echo '&lt;a href="join.php?name=' . $row['col_name'] . '"&gt;JOIN&lt;/a&gt;';

或者您可以使用带有隐藏输入和提交按钮的表单。

但是

你的代码真的很乱,尽量让你的代码更易维护和可读。并且不要使用任何 mysql_* 函数,它们已被弃用。

阅读有关 PDO 的更多信息:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    相关资源
    最近更新 更多