我有以下代码:
<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.";
}
}
我是PHP新手,如果在数据库中找不到名称,我认为mysqli_query
会返回false
。我在这里做错了什么?
答案 0 :(得分:1)
根据mysqli_query() documentation:
失败时返回FALSE。成功的SELECT,SHOW,DESCRIBE或 EXPLAIN查询mysqli_query()将返回 mysqli_result对象。对于 其他成功的查询mysqli_query()将返回TRUE。
将if (mysqli_num_rows($search) > 0)
替换为String result = s.replaceAll("(?i)(\\b\\w+\\b)\\s+\\1", "$1");
以检查此行是否存在。
顺便说一下,这段代码不能用于生产:它容易受到SQL注入攻击。我建议您在使用数据库之前阅读本文:http://php.net/manual/en/security.database.sql-injection.php
答案 1 :(得分:0)
尝试使用if (mysqli_num_rows($search)==0) {
echo "Kein Datensatz gefunden!";
} else {
echo "$temp erfolgreich gelöscht.";
}
:
String str ="Java";