Detect that a record to be deleted does not exist in the SQL table (MySQL)

1

Thanks for helping me a lot. I have a simple doubt.

I made a form that deletes records from a table, it works correctly, but I can not get an error message saying that the record does not exist, it simply says that I delete it (When that record is not present, it says that I delete it, when it's not true).

This is what I have done so far.

The html:

<div id="borrador"> <!-- La forma de borrar datos -->
<form method="POST" action="disenoclaseborrar.php" style="padding:50px 250px;">
<h1>Escriba el nombre del profesor para borrar su clase</h1>
<input type="text" placeholder="Escriba el nombre del profesor para borrar la clase" name="nombreprofe" id ="nombreprofe" required></input>
<div id="input submit" style="text-align: center;;"><input type="submit"  id="submit" name="submit" class="floated" value="Borrar clase"></input></div>
</form>

The php:

<?php
$nombreprofe = $_POST['nombreprofe'];
$db = mysqli_connect('localhost', 'root', '', 'prueba');
$nombreprofe = mysqli_real_escape_string($db, $nombreprofe);

$query = "DELETE FROM reservadiseno WHERE nombre = '$nombreprofe'"; 
$result = mysqli_query($db, $query);
$res = '';

if ($result > 0) {
$res = 'Se borraron los datos. Refresque la pagina para ver la tabla denuevo';
echo $res;
}else{
$res = "No se encontro nada para borrarlo";
echo $res;
}

?>
    
asked by Shredder 04.10.2018 в 16:15
source

1 answer

0

You can use mysqli_affected_rows :

  

Gets the number of rows affected in the last MySQL operation

int mysqli_affected_rows ( mysqli $link )

  

Returns the number of rows affected by the last query INSERT, UPDATE, REPLACE or DELETE.

Example:

<?php
$nombreprofe = $_POST['nombreprofe'];
$db = mysqli_connect('localhost', 'root', '', 'prueba');
$nombreprofe = mysqli_real_escape_string($db, $nombreprofe);

$query = "DELETE FROM reservadiseno WHERE nombre = '$nombreprofe'"; 
$result = mysqli_query($db, $query);
$res = '';

// AQUI
if (mysqli_affected_rows($db) > 0) {
$res = 'Se borraron los datos. Refresque la pagina para ver la tabla denuevo';
echo $res;
}else{
$res = "No se encontro nada para borrarlo";
echo $res;
}

?>
    
answered by 04.10.2018 в 16:32