I'm trying to catch all data from MySQL to check data already exists or not if data already exists then it should update else insert new data. I have 7 to 8 conditions in where clause, some of them are null now the issue is when i run query its not catching null column thats why its insert new data instead of updating below is my code
$fetch_stock = mysqli_query($connection,
"SELECT *
FROM `book_stock`
WHERE book_name = '$book_name'
AND author_id = $book_author
AND author_id1 = $book_author1
AND author_id2 = $book_author2
AND author_id3 = $book_author3
AND author_id4 = $book_author4
AND author_id5 = $book_author5
AND author_id6 = $book_author6"
);
if (mysqli_num_rows($fetch_stock) > 0) {
echo "UPDATE book_stock SET stock_count = stock_count + 1 WHERE book_name = '$book_name' AND author_id = $book_author AND author_id1 = $book_author1 AND author_id2 = $book_author2 AND author_id3 = $book_author3 AND author_id4 = $book_author4 AND author_id5 = $book_author5 AND author_id6 = $book_author6";
mysqli_query($connection,
"UPDATE book_stock
SET stock_count = stock_count + 1
WHERE book_name = '$book_name'
AND author_id = $book_author
AND author_id1 = $book_author1
AND author_id2 = $book_author2
AND author_id3 = $book_author3
AND author_id4 = $book_author4
AND author_id5 = $book_author5
AND author_id6 = $book_author6"
);
} else {
echo "INSERT INTO book_stock (book_name, author_id, author_id1, author_id2, author_id3, author_id4, author_id5, author_id6, stock_count) value ('$book_name', $book_author,$book_author1, $book_author2, $book_author3, $book_author4, $book_author5, $book_author6, 1)";
mysqli_query($connection,
"INSERT INTO book_stock
(book_name, author_id, author_id1, author_id2,
author_id3, author_id4, author_id5, author_id6,
stock_count)
value ('$book_name', $book_author,$book_author1, $book_author2,
$book_author3, $book_author4, $book_author5, $book_author6, 1)");
}
When i print query Its look like this
INSERT INTO book_stock
(book_name, author_id, author_id1, author_id2, author_id3,
author_id4, author_id5, author_id6, stock_count)
value ('cat demo1', 1,2, 3, 4, NULL, NULL, NULL, 1)
I got solution for this from 1 website, which is
$fetch_stock = mysqli_query($connection,
"SELECT *
FROM `book_stock`
WHERE book_name = '$book_name'
AND author_id = $book_author
AND IFNULL(author_id1, 0)= IFNULL($book_author1, 0)
AND IFNULL(author_id2, 0)= IFNULL($book_author2, 0)
AND IFNULL(author_id3, 0)= IFNULL($book_author3, 0)
AND IFNULL(author_id4, 0)= IFNULL($book_author4, 0)
AND IFNULL(author_id5, 0) = IFNULL($book_author5, 0)
AND IFNULL(author_id6, 0) = IFNULL($book_author6, 0)"
);
Its working when i want to display stock.
But i want to do a crud operation by using this logic.
I tried to do from above solution, its work fine but, in db table its update null with 0.
which is not got as per me. please guide me with best solution.
Thanks