2

I am trying to pass a value that I have received from my database to another php page to use within another SQL statement. I have tried using sessions and also passing using the $_POST method on the other page but have had no luck.

Here is a snippet of my code looping through to display all records:

while($row = mysqli_fetch_array($sql)){
echo '<td>
          <img src='.'"'.$row['image'].'"'.'><br/>
          <form method="post" action="edit-record.php">
          <input type="text" name="imgID" value='.'"'.$row['id'].'"'.'>
          <input type="submit" value="Edit" id="edit_btn" class="admin_btn"></form>
      </td>';
}

The value that I need is the ID for each specific image - $row['id'].

When the user clicks the EDIT button, they should be redirected to another page which displays only the specific record. This is why I need the ID received passed to the next page to insert into a query statement.

I hope this made sense and any help will be greatly appreciated.

UPDATE: Thanks for all of your help. I solved the problem by playing around with a few of your suggestions to pass the id via GET in the action of the form.

<form method="post" action="edit-record.php?id='. $row['id'].'">

No idea why that hadn't occurred to me! Thanks again.

5
  • as an alternative, you could also use a simple link anchor. edit.php?id=<echo the id here>. and from that just use $_GET['id'] Commented Dec 30, 2014 at 15:58
  • 1
    How is this not working? What happens if you print_r($_POST) at the top of edit_record.php? Commented Dec 30, 2014 at 16:04
  • did you make sure that there is a value within $row['id'] ? Commented Dec 30, 2014 at 16:07
  • The 'edit item' button should really be an 'anchor tag' where the 'href' is the url including the 'id' of the record. Style it to look like a 'button' using css. It sends you to the 'edit item' page and the 'id' is in the '$_GET' array. Commented Dec 30, 2014 at 16:09
  • You don't need to "escape" double-quotes like that, you can just put them inside the single quotes like a regular character, can you show how do you retrive POST values in edit-record.php? Commented Dec 30, 2014 at 16:09

4 Answers 4

3
while($row = mysqli_fetch_array($sql)){
    echo '<td>
              <img src="'.$row['image'].'"><br/>
              <form method="post" action="edit-record.php">
                  <input type="text" name="imgID" value="'.$row['id'].'">
                  <input type="submit" value="Edit" id="edit_btn" class="admin_btn">
              </form>
          </td>';
}

in edit-record.php...

<?php
    echo $_POST['imgID'];
?>

There is no reason your code technically wouldn't work but instead you could just eliminate the form and use a simple link...

while($row = mysqli_fetch_array($sql)){
    echo '<td>
              <img src="'.$row['image'].'"><br/>
              <a href="edit-record.php?id='.$row['id'].'">edit</a>
          </td>';
}

and in edit-record.php...

<?php
    echo $_GET['id'];
?>
Sign up to request clarification or add additional context in comments.

Comments

0

4 Ways to do this...

1) Use a cookie 2) Use a session (which by default uses a cookie but in a different way) 3) Use cURL 4) add it to the GET parameters... ie. somepage.com/page.php?id=1

Comments

0

Strange concatenation

 <input type="text" name="imgID" value="'.$row['id'].'">     

Sure you select id on the mysql query???

If you make

  echo $_POST['imgID'];      

what is the result???

You can pass the id via get in the action form:

 <form method="post" action="edit-record.php?id='. $row['id'].' ">     

On the other page you recive the form in $_POST and the id in $_GET['id']

Comments

0

~Aha, I think the problem is your quotes. Single quotes don't allow variables to be interpreted.~

Nevermind, thats not your problem, but I already wrote it out, so I'll leave it. Look how much cleaner those quotes are :)

Switch up your quotes like so:

echo "<td>
          <img src='{$row['image']}'><br/>
          <form method='post' action='edit-record.php'>
          <input type='text' name='imgID' value='{$row['id']'}'>
          <input type='submit' value='Edit' id='edit_btn' class='admin_btn'></form>
      </td>";

Need curly braces around array element (e.g {$row['id']})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.