Jump to content

inserting date error


newbie_07

Recommended Posts

Hi gurus:

I want to insert current date into database.

I have made a column named current_date and its datatype is date.

I want to insert the current date into it.

 

Here's my code:

$sql = "INSERT INTO invoices SET client_id='$_POST[c_id]', service_name='$_POST[s_name]',
	total_price='$_POST[t_price]', current_date='$date' , due_date='$_POST[d_date]' ";

 

I got the following error:

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 'current_date='2008-05-14' , due_date='march, 5, 2006'' at line 3

You can see the in the current_date value is showing but its not inserting in database.

 

Please help

 

Link to comment
Share on other sites

try this

a few fixes and changed $date to CURDATE()

$sql = "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}', total_price='{$_POST['t_price']}', current_date='CURDATE()' , due_date='{$_POST['d_date']}' ";

 

if this is wrong can you post how your setting $date

Link to comment
Share on other sites

it could be if you have set your due_date column as a datetime then it will be expecting it to come at it in the format yyyy-mm-dd, you could try

 

$sql = "INSERT INTO invoices SET client_id='$_POST[c_id]', service_name='$_POST[s_name]',
total_price='$_POST[t_price]', current_date='$date' , due_date=DATE_fORMAT('$_POST[d_date]' , '%Y-%m-%d')";

Link to comment
Share on other sites

try this

a few fixes and changed $date to CURDATE()

$sql = "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}', total_price='{$_POST['t_price']}', current_date='CURDATE()' , due_date='{$_POST['d_date']}' ";

 

if this is wrong can you post how your setting $date

 

Still not working.

Here's my full code:

 


$date = date("Y-m-d");

$sql = "INSERT INTO invoices SET client_id='$_POST[c_id]', service_name='$_POST[s_name]', service_description='$_POST[s_desc]',
	comments='$_POST[comments]', init_price='$_POST[i_price]', price='$_POST[price]', adjustments='$_POST[adj]',
	total_price='$_POST[t_price]', current_date='CURDATE()' , due_date='$_POST[d_date]' ";

 

Error is:

 

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 'current_date='CURDATE()' , due_date='march, 5, 2006'' at line 3

 

 

Link to comment
Share on other sites

$sql = "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}', total_price='{$_POST['t_price']}', current_date=CURDATE() , due_date='{$_POST['d_date']}' ";

$sql = "INSERT INTO invoices SET client_id='$_POST[c_id]', service_name='$_POST[s_name]', total_price='$_POST[t_price]', current_date=CURDATE() , due_date='$_POST[d_date]' ";

 

Will both work :)

 

And thanks MadTechie, finally discovered the BB code to make the syntax colour coded without the constant use of <?php ?>, staring at me in the face '-.-

Link to comment
Share on other sites

$sql = "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}', total_price='{$_POST['t_price']}', current_date=CURDATE() , due_date='{$_POST['d_date']}' ";

$sql = "INSERT INTO invoices SET client_id='$_POST[c_id]', service_name='$_POST[s_name]', total_price='$_POST[t_price]', current_date=CURDATE() , due_date='$_POST[d_date]' ";

 

Will both work :)

you should use

client_id='{$_POST['c_id']}'

or

"client_id='".$_POST['c_id']'."etc

 

 

And thanks MadTechie, finally discovered the BB code to make the syntax colour coded without the constant use of <?php ?>, staring at me in the face '-.-

 

Noooooooooooooooooooooooo.

Link to comment
Share on other sites

$sql = "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}', total_price='{$_POST['t_price']}', current_date=CURDATE() , due_date='{$_POST['d_date']}' ";

$sql = "INSERT INTO invoices SET client_id='$_POST[c_id]', service_name='$_POST[s_name]', total_price='$_POST[t_price]', current_date=CURDATE() , due_date='$_POST[d_date]' ";

 

Will both work :)

you should use

client_id='{$_POST['c_id']}'

or

"client_id='".$_POST['c_id']'."etc

 

 

And thanks MadTechie, finally discovered the BB code to make the syntax colour coded without the constant use of <?php ?>, staring at me in the face '-.-

 

Noooooooooooooooooooooooo.

 

 

I have applied both, still not working.

Here's the Query:

$sql = "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}',
service_description='{$_POST['s_desc']}', comments='{$_POST['comments']}',
init_price='{$_POST['i_price']}', price='{$_POST['price']}', adjustments='{$_POST['adj']}',
total_price='{$_POST['t_price']}', 
current_date=CURDATE() , due_date='{$_POST['d_date']}' ";

 

This is the result from: print $sql;

INSERT INTO invoices SET client_id='3', service_name='Fan Cleaning', service_description='Bottom to Top', 
comments='Web Development', init_price='50', price='40', adjustments='20', 
total_price='30', current_date=CURDATE() , due_date='march, 5, 2007' 
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 'current_date=CURDATE() , 
due_date='march, 5, 2007'' at line 4

Link to comment
Share on other sites

whats the field type of due_date ?

if its date you need to reformat the date to the same at the $date

 

a table structure dump will help

 

No No ...

The field type of due_date is varchar and i am simply getting a value from a textbox. It has no problem.

I want that whenever a record is inserted, the current date should be inserted into the current_date column and its datatype is date.

 

Link to comment
Share on other sites

My Test works fine

 

Dump

-- 
-- Table structure for table `Test`
-- 

CREATE TABLE `test` (
  `uID` mediumint( NOT NULL auto_increment,
  `Date` date NOT NULL,
  PRIMARY KEY  (`uID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

 

My SQL statment

INSERT INTO `Test`.`Test` (
`uID` ,
`Date`
)
VALUES (
NULL , CURDATE( )
);

 

 

edit (tagged it)

 

edit#2 try

changing CURDATE( ) to NOW( )

Link to comment
Share on other sites

Here's the table structure:

 

--
-- Table structure for table `invoices`
--

CREATE TABLE `invoices` (
  `invoice_id` int(50) NOT NULL auto_increment,
  `client_id` int(50) default NULL,
  `service_name` varchar(255) default NULL,
  `service_description` varchar(255) default NULL,
  `comments` varchar(255) default NULL,
  `init_price` int(50) default NULL,
  `price` int(50) default NULL,
  `adjustments` int(50) default NULL,
  `total_price` int(50) default NULL,
  `current_date` date NOT NULL,
  `due_date` varchar(255) default NULL,
  PRIMARY KEY  (`invoice_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

 

And the query still not working :(.

Link to comment
Share on other sites

Ahhh i spotted the problem,

current_date is reserved

use back ticks ` ie

`current_date`

see below

$sql = "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}', total_price='{$_POST['t_price']}', `current_date`=CURDATE() , due_date='{$_POST['d_date']}' ";

Link to comment
Share on other sites

Ahhh i spotted the problem,

current_date is reserved

use back ticks ` ie

`current_date`

see below

$sql = "INSERT INTO invoices SET client_id='{$_POST['c_id']}', service_name='{$_POST['s_name']}', total_price='{$_POST['t_price']}', `current_date`=CURDATE() , due_date='{$_POST['d_date']}' ";

 

Great! Excellent that works fine.....just problem of the reserved word....

Now plz show me how to insert date from the list boxes, like year, month and day.....

Link to comment
Share on other sites

Correct safe method

<?php
// database connection..

$client_id=mysql_real_escape_string($_POST['c_id']);
$service_name=mysql_real_escape_string($_POST['s_name']);
$total_price=mysql_real_escape_string($_POST['t_price']);
$due_date=mysql_real_escape_string($_POST['d_date']);

$sql = "INSERT INTO invoices SET client_id='$client_id, service_name='$service_name', 
total_price='$total_price', `current_date`=CURDATE() , due_date='$due_date' ";

$sql_result=mysql_query($sql)or dir(mysql_error());

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.