nati Posted November 9, 2008 Share Posted November 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132034-solved-problem-with-inserting-date/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132034-solved-problem-with-inserting-date/#findComment-686104 Share on other sites More sharing options...
nati Posted November 9, 2008 Author Share Posted November 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132034-solved-problem-with-inserting-date/#findComment-686114 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2008 Share Posted November 9, 2008 Echo $query1 to see what it actually contains. It would appear that $prez is empty, which should not be a problem if it is defined as a string data type. Quote Link to comment https://forums.phpfreaks.com/topic/132034-solved-problem-with-inserting-date/#findComment-686126 Share on other sites More sharing options...
nati Posted November 9, 2008 Author Share Posted November 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132034-solved-problem-with-inserting-date/#findComment-686144 Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 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())"; Quote Link to comment https://forums.phpfreaks.com/topic/132034-solved-problem-with-inserting-date/#findComment-686162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.