-1

I want there is a delete button in every row in table and when click the button for example in row 1 then the row 1 deleted.

and EXTRA if you know how instead of delete, when I press the button a value in the table changes. For example when click delete btn then value "OK" in table change to "DELETED".

The code work right for the specific id that put in delete.php file.

I'm not interested in database security I just want to find a way to make it work.

index.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "userform";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);



if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo '
<table>
<tr>
<td>Fullname</td>
<td>', $row['name'] . $row['lname'], '</td>
</tr>
<br>
<tr>
<td>Email address</td>
<td>', $row['email'], '</td>
</tr>
<br>
<tr>
<td>Reservation ID</td>
<td>', $row['rescode'], '</td>
</tr>
<br>
<tr>
<td>Check-in</td>
<td>', $row['checkin'], '</td>
</tr>
<br>
<tr>
<td>Check-out</td>
<td>', $row['checkout'], '</td>
</tr>
<br>
<tr>
<td>Mobile number</td>
<td>', $row['mnumber'], '</td>
</tr>
<br>
<tr>
<td>Total price</td>
<td>', $row['totalprice'], '</td>

<td><a href="delete.php">Cancel</a></td>
</tr>

</table>';
  }
} else {
  echo "0 results";
}
$conn->close();
?>

<script>

delete.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "userform";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);


$sql = "DELETE FROM usertable WHERE id='10'";


if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
  } else {
    echo "Error deleting record: " . $conn->error;
  }

$conn->close();
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Stal
  • 45
  • 9
  • you should select the ids also add this id a hidden field or put as parameter button in the form. – nbk Jan 14 '22 at 18:34
  • 1
    For example https://stackoverflow.com/questions/17144846/add-delete-button-to-php-results-table or https://www.11zon.com/zon/php/php-mysql-delete-row-button.php or – nbk Jan 14 '22 at 18:36
  • I tried but they do not work. Do you have something to suggest me for the above code. – Stal Jan 14 '22 at 18:51
  • the eaxample show what i mentioned in my first comment. You can make a get and a delete ling with a button first example and the code to delete is in the second. you have everysthing you need – nbk Jan 14 '22 at 19:00

2 Answers2

2

Query

This query:

$sql = "SELECT name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);

should be

$sql = "SELECT id, name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
$result = $conn->query($sql);

Delete link

This html code

<td><a href="delete.php">Cancel</a></td>

should be

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

Please, ... check your code: you concat strings using "," instead of ".".

Finally

This SQL code

$sql = "DELETE FROM usertable WHERE id='10'";

should be

$sql = "DELETE FROM usertable WHERE id='" . $_GET['id'] . "'";
sensorario
  • 20,262
  • 30
  • 97
  • 159
1

try this: in index.php,

  • add 'id' in sql. $sql = "SELECT id, name, email, code, status, lname, checkin, checkout, mnumber, totalprice, rescode FROM usertable";
  • than pass the id in cancel link, i.e. href="delete.php?id=".$row['id']

in delete.php, add this line $id= $GET['id']; and pass $id to delete query, $sql = "DELETE FROM usertable WHERE id=$id";

Dharman
  • 30,962
  • 25
  • 85
  • 135
php12
  • 19
  • 1