-3

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();
Happy Coding
  • 2,517
  • 1
  • 13
  • 24
trecoolable
  • 187
  • 1
  • 3
  • 12

3 Answers3

1

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')";
Pupil
  • 23,834
  • 6
  • 44
  • 66
1

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')";
chris85
  • 23,846
  • 7
  • 34
  • 51
satish kilari
  • 746
  • 4
  • 21
-1

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

Pawan
  • 1,537
  • 1
  • 15
  • 19
olayinka
  • 3
  • 4
  • 1
    In this application the `{}` is not needed. If the values are array elements then it's needed i.e. `{$user['email']}`. The `{}` tells PHP to process it separately. – David Fairbanks Sep 25 '15 at 05:26