Reputation: 13
I have the following code:
<form action="" method="POST">
<input id="delete" name="delete" placeholder="Name" type="text"></b>
<input type="submit" name="deleteButton" value="Go!">
</form>
<?php
if (isset($_POST['deleteButton'])) {
$temp = $_POST['delete'];
$deleteSQL = "DELETE FROM `berater` WHERE `Nachname` LIKE '$temp' OR `Vorname` LIKE '$temp' OR CONCAT(Vorname,' ', Nachname) LIKE '%$temp%'";
$searchSQL = "SELECT FROM `berater` WHERE `Nachname` LIKE '$temp' OR `Vorname` LIKE '$temp' OR CONCAT(Vorname,' ', Nachname) LIKE '%$temp%'";
$search = mysqli_query($db_link, $searchSQL);
$loeschen = mysqli_query($db_link, $deleteSQL);
if (!$search) {
echo "Kein Datensatz gefunden!";
} else {
echo "$temp erfolgreich gelöscht.";
}
}
I am new to PHP and thought the mysqli_query
returns false
if the name is not found in the database. What am I doing wrong here?
Upvotes: 0
Views: 63
Reputation: 171
Try using mysqli_num_rows
:
if (mysqli_num_rows($search)==0) {
echo "Kein Datensatz gefunden!";
} else {
echo "$temp erfolgreich gelöscht.";
}
Upvotes: 0
Reputation: 2296
According to mysqli_query() documentation:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
Replace if (!$search)
with if (mysqli_num_rows($search) > 0)
to check if such row existed.
By the way, this code can not be used in production: it is vulnerable to SQL injection attacks. I advise you to read this article before working with the database: http://php.net/manual/en/security.database.sql-injection.php
Upvotes: 1