Skip to content
Advertisement

using “INSERT INTO” statement in php code doesn’t work

I’m trying to build a database for a bookstore with 3 tables: Book, Volume and Publication. I’m using mysqli() and the code neither works nor echoes any errors.

JavaScript

Advertisement

Answer

The problem is that you are checking with === TRUE.

As PHP Manual says:

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.

While your error checking would work for INSERT or UPDATE queries the SELECT query will not return true. In fact checking like this for boolean values is completely unnecessary.

Remove the check for === TRUE and your code should work fine.

JavaScript

If you enable mysqli error reporting, you don’t need any if statements, which will make your code much simpler:

JavaScript

In addition, your values in the last update queries are missing quotes.

JavaScript

Also there is no need to execute your SELECT query twice to get the values. You should refactor your code so that the second query ($totalNum=$conn->query($sql);) is not needed.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement