简体   繁体   中英

Generate buttons with php that delete or edit a row in a table in db

I need to call database and a specific table, then display table values in rows and at the end of them have buttons 'edit' and 'delete' that allow me to edit that line or delete it completely. At the moment I only manage to display the table and generate the buttons, but not 'tie' the buttons to that row. I need to generate the buttons when displaying the table, and then 'tie' the buttons to corresponding row.

Edit:

if(!empty($_POST) && isset($_POST['show'])) {
    $sel = "SELECT id, vardas, pavarde, amzius, miestas FROM zmogaus_info";
    $res = mysqli_query($conn, $sel);
    if(mysqli_num_rows($res)>0){
        while($row = mysqli_fetch_assoc($res)){
            echo "Id: ".$row["id"]." ".$row["vardas"]." ".$row["pavarde"]." ".$row["amzius"]."m."."<input type='submit' name='".$row['id']."' value='Delete'>"."<br>";
        }
    }
}
if(!empty($_POST) && isset($_POST[$row['id']])){
    $sql = "DELETE FROM zmogaus_info WHERE id=".$row['id'];
    mysqli_query($conn, $sql);
}

I think the problem might be with the way I use $row['id'] and i might need to make it into a global variable?

If you are able to var_dump($result) and there is data there from DB, then there is no need for a while loop.

Also research PDO, much safer method of data transfer to and from DB.

$sel = "SELECT id, vardas, pavarde, amzius, miestas FROM zmogaus_info";
$result = $mysqli -> query($sel);

// Define the associative array as $row
$row = $result -> fetch_assoc();

echo "Id: ".$row["id"]." ".$row["vardas"]." ".$row["pavarde"]." ".$row["amzius"]."m."."<input type='submit' name='".$row['id']."' value='Delete'>"."<br>";

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