我的PHP代码没有成功地将记录插入我的数据库。 Mysql在我指定INSERT INTO [duplicate]的行上给出了语法错误

This question already has an answer here:

My php code is not successfully inserting records to my database. Mysql is giving me a syntax error on the line where I specify INSERT INTO

$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$select_db = mysqli_select_db($conn, $dbsel) or die ($dberror2);
$sql = "INSERT INTO `lit_order_table` (`email`, 
name_first,name_last,company,address,apt,city,state,postal_code,country,ord_options,quantity,message)

VALUES ($email,$name_first,$name_last,$company,$address,$apt,$city,$state,$postal_code,$country,$ord_options,$quantity,$message)";


$conn->close();
</div>

Use as below:

VALUES ('{$email}','{$name_first}','{$name_last}','{$company}','{$address}','{$apt}','{$city}','{$state}','{$postal_code}','{$country}','{$ord_options}','{$quantity}','{$message}')";

The above should work. The defined variables has to be wrapped with '{}' to be inserted to the database

You need to add single quotes around string values.

Corrected SQL:

$sql = "INSERT INTO lit_order_table (email, name_first, name_last, 
company, address, apt, city, state, postal_code, country, ord_options, quantity, message)

VALUES ('$email', '$name_first', '$name_last', '$company', '$address'
 ,'$apt', '$city', '$state', '$postal_code', '$country', '$ord_options', 
'$quantity', '$message')";

connect to db and try like

$sql = "INSERT INTO lit_order_table (email, name_first, name_last, 
company, address, apt, city, state, postal_code, country, ord_options, quantity, message)VALUES ('$email', '$name_first', '$name_last', '$company', '$address','$apt', '$city', '$state', '$postal_code', '$country', '$ord_options', '$quantity', '$message')";