'SQL - Delete Statement Not Working
Can someone take a look at my delete statement and let me know why this might not be working? As you can see the Add Statement is here as well...This works perfectly...I traded out a statement in the DELETE and it does not work.
Add Statement Works Perfect:
<?php
include 'include/dbconnect.php';
$usr_id=$_POST['id'];
$usr_name=$_POST['username'];
$item_id=$_POST['itemid'];
$sql = "SELECT * FROM useritems where userid='$usr_id' And itemid='$item_id'";
$result = mysqli_query($conn, $sql);
//echo 'incoming data '.$result;
if (mysqli_num_rows($result) <1) {
$sql = "insert into useritems (userid,username,itemid) values ('$usr_id','$usr_name','$item_id')";
mysqli_query($conn, $sql);
echo 'success';
}
else
echo 'fail';
Delete Statement does not:
?>
<?php
include 'include/dbconnect.php';
$usr_id=$_POST['id'];
$usr_name=$_POST['username'];
$item_id=$_POST['itemid'];
$sql = "SELECT * FROM useritems where userid='$usr_id' And itemid='$item_id'";
$result = mysqli_query($conn, $sql);
//echo 'incoming data '.$result;
if (mysqli_num_rows($result) <1) {
$sql = "DELETE FROM useritems WHERE userid='$usr_id' AND itemid='$item_id'";
mysqli_query($conn, $sql);
echo 'success';
}
else
echo 'fail';
?>
Solution 1:[1]
You only go in to the DELETE Statement if you found num rows less then 1. That means : no ROWS:
if (mysqli_num_rows($result) <1) {
Change it to
if (mysqli_num_rows($result) >0) {
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Barmar |
