I’m going to make edit menu in my web. so I direct the page from product into edit page. What I’m confused is how to get the productID from product’s page to use in edit page?
Here is my code in product
<?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 } ?>
and it’s my code in edit page
<?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 } ?>
When I try to access edit page, there’s an error it said
"Undefined index:$id[0] = $_REQUEST['id'];"
in edit page.
Could anyone help me?
It looks like you’re confusing two methods of passing data between pages, forms and query strings in
<a href...>s.Forms:
Data is in
<input>-type elements (or friends) and inside a<form...>tag.For example
Usually passed via POST and accessed in PHP via
$_POST.For example, the values in the text boxes referenced above would be accessed with something like:
Links:
Passed as query strings in
<a href...>, for example:Usually passed via GET and accessed in PHP via
$_GET.For example, the values in the query string provided above would be accessed with something like
So in this case it looks like you’re hyperlinking an input button — which is not the usual way to do things, but you would fix it by changing this:
To, this
And then reference the variable in edit.php as
$_GET['id'].But since you know it’s going to be an integer and nothing else, something like:
Is good enough sanitation (at least for that variable).
Lastly, I notice you assign a variable to
$id[0]but then reference$id. Assigning a variable to$id[0]is not the same as assigning it to$id, as$idis an array in the former and an integer in the latter. It seems to me that you can just drop the[0]w.r.t.$idin your edit.php