edit - How to get ID from another file in php?


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 >
  <div >
    <?php echo "<image src=\"images/".$data['gameId'].".png\" alt=\"gameImage\" </image>"?>
    <div ></div>
    <div >Name</div><div>: <?php echo $data['gameName'];?></div>
    <div >Developer</div><div>: <?php echo $data['gameDeveloper']; ?></div>
    <div >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>
<?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 ></div>
    <div >Name</div><div>: <input type="text" value="<?php echo $data['gameName'];?>" id="gameName" name="gameName"/></div>
    <div >Developer</div><div>: <input type="text" value="<?php echo $data['gameDeveloper'];?>" id="gameDeveloper" name="gameDeveloper"/></div>
    <div >Price</div><div>: <input type="text" value="<?php echo $data['gamePrice'];?>" id="gamePrice" name="gamePrice"/></div>
    <div id="txtError">
    <!--error message here-->
    <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.


Data is in<input>-type elements (or friends) and inside a<form...> tag.

For example

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

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:

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


Passed as query strings in<a href...>, for example:

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

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

echo $_GET['var1'];  // "foo"
echo $_GET['var2'];  // "bar"

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:

<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>

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:

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

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$id is 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.$id in your edit.php




You can pass through the query string

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

In this case your PHP code will get change to

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



Add the id as a parameter to your edit url:

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

also at the top of your edit.php:

$id = $_REQUEST['id'];

People are also looking for solutions to the problem: php - Looping Through Multiple HTML Elements with DOMDocument


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.