how to select where month in mysql and php? - php

How to select where month in mysql and php ?
SELECT * FROM `myTable` WHERE insert_date=MONTH($_POST['month']);
Type insert_date = datetime
here code for input post month:
<select name="month">
<option value="" selected>Choose Month :</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">Maret</option>
<option value="4">April</option>
<option value="5">Mei</option>
<option value="6">Juny</option>
<option value="7">July</option>
<option value="8">Augustus</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">Desember</option>
</select>

Query should be like this
SELECT * FROM myTable WHERE MONTH(insert_date)=$_POST['month'];

Do not use $_POST directly. At least use (int) $_POST['month']
If insert_date is a date or timestamp field, use WHERE MONTH(insert_date) = :month with :month bind as (int) $_POST['month']

Related

Keeping lead Zero in MySQL

I have a table with a varchar column. I use a select form with values 01, 02, 03, 04 .... 10, 11, 12 etc to store data in this column. My problem is that, when I submit the form, the leading 'Zero' is removed. How can I maintain the leading zero?
**
NB. I have checked other similar posts but they don't have the
solution for my issue. I have tried var/char/text and still it's not
working hence why I posted this question if there's another way to get this done.
**
<form method=post enctype="multipart/form-data">
<select name="dd">
<option selected="selected">Day</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
</select>
<select name="mm">
<option selected="selected">Month</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
</select>
</form>
MySQL is as follows;
$caption = $_POST['dd'].-$_POST['mm'];
$sql = "INSERT INTO pages SET caption = '$caption'";
Use sprintf to format your value:
$caption = sprintf("%02d-%02d", $_POST['dd'], $_POST['mm']);
The key here is to work with the data as strings and don't do math on it like -$_POST['mm'] which will treat it as a number. In numbers leading zeros are always omitted because they always exist. 10 is actually something like 0x0000000a as far as the CPU is concerned.

Codeigniter: Fetch the value from an option in the dropdown list and check SQL condition

I am having an issue fetching the values from a dropdown list to check a condition with the mysql query. It is using codeigniter. Let me post the code I have included below:
VIEW:
<select name="month" id="month">
<option>Select Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
The above values should be equal to the month in the date column of the table. I will post my model below:
MODEL:
$month = $this->input->post('month');
$this->datatables->select('order_no,order_date,naturescrm_customers.name,total,naturescrm_users.username,naturescrm_order.id,
naturescrm_order_status.status_name')
->unset_column('naturescrm_order.id')
->add_column('Actions', btn_edit('agent/order/edit/' . '$1').' '.get_view('$1','agent/order/view/') ,'naturescrm_order.id')
->from($this->_table_name)
->join("naturescrm_customers","naturescrm_customers.id=naturescrm_order.customer_id","left")
->join("naturescrm_order_status","naturescrm_order_status.status_id=naturescrm_order.order_status","left")
->join("naturescrm_users","naturescrm_users.id=naturescrm_order.user_id","left")
->where("DATE_FORMAT(order_date, '%m') = $month");
$this->db->order_by("order_date","desc");
There is my code in the model and in the view, So when I Select a month in the dropdown, the values being fetched in the database should reflect based on the month. Any help will be much appreciated. Thanks

Getting rid of unwanted objects in a drop down list created by php and mysqli

I've created a piece of code that dynamically create objects within the select table.
These objects are years, however because of the mysql database there are multiple of the same year and therefore adding in unnecessary data when retrieved through php.
CODE:
$searchYearSQLI = "SELECT bookYear FROM nbc_book ORDER BY bookYear";
$querySearchYear = mysqli_query($dbCon, $searchYearSQLI) or die(mysqli_error($dbCon));
while($searchYear = mysqli_fetch_array($querySearchYear)){
$displayYear = $searchYear['bookYear'];
echo "<option value=\"$displayYear\">$displayYear<option>\n";
}
OUTPUT:
<option value="1998">1998</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2002">2002</option>
<option value="2002">2002</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2004">2004</option>
<option value="2004">2004</option>
<option value="2004">2004</option>
<option value="2004">2004</option>
<option value="2004">2004</option>
<option value="2004">2004</option>
Is it possible to reduce these duplicate outputs and still have them point to the correct data within the database?
Just use DISTINCT function
$searchYearSQLI = "SELECT DISTINCT(bookYear) FROM nbc_book ORDER BY bookYear";
Change to:
$searchYearSQLI = "SELECT DISTINCT bookYear FROM nbc_book ORDER BY bookYear";

Laravel Creating A Custom Form Macro Select Year and Month

I need help building my laravel custom macro. In my start/global.php I required a macros.php
...
require app_path().'/macros.php';
and on my macros.php I have this code that I read in this site.
Form::macro('selectMonths', function()
{
$selectMonth = preg_replace('/></', '><option value="">Month</option><', Form::selectMonth('month'));
return $selectMonth;
});
and the result is this.
<select class="form-control" name="month">
<option value="">Month</option>
<option value="1">January</option>
<option value="">Month</option>
<option value="2">February</option>
<option value="">Month</option>
<option value="3">March</option>
<option value="">Month</option>
<option value="4">April</option>
<option value="">Month</option>
<option value="5">May</option>
<option value="">Month</option>
<option value="6">June</option>
<option value="">Month</option>
<option value="7">July</option>
<option value="">Month</option>
<option value="8">August</option>
<option value="">Month</option>
<option value="9">September</option>
<option value="">Month</option>
<option value="10">October</option>
<option value="">Month</option>
<option value="11">November</option>
<option value="">Month</option>
<option value="12">December</option>
<option value="">Month</option>
</select>
how can I add this Month before January and not after every month?
Thanks!
Try using this pattern:
/name="month">/
Code:
$selectMonth = preg_replace('/name="month">/', 'name="month"><option value="">Month</option>', Form::selectMonth('month'));
This should insert it only at the very top of your list.
Try this code:
Form::macro('selectMonths', function($name, $selected = null, $options = array())
{
$selectMonth = preg_replace('/></', '><option value="">Month</option><', Form::selectMonth($name, $selected, $options), 1);
return $selectMonth;
});
Form::macro('selectYears', function($name, $begin, $end, $selected = null, $options = array())
{
$selectYear = preg_replace('/></', '><option value="">Year</option><', Form::selectYear($name, $begin, $end, $selected, $options), 1);
return $selectYear;
});
Are you just trying to get a list of years? If so, you don't need a custom Macro. Laravel has you covered:
{{ Form::selectYear('year', 2013, 2015) }}
And for the month:
{{ Form::selectMonth('month') }}

Convert HTML form input into date php understands

I'm looking for a way to have people visiting my website enter a date through a HTML form and then use php to subtract or add time from that date they entered in. For example, in the form they will enter their Birthday and I want php to echo 5 days after their birthday. I've looked into many ways to add dates in php, so I don't think that will be a problem. I just can't figure out how to get php to recognize form inputs as a date.
Here is my HTML code to select the date:
<select name="DMonth" input type="number">
<option>- Select Month -</option>
<option value=1>January</option>
<option value=2>February</option>
...
<option value=12>December</option>
</select>
<select name="DDay" input type="number">
<option>- Select Day -</option>
<option value=1>1</option>
<option value=2>2</option>
...
<option value=31>31</option>
</select>
<select name="DYear" input type="number">
<option>- Select Year -</option>
<option value=2013>2013</option>
<option value=2014>2014</option>
...
<option value=2022>2022</option>
</select><br>
<select name="DHour" input type="number">
<option>- Select Hour -</option>
<option value=00>12 a.m.</option>
<option value=01>1 a.m.</option>
<option value=02>2 a.m.</option>
<option value=03>3 a.m.</option>
<option value=04>4 a.m.</option>
<option value=05>5 a.m.</option>
<option value=06>6 a.m.</option>
<option value=07>7 a.m.</option>
<option value=08>8 a.m.</option>
<option value=09>9 a.m.</option>
<option value=10>10 a.m.</option>
<option value=11>11 a.m.</option>
<option value=12>12 p.m.</option>
<option value=13>1 p.m.</option>
<option value=14>2 p.m.</option>
<option value=15>3 p.m.</option>
<option value=16>4 p.m.</option>
<option value=17>5 p.m.</option>
<option value=18>6 p.m.</option>
<option value=19>7 p.m.</option>
<option value=20>8 p.m.</option>
<option value=21>9 p.m.</option>
<option value=22>10 p.m.</option>
<option value=23>11 p.m.</option>
</select>
<select name="DMin" input type="number">
<option>- Select Minute -</option>
<option value=0>00</option>
<option value=15>15</option>
<option value=30>30</option>
<option value=45>45</option>
</select>
and this is what I thought I would use in php
$DMonth=$_POST["DMonth"];
$DDay=$_POST["DDAY"];
$DYear=$_POST["DYear"];
$DHour=$_POST["DHour"];
$DMin=$_POST["DMin"];
$Bday = date_create($DYear.'-'.$DMonth.'-'.$DDay.' '.$DHour.':'.$DMin.':00');
echo date_format($Bday, 'Y-m-d H:i:s');
but when I run the form, the only thing it echos is the current date and time.
I don't see your code for hours and minutes
This is a bit convoluted since it is an addon to your code but will do the trick.
$DMonth=$_POST["DMonth"];
$DDay=$_POST["DDAY"];
$DYear=$_POST["DYear"];
$DHour=$_POST["DHour"];
$DMin=$_POST["DMin"];
$Bday = date_create($DYear.'-'.$DMonth.'-'.$DDay.' '.$DHour.':'.$DMin.':00');
echo date_format($Bday, 'Y-m-d H:i:s');
$datetime = new DateTime(date_format($Bday, 'Y-m-d H:i:s'));
$datetime->modify('+5 days');
echo $datetime->format('Y-m-d H:i:s');
Simpler version
$datetime = DateTime::createFromFormat('Y-n-j H:i', $DYear.'-'.$DMonth.'-'.$DDay.' '.$DHour.':'.$DMin);
echo $datetime->format('Y-m-d H:i:s'); // Birthday
$datetime->modify('+5 days');
echo $datetime->format('Y-m-d H:i:s'); // 5 days later
How about...
$datetime= mktime($DHour, $DMin, 0, $DMonth, $DDay, $DYear);
?
See the mktime help page for more information - no need to build strings or worry about formats, just provide the appropriate parameters name Hour, Minute, Second, Month, Day, Year
There are also a couple of problems in your html. The syntax for a select is as follows:
<select name="DYear">
<option>- Select Year -</option>
<option value="2013">2013</option>
Note that the <select> has no input and that values are enclosed in quotes. There's also no need to specify type="number". To correctly poarse the values as integers instead of strings, do this in your PHP:
$DYear = (int) $_POST["DYear"];

Categories