当前位置: 动力学知识库 > 问答 > 编程问答 >

php - SQL syntax ERROR in STR_TO_DATE

问题描述:

You have an error in your SQL syntax; check the manual that

corresponds to your MySQL server version for the right syntax to use

near ' STR_TO_DATE('1988-02-04','%m/%d/%Y'),

$sql = "INSERT INTO customer_registration( ";

$sql .= "CUSTOMER_FNAME, CUSTOMER_LNAME, CUSTOMER_DOB, APARTMENT, ";

$sql .= "STREET, CITY, PROVINCE, POSTAL_CODE, EMAIL, PHONE, SIGN_IN_DATE ";

$sql .= ") VALUES (";

$sql .= " '{$fname}', {$lname}, STR_TO_DATE('$dob','%m/%d/%Y'), {$apt}, {$city}, {$province}, {$postalCode}, {$_email}, {$phone}, {NOW()}";

$sql .= ")";

$result= mysqli_query($connection, $sql);

What seems to be the error ?

网友答案:

In order for it to work, all I had to do was add the quotes and remove the quotes off the functions.

$sql  = "INSERT INTO customer_registration( ";
$sql .= "CUSTOMER_FNAME, CUSTOMER_LNAME, CUSTOMER_DOB, APARTMENT, ";
$sql .= "STREET, CITY, PROVINCE, POSTAL_CODE, EMAIL, PHONE, SIGN_IN_DATE ";
$sql .= ") VALUES (";
$sql .= " '{$fname}', '{$lname}', '{$dob}', '{$apt}', '{$street}', '{$city}', '{$province}', '{$postalCode}', '{$_email}', '{$phone}', NOW()";
$sql .= ")";
网友答案:

1) Use strtotime() on your first date. Then date('Y-m-d') to convert it back.

2) Use Single quote ' in your values. Like '{$lname}'

3) Remove Single quote ' from Now()

<?
$dob = strtotime($dob);
$dob = date('%m/%d/%Y',$dob);

$sql  = "INSERT INTO customer_registration( ";
$sql .= "CUSTOMER_FNAME, CUSTOMER_LNAME, CUSTOMER_DOB, APARTMENT, ";
$sql .= "STREET, CITY, PROVINCE, POSTAL_CODE, EMAIL, PHONE, SIGN_IN_DATE ";
$sql .= ") VALUES (";
$sql .= " '{$fname}', '{$lname}', '{$dob}', '{$apt}', '{$city}', '{$province}', '{$postalCode}', '{$_email}', '{$phone}', NOW()";
$sql .= ")";
$result= mysqli_query($connection, $sql);

?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: