如何从PHP中的另一个文件获取ID?

时间:2012-05-05 03:24:36

标签: php edit show

我要在网上制作编辑菜单。所以我将页面从产品导入编辑页面。我感到困惑的是如何从产品页面获取productID以在编辑页面中使用? 这是我在产品中的代码

<?php $query= "SELECT * FROM game";
$rs = mysql_query($query);
while($data = mysql_fetch_array($rs)) { ?>
<div class="gameBox">
    <div style="margin:5px;">
        <?php echo "<image src=\"images/".$data['gameId'].".png\" alt=\"gameImage\" </image>"?>
        <div class="cleaner"></div>
        <div class="myLabel">Name</div><div>: <?php echo $data['gameName'];?></div>
        <div class="myLabel">Developer</div><div>: <?php echo $data['gameDeveloper']; ?></div>
        <div class="myLabel">Price</div><div>: $ <?php echo $data['gamePrice']; ?></div>
        <br />
        <a href="edit.php" <?php $id=$data['gameId'];?>><input type="button" value="Edit"/></a>
        <a href="<!-- Delete -->"><input type="button" value="Delete"/></a>
    </div>
</div>
<?php } ?>

这是我在编辑页面中的代码

<?php include("connect.php");
$id[0] = $_REQUEST['id'];
$query = "SELECT * FROM game WHERE gameId=".$id."";
$rs = mysql_query($query);
while($data = mysql_fetch_array($rs)) { ?>
    <form action="doUpdate.php" method="post">
        <?php echo "<image src=\"images/".$id.".png\" alt=\"gameImage\" </image>"?>
        <div class="cleaner"></div>
        <div class="myLabel">Name</div><div>: <input type="text" value="<?php echo $data['gameName'];?>" id="gameName" name="gameName"/></div>
        <div class="myLabel">Developer</div><div>: <input type="text" value="<?php echo $data['gameDeveloper'];?>" id="gameDeveloper" name="gameDeveloper"/></div>
        <div class="myLabel">Price</div><div>: <input type="text" value="<?php echo $data['gamePrice'];?>" id="gamePrice" name="gamePrice"/></div>
        <br/>
        <div id="txtError">
        <!--error message here-->
        </div>
        <input type="submit" value="Submit"/>
        <a href="product.php"><input type="button" value="Cancel"/></span></a>
<?php } ?>

当我尝试访问编辑页面时,它说错误

"Undefined index:$id[0] = $_REQUEST['id'];" 

在编辑页面中。 有谁可以帮助我?

3 个答案:

答案 0 :(得分:1)

将id作为参数添加到您的编辑网址:

<a href="edit.php?id=<?php echo $data['gameId'];?>"><input type="button" value="Edit"/></a>

也位于edit.php的顶部:

$id = $_REQUEST['id'];

答案 1 :(得分:1)

您可以传递查询字符串

<a href="edit.php?<?php $id=$data['gameId'];?>>

在这种情况下,您的PHP代码将更改为

$id[0] = $_SERVER['QUERY_STRING'];

答案 2 :(得分:1)

看起来你混淆了在<a href...> s中的页面,表单和查询字符串之间传递数据的两种方法。

<强>表单

数据位于<input> - 类型元素(或朋友)和<form...>标记内。

例如

<form action="handler.php">
    <input type="text" name="var1" />
    <input type="text" name="var2" />
    <input type="submit">
</form>

通常通过POST传递,并通过$_POST在PHP中访问。

例如,上面引用的文本框中的值可以通过以下方式访问:

<?php
echo $_POST['var1'];  // First text box
echo $_POST['var2'];  // Second text box

<强>链接

<a href...>中作为查询字符串传递,例如:

<a href="http://www.yoursite.com/index.php?var1=foo&var2=bar">Click Me</a>

通常通过GET传递,并通过$_GET在PHP中访问。

例如,上面提供的查询字符串中的值可以通过类似

的方式访问
<?php
echo $_GET['var1'];  // "foo"
echo $_GET['var2'];  // "bar"

所以在这种情况下,你看起来像是超链接输入按钮 - 这不是通常的做事方式,但是你可以通过更改它来修复它:

<a href="edit.php" <?php $id=$data['gameId'];?>><input type="button" value="Edit"/></a>

To,this

<a href="edit.php?id=<?php echo $data['gameId'];?>"><input type="button" value="Edit"/></a>

然后将edit.php中的变量引用为$_GET['id']

但是既然你知道它将是一个整数而没有别的东西,比如:

$id = (int)$_GET['id'];

卫生条件是否足够好(至少对于那个变量而言)。

最后,我注意到您将变量分配给$id[0],然后引用$id。将变量分配给$id[0]与将$id分配给$id不同,因为[0]是前者中的数组,后者中是整数。在我看来,你可以放弃$id w.r.t.你的edit.php中的{{1}}