0

I currently have a form that asks for each of these in separate dropdowns.

Day | Month | Year

When I am inserting into the database, I perform the following but all it inserts is 00-00-0000.

$_POST['date'] = $_POST['dob_day'].'-'.$_POST['dob_month'].'-'.$_POST['dob_year'];

//I then do my insert and I'm inserting this..

mysql_real_escape_string($_POST['date'])

What seems to be the issue?

Dave
  • 691
  • 2
  • 11
  • 25
  • Do you get actual numbers/number-strings returned from the web-page? – David Thomas Oct 25 '12 at 20:57
  • Your code is vurnerable to SQL injections. Please fix that problem first. See [here](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) – juergen d Oct 25 '12 at 20:57
  • I honestly didn't check but every other bit of data is coming across and storing just fine into the db so I would assume it is.. – Dave Oct 25 '12 at 20:57
  • I'm using `mysql_real_escape_string` though? – Dave Oct 25 '12 at 20:59

3 Answers3

2
  1. Don't assign anything to $_POST, that's super bad practice.
  2. Dates in mySQL should be formatted YYYY-MM-DD.

$date = $_POST['dob_year'] . '-' . $_POST['dob_month'] . '-' . $_POST['dob_day'];

Sammitch
  • 30,782
  • 7
  • 50
  • 77
0

you're assigning the data to a POST which isn't correct.

$date = $_POST['dob_day'].'-'.$_POST['dob_month'].'-'.$_POST['dob_year'];

then insert $date

David Nguyen
  • 8,368
  • 2
  • 33
  • 49
  • No problem assigning to POST, you still can use it as usual array, as far as I know. – dfsq Oct 25 '12 at 20:59
  • Thanks David Nguyen - just that the MySQL format is yyyy-mm-dd which I completely forgot! Thank you – Dave Oct 25 '12 at 21:05
0

If your values are empty after this, you are not using POST correctly. I changed it up a little bit to make it more readable:

$day = $_POST['dob_day'];
$month = $_POST['dob_month'];
$year = $_POST['dob_year'];

$date = $day . '/' . $month . '/' . $year;
$insert = "INSERT INTO table('dob') VALUES('$date')";
// then run $insert
burmat
  • 2,548
  • 1
  • 23
  • 28