Jump to content

[SOLVED] Problem with inserting date


nati

Recommended Posts

Hello everybody,

 

I hope this is right place for the question I need to ask.

 

I have php 5.2.6 and MySQL 5.0.51b-community-nt.

 

The problem is next: I have an insert statment on my php page and one of columns I have to insert is current date.

 

Type of that column in database is DATE.

 

As I have read, database format of column type DATE is 'YYYY-MM-DD', so, I used php function:

date('Y-m-d') to get proper format, but I am getting next 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 ' '', 2008-11-09)' at line 24".

 

Where is my mistake, can anyone help me please?

 

 

Tnx in advance!

Nati

Link to comment
https://forums.phpfreaks.com/topic/132034-solved-problem-with-inserting-date/
Share on other sites

DATE values are strings and need to be enclosed in single-quotes.

 

You can also use the mysql CURDATE() function directly in your query instead of using some slow parsed/tokenized/interpreted php code to produce the date.

Well, I used single quotes, and I will provide my code so you can see clearly how it looks like:

 

$datum=date('Y-m-d');

$query1 = "insert into registrovani_korisnici

            (id,

            username,

            password,

            ime,

            prezime,

            datum_otvaranja_naloga)

            values

            ('',

            '".$korime."',

            '".md5($loz)."',

            '".$ime."',

            '".$prez."',

              '".$datum."')";

 

And this code produces the error I told about.

 

If I change my code as you suggested:

 

$query1 = "insert into registrovani_korisnici

            (id,

            username,

            password,

            ime,

            prezime,

            datum_otvaranja_naloga)

            values

            ('',

            '".$korime."',

            '".md5($loz)."',

            '".$ime."',

            '".$prez."',

              curdate())";

 

I am again getting 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 ' '', CURDATE())' at line 24"

 

Am I doing something wrong?

 

 

Regards,

Nati

Hey, it is solved!

 

prez is mandatory data, as some other fields, and before this query I had some check of input fields which was skipped because of my typing mistake  :-X.

 

I corrected it and everything works fine now.

 

Tnx a lot,

Nati

And here's mu suggestion for your query. Less quotes => lower possibility of mistake. And 'id' can be ommited altogether, as you're entering an empty value.

$query1 = "INSERT INTO registrovani_korisnici (
             username,
             password,
             ime,
             prezime,
             datum_otvaranja_naloga
             ) VALUES (
             '$korime',
             MD5($loz),
             '$ime',
             '$prez',
             CURDATE())";

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.