Issue
So in this part of the website i'm making an edit person information page and if there are more than one person with the name searched you get a table with all the persons; you choose the needed one and edit it in another page. I need to pass the array that matches with the person selected. I don't know how to pick the array in the other page through POST. This is a part of code of the page that sends the array:
$squery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
$num = mysqli_num_rows($squery);
$i=0;
$array= array();
while($rowa=mysqli_fetch_assoc($squery)){
$array[$i]=$rowa;
$i++;
}
$ssquery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
if($num > 1) {
echo 'Trovato più di un elemento';
echo '<table border="1">';
echo '<tr>';
echo '<td><p>S</p></td>';
echo '<td><p>Nome</p></td>';
echo '<td><p>Cognome</p></td>';
echo '<td><p>Città</p></td>';
echo '</tr>';
$i=0;
echo '<form method="POST" action="moficatr.php">';
while ($row = mysqli_fetch_array($ssquery)) {
echo '<tr>';
echo '<td> <p> <input type="Radio" name="persona" value="'.
$rowa[$i] . '"></p></td>';
echo ' <td><p>' .$row['Nome'] . '</p></td>';
echo '<td><p>'.$row['Cognome'].'</p></td>';
echo '<td><p>'.$row['Citta'].'</p></td>';
echo '</tr>';
$i++;
}
echo '</table>';
echo '<br><br><input type="submit" value="Modifica"></form>';
}
Solution
You can simply pass the selected user's ID to the next page and then fetch all the details using the ID from that page instead of sending the whole array of data to the other side.
Why are you executing the same query twice in your code ? you only need one query which will give you all the data.
Also I strongly encourage you to use PDO please - http://php.net/manual/en/book.pdo.php Or at least prepared statments which is supported in MySQLi
as well
$ssquery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
$num = mysqli_num_rows($ssquery);
if($num > 1) {
echo 'Trovato più di un elemento';
echo '<table border="1">';
echo '<tr>';
echo '<td><p>S</p></td>';
echo '<td><p>Nome</p></td>';
echo '<td><p>Cognome</p></td>';
echo '<td><p>Città</p></td>';
echo '</tr>';
echo '<form method="POST" action="moficatr.php">';
while ($row = mysqli_fetch_assoc($ssquery)) {
echo '<tr>';
echo '<td> <p> <input type="Radio" name="persona" value="'.
$row['id'] . '"></p></td>'; //you can change 'id' with your column name if it's different
echo ' <td><p>' .$row['Nome'] . '</p></td>';
echo '<td><p>'.$row['Cognome'].'</p></td>';
echo '<td><p>'.$row['Citta'].'</p></td>';
echo '</tr>';
$i++;
}
moficatr.php
//Set up a Mysql connection.
$user_id = (int) $_POST['persona'];
//Query the db to fetch all the details of this user.
$query = "SELECT * from your_table where id=?";
//Prepare statment and execute.
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $name, $address,...);
echo $name;
echo $address;
Answered By - Vishnu Nair
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.