Reputation: 661
I have created a form that sends data to a database, all fields are working except date, i assume it is to do with the format but i am unsure how to go about it?
HTML
<dt>Date of Purchase</dt>
<dd>
<select id="date_of_purchase" name="entry[date_of_purchase]">
<option value="">Month</option>
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="Mar">Mar</option>
</select>
<select id="date_of_purchase" name="entry[date_of_purchase]">
<option value="">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id="date_of_purchase" name="entry[date_of_purchase]">
<option value="">Year</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
</dd>
PHP
$stmt = DB::query(Database::INSERT, 'INSERT INTO `registration` (`first_name`, `last_name`, `date_of_purchase`) VALUES (:first_name, :last_name, :date_of_purchase)');
$stmt->param(':first_name', $post['first_name']);
$stmt->param(':last_name', $post['last_name']);
$stmt->param(':date_of_purchase', $post['date_of_purchase']);
Upvotes: 0
Views: 360
Reputation: 3713
One way to do it could be :
//in PHP
//first : convert and valid the INPUT
try{
$date = DateTime.CreateFromFormat("d-m-Y",$post["day"]."-".$post["month"]."-".$post['year']);
//then you will convert it to MySQL format :
$stmt = DB::query(Database::INSERT, 'INSERT INTO `registration` (`first_name`, `last_name`, `date_of_purchase`) VALUES (:first_name, :last_name, :date_of_purchase)');
$stmt->param(':first_name', $post['first_name']);
$stmt->param(':last_name', $post['last_name']);
$stmt->param(':date_of_purchase', $date->format("Y-m-d h:i:s");
}catch(Exception $e){
//here re-ask the date because it is false
}
Upvotes: 0
Reputation: 1993
you should use different names for fields
<dt>Date of Purchase</dt>
<dd>
<select id="month_of_purchase" name="month_of_purchase">
<option value="">Month</option>
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="Mar">Mar</option>
</select>
<select id="day_of_purchase" name="day_of_purchase">
<option value="">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id="year_of_purchase" name="year_of_purchase">
<option value="">Year</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
</dd>
and then construct date as
$stmt->param(':date_of_purchase', sprintf("%d-%d-%d", $post['day_of_purchase'], $post['month_of_purchase'], $post['year_of_purchase']));
(i assume that $post['year_of_purchase'] is the same as $_POST['year_of_purchase'])
Upvotes: 2
Reputation: 806
Change your code to something like this:
HTML
<dt>Date of Purchase</dt>
<dd>
<select id="date_of_purchase" name="entry[month]">
<option value="">Month</option>
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="Mar">Mar</option>
</select>
<select id="date_of_purchase" name="entry[day]">
<option value="">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id="date_of_purchase" name="entry[year]">
<option value="">Year</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
</dd>
PHP
$stmt = DB::query(Database::INSERT, 'INSERT INTO `registration` (`first_name`, `last_name`, `date_of_purchase`) VALUES (:first_name, :last_name, :date_of_purchase)');
$stmt->param(':first_name', $post['first_name']);
$stmt->param(':last_name', $post['last_name']);
$stmt->param(':date_of_purchase', $post['month'] . $post['day'] . $post['year']); // Just an example, I am pretty sure this won't work, change it according to your database
Upvotes: 0