João João - 6 months ago 11
PHP Question

how to pass mysqli_fetch_assoc rows to other pages

I have a page that gets info from the database and transforms it into rows:

<?php

session_start();
$cuser = $_SESSION["username"];

$getRequests = "SELECT * FROM friends WHERE user2='$cuser' AND accepted='0'";
$query = $conn->query($getRequests);

if ($query->num_rows >= 1) {
while($row = mysqli_fetch_assoc($query)) {
echo "<div id=fRequest>";
echo "<a href='#'>".$row["user1"]."</a>";
echo "</div><br>";
echo "<a href='#'><button>Accept</button></a>", " ", "<a href='#'><button>Ignore</button></a>";
echo "<br><br>";
}
} else {
echo "You don't have any friend request :(";

}

?>


How do I pass info (from a specific row) to another page, to then run a sql script?

PS: I usually get more than 1 result from this query.

Answer

If you want to send them via the $_POST superglobal, you can do the following:

<?php
   //your previous php code here that obtains the values
?>

<form action="otherpage.php" method="POST" id="myForm">
  <input type="hidden" name="v1" value="<?php echo /*value you want to pass*/?>">
  <input type="hidden" name="v2" value="<?php echo /*value you want to pass*/?>">
</form>

<script type="text/javascript">
    document.getElementById('myForm').submit(); // SUBMIT FORM
</script>

You would create as many inputs as you need. This can also be done automatically with php if the amount of inputs vary.

Then on page otherpage.php you would do something similar to:

<?php
  $v1  = $_POST['v1'];
  $v2  = $_POST['v2'];
?>

You can use mysql_real_escape_string() to sanitize your input, HOWEVER, if you are using PDO Prepared statements for your following queries while binding your parameters, then escaping should not be necessary as mentioned in the following thread: what is the PDO equivalent of mysql_real_escape_string