Home » Php » PHP, POST data from one row only (id) from WHILE loop, when SUBMIT

PHP, POST data from one row only (id) from WHILE loop, when SUBMIT

Posted by: admin February 25, 2020 Leave a comment

Questions:

Issue, how can I POST needed data (id,username, etc.) only for one row, from while loop, when submit?

Code (I am using POST as call “function” delete_member, with if condition, what is better way?).

if(isset($_POST["delete_member"]))
{
# Delete member
echo $id=$_POST["id"];
$sql="delete from members where id = $id";
mysqli_query($db,$sql) ? $status = true : $status = false;
message($status);
}

$select_members = mysqli_query($db,"select * from members");
while($row = mysqli_fetch_array($select_members))
{
echo('
<tr>
<td><input type="text name="id" value="'.$row['id'].'" readonly></td>
<td><input type="text name="username" value="'.$row['username'].'" readonly></td>
<td><input type="text name="password" value="'.$row['password'].'" readonly></td>
<td><input type="text name="active" value="'.$row['active'].'" readonly></td>
<td><input type="submit" name="delete_member" value="Smazat"></td>
<td><input type="submit" name="send_email" value="Odeslat email"></td>
<tr>
');
}

Nothing stored in echo $id=$_POST["id"];. Is possible send id from input type="submit"? For example like this.

<input type="submit" name="delete_member" id=".$row['id']." value="Smazat">

Thanks.

How to&Answers:
if(isset($_POST["delete_member"]))
{
# Delete member
echo $id=$_POST["id"];
$sql="delete from members where id = $id";
mysqli_query($db,$sql) ? $status = true : $status = false;
message($status);
}

$select_members = mysqli_query($db,"select * from members");
while($row = mysqli_fetch_array($select_members))
{
echo('
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['username'].'</td>
<td>'.$row['password'].'</td>
<td>'.$row['active'].'</td>

<td><form method="post" action="HERE_FILL_IN_SCRIPT_URL"><input type="hidden" name="id" value="'.$row['id'].'"><input type="submit" name="delete_member" value="Smazat"><input type="submit" name="send_email" value="Odeslat email"></form></td>
<tr>
');
}

Answer:

at the end, best method is change input to button, where value is not displayed, so value can be $row['id'] and name is between tags<button>NAME</button>.

if(isset($_POST["deleteMember"]))
{
# Delete member
$id=$_POST["deleteMember"];
$sql="delete from members where id = $id";
mysqli_query($db,$sql) ? $status = true : $status = false;
message("Smazání člena",$status);
}

while($row = mysqli_fetch_array($select_members))
{
echo('
<tr>
<td><input type="text name="id[]" value="'.$row['id'].'" readonly></td>
<td><input type="text name="username[]" value="'.$row['username'].'" readonly></td>
<td><input type="text name="password[]" value="'.$row['password'].'" readonly></td>
<td><input type="text name="active[]" value="'.$row['active'].'" readonly></td>
<td><input type="text name="sentEmailAll" value="'.$row['email_sent'].'" readonly></td>
<td><button type="submit" name="deleteMember" value="'.$row['id'].'">Vymazat člena</button></td>
<tr>
');
}

But I thing this is not elegant, I believe there is better way to solve similar things.