Jump to content

PHP error on MySQL insert


ashworth102680

Recommended Posts

I'm sure it's the simplest of issues, but I can't recall why this isn't working.

 

<?php
//setup our insert command
$sql = "INSERT INTO `my_db_name`.`my_table_name` (`id`, `first_name`, `last_name`, `email`, `birthday_month`, `birthday_day`, `phn1`, `phn2`, `phn3`, `ip_address`, `date_entered`) VALUES (NULL, $_POST['fName'], $_POST['lName'], $_POST['email'], $_POST['birthdayMonth'], $_POST['birthdayDay'], $_POST['phn1'], $_POST['phn2'], $_POST['phn3'], $_SERVER['REMOTE_ADDR'], date(YYYY-MM-DD HH:mm:SS));";

//do insert into MySQL
mysql_query($sql);
?>

 

The error I'm seeing is this...

 

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in handler_file.php

 

Any help is appreciated!

Link to comment
https://forums.phpfreaks.com/topic/179008-php-error-on-mysql-insert/
Share on other sites

when you have arrays queries you surround them with curly brackets. for example, you would do

{$_POST['fName']}

but for each time you access an array.

 

also you don't need a semi colon at the end of the sql query

 

also you should concatenate the return value for the date function. I don't believe you can just stick functions in strings like that

 

$sql = "blah blah blah " . date(YYYY-MM-DD HH:mm:SS) . ")";

That worked to get rid of the error, but produced another issue. Now I get no errors, but nothing enters my table. Table is empty. :(

 

$sql = "INSERT INTO `fantasticsams`.`eclub` (`id`, `first_name`, `last_name`, `email`, `birthday_month`, `birthday_day`, `phn1`, `phn2`, `phn3`, `ip_address`, `date_entered`) VALUES (NULL, {$_POST['fName']}, {$_POST['lName']}, {$_POST['email']}, {$_POST['birthdayMonth']}, {$_POST['birthdayDay']}, {$_POST['phn1']}, {$_POST['phn2']}, {$_POST['phn3']}, {$_SERVER['REMOTE_ADDR']}, {date(YYYY-MM-DD HH:mm:SS)});";

The or die() statement is telling me...

 

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 '@test.com, 03, 08, 111, 222, 3333, 127.0.0.1, {date('YYYY-MM-DD HH' at line 1

 

Seems to be outputting date()'s curly brackets. I've tried with and without the curly brackets.

Newest query updated with suggestions. Still dying though.

 

$sql = "INSERT INTO `DB`.`TABLE` (`id`, `first_name`, `last_name`, `email`, `birthday_month`, `birthday_day`, `phn1`, `phn2`, `phn3`, `ip_address`, `date_entered`) VALUES (NULL, {$_POST['fName']}, {$_POST['lName']}, {$_POST['email']}, {$_POST['birthdayMonth']}, {$_POST['birthdayDay']}, {$_POST['phn1']}, {$_POST['phn2']}, {$_POST['phn3']}, {$_SERVER['REMOTE_ADDR']}, ".date('YYYY-MM-DD HH:mm:SS').")";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.