I made a simple HTML web page with a list of emplyees (only two atm).
<form method="post" action = "del.php">
<table border = "1">
<tr>
<th>Employee Name</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lavoratori";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT nome, id FROM operai";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo"<tr>";
echo'<td><input type = "checkbox" name = checkbox[]" value = '.$row['id']."<td>".$row['nome']."</td>";
echo"</tr>";
}
echo"</table>";
}
$conn->close();
?>
<input type = "submit" name = "delete" id = "delete" value = "Delete Records">
</form>
This is del.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lavoratori";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['delete'])){
$chkarr = $_POST['checkbox'];
foreach($chkarr as $id){
$sql = "DELETE FROM operai WHERE id = .$id.";
$result = $conn->query($sql);
}
header("Location: /test_purpose/home.php");
}
$conn->close();
?>
Can you guys tell me what's going on? I'm new to PHP. What i'm trying to do is to delete a specific a row from selecting with a checkbox. And it does not work, it redirects me back to the main page, without deleting anything obviously.