-2

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?

webnub
  • 41
  • 7
  • 2
    First you shoukld urgently switch to prepared statements. https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php When you submit your delete it ddoesn not know the variable row. it is the name of the select see https://stackoverflow.com/a/11404667/5193536 – nbk Apr 05 '20 at 17:58

1 Answers1

0

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>";
dale landry
  • 7,831
  • 2
  • 16
  • 28
  • I changed up my code according to what u showed me, it only displayed the 1st line, then i made a loop and it shows all but the 1st line. Another problem is still the same, The buttons do not work, if i press them they do absolutely nothing for some reason and i do not know why – webnub Apr 06 '20 at 03:07
  • Okay, so you're fetching more than one result of likely user info and you wish to create a table with each users info that is being parsed from the website? When you dump your associative array $row, are all the results there or is it just one of the users? What exactl do you want your button to do? Delete the entry of that user from the DB? – dale landry Apr 07 '20 at 00:03