简体   繁体   中英

I want to get id once delete button got clicked, I am unable to send its value in other page, php

if (mysqli_num_rows($res) > 0) {
    while ($row = mysqli_fetch_assoc($res)) {

        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['first_name'] . "</td>";
        echo "<td>" . $row['second_name'] . "</td>";
        echo "<td>" . $row['contact_no'] . "</td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "<td>" . $row['address'] . "</td>";
        echo "<td>" . $row['date_created'] . "</td>";
        echo "<td> <a href='delete.php' id=".$row['id']."'>Delete</a></td>";
        echo "</tr>";

    }
}
try this

echo "<td> <a href='delete.php' id='".$row['id']."'>Delete</a></td>";

You simply assign that id value to one PHP variable and you can call that anywhere you want.

if (mysqli_num_rows($res) > 0) {
    while ($row = mysqli_fetch_assoc($res)) {
         $id=$row['id'];
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['first_name'] . "</td>";
        echo "<td>" . $row['second_name'] . "</td>";
        echo "<td>" . $row['contact_no'] . "</td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "<td>" . $row['address'] . "</td>";
        echo "<td>" . $row['date_created'] . "</td>";
        echo "<td> <a href='delete.php' id='$id'>Delete</a></td>";
        echo "</tr>";

    }
}

Send value to delete.php like below

echo "<td> <a href='delete.php?id=".$row['id']."' id=".$row['id']."'>Delete</a></td>"; 

And get value of id on delete.php with like below

$id = $_GET['id'];

You can use get variable at the end of url and then use it another page as $_GET['variable'] for more information http://php.net/manual/en/reserved.variables.get.php

so change your code like this

<?php
if (mysqli_num_rows($res) > 0) 
    { 
        while ($row = mysqli_fetch_assoc($res)) { ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['first_name']; ?></td>
            <td><?php echo $row['second_name']; ?> </td>
            <td><?php echo $row['contact_no']; ?> </td>
            <td><?php echo $row['email']; ?></td>
            <td><?php echo $row['address']; ?></td>
            <td><?php echo $row['date_created']; ?></td>
            <td><a href="delete.php?id='<?php echo $row['id']; ?>'">Delete</a></td>
        </tr>
    <?php }
} ?>

on next page get the id of that button as $id = $_GET['id'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM