Insert query not working

I am trying to insert information into my mysql database using the following code:

//$query = "INSERT INTO book_sales (wborder_number) VALUES ($order_number)"; // this works

$query = "INSERT INTO book_sales (wborder_number, wbstate) VALUES ($order_number, $state)"; // error in syntax

mysqli_query($cnct, $query);

(I attempted to format that using Control-K, but it doesn’t look like it worked. Sorry!)

If I use the first query, it inserts the order_number just fine. If I use the second query, though, I get the following error:

It seems to be trying to insert into a field labeled “Alabama” in my database, which contains no such field. The field I am trying to insert into is called, as in my code, “wbstate”, which is a valid field, as shown in the following clip from my phpMyAdmin:

The value contained in the variable “$state”, though, is indeed “Alabama”, so it seems to be confusing the value in the variable with the name of the field!

I have tried many different combinations of inserting into various fields, and they all seem to do the same. If I only insert into the “order_number” field, it works fine, but if I try to insert into any other field, I get an error message similar to the one displayed. Sometimes it will state a field which is different than what I call for; other times I just get an error saying that there is an error in my syntax. It just seems to me to be “screwy”!!! Does anyone know what is going on here?

Hi Wadori,

This forum is dedicated to Microsoft SQL Server, so we may not be able to provide detailed assistance with MySQL/PHP issues. I recommend first testing the INSERT operation directly in your database. If it succeeds, the issue is likely in your application code; if it fails, the cause is probably within the database layer.

Based on the information provided, it’s difficult to give a definitive answer. You might try adjusting your query as follows:

php

$query = "INSERT INTO book_sales (wborder_number, wbstate) VALUES ($order_number, '$state')";

I also encourage you to make use of AI tools for questions like this — they can be very helpful and often provide clear explanations.

Finally, please make sure you are familiar with SQL injection risks before deploying your application to production. Understanding this topic is essential for building secure applications.

(I use AI to refine my English responses, as it is not my native language.)

Thank you.

Has been resolved. Thank you all.