Cannot insert data into a table

I have created 4 tables. The 4th one has just one field. I cannot insert data into any of them.

I have created the tables without any error feedback.

I am using the command likie:
INSERT INTO restaurant4 (lotno) VALUES ($lotno) ;

I get error messages each time and I don’t think any data has been inserted.

Why doesn’t this work?

Mike Smith

We dont have a crystal ball and cannot see your screen. How about posting your code and the actual error messages.

1 Like

The code is:

$sql = "INSERT INTO restaurant2 (lotno, fname)
VALUES ($lotno, $fname )"  ;

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

The Error message is:
INSERT INTO restaurant2 (lotno, fname) VALUES (1111, John )
Unknown column ‘John’ in ‘field list’

Use prepared statements and your problem will be solved

I assume that when you say “prepared statements” you mean something like below:

Here is the code for line 110:
INSERT INTO restaurant2 (lotno, fname) VALUES ($lotno, $fname ) ;

Here is the error message:
Parse error:
syntax error, unexpected ‘INTO’ (T_STRING) in C:\PHP Samples\Restaurant\RestaurantStore1t.php on line 110

Mike Smith

No, a prepared statement is where you use replaceable parameters in the query, call the prepare() method, and then call the execute() method with values for those parameters.

Have a look at the code being discussed in this thread and you’ll see the idea: Use a checkbox to update a mysql table - #16 by droopsnoot

Prepared statements get around a lot of issues such as properly quoting values inside queries.

1 Like

Yes, it worked perfectly.

Thank you so much.

Mike Smith

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.