SELECT multiple WHERE in same column - php

I want choose month and year from the same column. How can I separate month and year from the column?
<label>Bulan :</label>
<select name="month">
<option value=""></option>
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mac</option>
<!-- ... -->
</select>
<label>Tahun :</label>
<select name="year">
<option value=""></option>
<option value="2017">2017</option>
<option value="2016">2016</option>
</select>
<input type="submit" value="Hantar" >
<?php
if(isset($_POST['year']) && ($_POST['month']))
{
$tarikh = mysql_real_escape_string($_POST['month']);
$tarikh = mysql_real_escape_string($_POST['year']);
I am running this query
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikh')
and (YEAR(tarikh) = '$tarikh')";
Why doesn't the output display? What is wrong with the above query?

in query string you use same variable for month and year
add different variable names in
$tarikhMonth = mysql_real_escape_string($_POST['month']);
$tarikhYear = mysql_real_escape_string($_POST['year']);
and
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikhMonth')
and (YEAR(tarikh) = '$tarikhYear')";

You are overwriting your $tarikh variable. Naming them $month and $year should work.
I would use PDO instead of trying to escape strings manually as well.

You need to have different variable names for the month and year. Try this
<label>Bulan :</label> <select name="month">
<option value=""></option>
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mac</option>...
</select><label>Tahun :</label> <select name="year">
<option value=""></option>
<option value="2017">2017</option>
<option value="2016">2016</option>
</select><input type="submit" value="Hantar" ></td>
<?php
if(isset($_POST['year']) && ($_POST['month']))
{
$tarikh_month = mysql_real_escape_string($_POST['month']);
$tarikh_year = mysql_real_escape_string($_POST['year']);
$query ="SELECT * FROM pelanggan
WHERE (MONTH(tarikh) = '$tarikh_month')
and (YEAR(tarikh) = '$tarikh_year')";

Related

How can I achieve years on drop down menu

Hello everyone one I want to get this kind of drop down menu which has years need to be selected
For example season 2021/2022, 2020/2021, all these value should be generated from 1990 to 2022
If I understood correctly, you want to populate the select with options with all seasons until now since a specific year?
You could modify a for loop so the years appears in a descending order, and then output the option tag with the "previous year / year" as a value like this:
<div class="season-select-wrapper">
<?php
// Define variables for the select
$currentYear = 2022;
$startYear = 1990;
?>
<label for="season">Select a season</label>
<select name="season" id="season">
<?php for ($year = $currentYear; $year > $startYear; $year--) { $prevYear = $year - 1; ?>
<option value="<?php echo "{$prevYear}/{$year}"; ?>"><?php echo "{$prevYear}/{$year}"; ?></option>
<?php } ?>
</select>
</div>
This would create HTML markup like this:
<div class="season-select-wrapper">
<label for="season">Select a season</label>
<select name="season" id="season">
<option value="2021/2022">2021/2022</option>
<option value="2020/2021">2020/2021</option>
<option value="2019/2020">2019/2020</option>
<option value="2018/2019">2018/2019</option>
<option value="2017/2018">2017/2018</option>
<option value="2016/2017">2016/2017</option>
<option value="2015/2016">2015/2016</option>
<option value="2014/2015">2014/2015</option>
<option value="2013/2014">2013/2014</option>
<option value="2012/2013">2012/2013</option>
<option value="2011/2012">2011/2012</option>
<option value="2010/2011">2010/2011</option>
<option value="2009/2010">2009/2010</option>
<option value="2008/2009">2008/2009</option>
<option value="2007/2008">2007/2008</option>
<option value="2006/2007">2006/2007</option>
<option value="2005/2006">2005/2006</option>
<option value="2004/2005">2004/2005</option>
<option value="2003/2004">2003/2004</option>
<option value="2002/2003">2002/2003</option>
<option value="2001/2002">2001/2002</option>
<option value="2000/2001">2000/2001</option>
<option value="1999/2000">1999/2000</option>
<option value="1998/1999">1998/1999</option>
<option value="1997/1998">1997/1998</option>
<option value="1996/1997">1996/1997</option>
<option value="1995/1996">1995/1996</option>
<option value="1994/1995">1994/1995</option>
<option value="1993/1994">1993/1994</option>
<option value="1992/1993">1992/1993</option>
<option value="1991/1992">1991/1992</option>
<option value="1990/1991">1990/1991</option>
</select>
</div>

Filter table using month and year (PHP MYSQL)

im supposed to make a search function that allowed the users to filter their table using month and year..
this is the interface..
user should be able to search the 'dateissued' by selecting the month and year. but i have no idea of how to do this.
<div class="col-sm-3">
<select class="form-control" name="month">
<option>Select Month</option>
<option value="1">January</option>
<option value="2">February</option>
</select>
</div>
<div class="col-sm-3">
<select class="form-control" name="yearManufactured" required>
<option>Select Year</option>
<?php
foreach(range(1950, (int)date("Y")) as $year) {
echo "\t<option value='".$year."'>".$year."</option>\n\r";
}
?>
</select>
</div>
can someone guide me? or gave any links to related example or tutorials..
You can query like this, $month and $year is the $_REQUEST value:
$month = strlen(trim($month)) == 1 ? "0".$month : $month;
$searchDate = $year."-".$month;
SELECT * FROM $table WHERE dateissued like '$searchDate%'
Hope this helps :)
you can add month like 01,02,03... in php and use extract in mysql
<select class="form-control" name="month">
<option>Select Month</option>
<option value="01">January</option>
<option value="02">February</option>
.......
</select>
<select class="form-control" name="yearManufactured" required>
<option>Select Year</option>
<?php
foreach(range(1950, (int)date("Y")) as $year) {
echo "<option value='".$year."'>".$year."</option>\n\r";
}
?>
</select>
PHP :
$where =' where 1=1';
if(isset($_POST['month']) && !empty($_POST['month']))
{
$where.=" and EXTRACT(MONTH from dateIssued)='".$_POST['month']."'";
}
if(isset($_POST['yearManufactured']) && !empty($_POST['yearManufactured']))
{
$where.=" and EXTRACT(YEAR from dateIssued)='".$_POST['yearManufactured']."'";
}
$query ="select * from table_name $where";

how to display jquery/ajax data into select field?

code:
<script>
$(document).ready(function(){
$(".field").change(function(){
field = $(".field").val();
$.ajax({
type:"POST",
data:{"field":field},
url:"potential-courses.php",
success:function(data){
$(".course").val(data);
}
});
});
});
</script>
potential-courses.php
<?php
include("conn.php");
$field = $_POST['field'];
$sql = "select * from course_master where field = '$field' order by course_full_name";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['course_short_name'].">".$row['course_full_name']."</option>";
}
?>
html code:
<select name='field' class='field' id="field">
<option value="">Select Field</option>
<option value='engineering'>Engineering</option>
<option value='law'>LAW</option>
<option value='medical'>Medical</option>
<option value='management'>Management</option>
<option value='pharmacy'>Pharmacy</option>
<option value='hotel management'>Hotel Management</option>
<option value='mass communication'>Mass Communication</option>
<option value='agriculture'>Agriculture</option>
<option value='architecture'>Architecture</option>
<option value='education'>Education</option>
<option value='paramedical'>Paramedical</option>
<option value='design'>Design</option>
<option value='commerce'>Commerce</option>
<option value='film/tV/media'>Film /TV/ Media</option>
</select>
<select name="course" class="course">
<option value="">Select Courses</option>
</select>
In this code I have two dropdown list i.e
<select name='field' class='field' id="field">
and another is
<select name="course" class="course">
when I change value from "name=field" it display nothing in "name=course". where I am doing wrong please help me.
Thank You
Change it:
$(".course").val(data);
to
$(".course").html(data);
It will add the <option> set that you have returned from php to your <select>

Select input is empty

People can select their graduation year from a selection input. I email their selection to myself. However, based on the fact that nothing is printing to the console, the $_POST["year"] must be empty, which is why no year is actually being sent to me as shown in the pic below. How do I fix this?
<div class = "mailInput"> Year:
<div class = "select" name = "year"><select>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
</div>
// Assign year input to year variable
if (!empty($_POST["year"])) {
$year = $_POST["year"];
echo("<script>console.log('Year after: ".$year."');</script>");
}
You have written wrong html code,
<div class="mailInput"> Year:
<div ><select class="select" name="year"> // this line
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
</div>
You have given name to div element which was parent of select,
now above I gave name to select element.
Give it a try. It will work.
<div class = "mailInput"> Year:
<div class = "select" >
//give name for select tag not for div
//
<select name = "year">
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
</div>
// Assign year input to year variable
if (!empty($_POST["year"])) {
$year = $_POST["year"];
echo("<script>console.log('Year after: ".$year."');</script>");
}

How to use index of loop in Post method PHP

I need your help! I am trying to save a variable in sql table using Php but I have problem. There are two questions in php, the first concern the continent and the second is depended from the continent. I want to use a loop to check which of the continents has been selected in the first question and then save the value of the second question. I hide the option of unchecked continent using some javascript code (I don't have problem).
The HTML code:
<form method="post" action="">
<fieldset><legend>Continents</legend>
<select id="q1" name="q1">
<option value="1">Africa</option>
<option value="2">Asia</option>
<option value="3">Australia</option>
<option value="4">America</option>
<option value="5">Europe</option>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
The Php code
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
for ($i = 1; $i <= 5; $i++) {
if($q1 == $i) {
$q2 = $_POST[$continents[$i-1]]
}
}
Your array should be from 1 to 5 instead of 0 to 4 as you have values 1 to 5 in q1.
Alternatively, I suggest that change your HTML structure to get the continent value in a single line without using loop. You need to change the values of continent like,
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
And to get the value of selected continent use $_POST[$_POST['q1']]. For egs, $_POST['q1']=Asia, then $_POST['Asia'] will return the Asia's choice,
$q2= $_POST[$_POST['q1']];
change
$continents =
array(1 => "Africa",
2 => "Asia",
3 => "Australia",
4 => "America",
5 => "Europe");
or
if(($q1-1) == $i) {
$q2 = $_POST[$continents[$i]]
}
First, you may change your select continent HTML:
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
Then you could loop over your continents and get the answer:
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
foreach($continents as $continent) {
if ($_POST['q1'] == $continent) {
$q2 = $_POST[$continent];
}
}
Now you have your answer to the second question in $q2.
How about this?
// Always try to seperate logic from your view
// Intialize data
$continents = array("Africa", "Asia", "Australia", "America", "Europe");
// Check for $_POST
if(isset($_POST["q1"])) {
foreach($continents as $id => $continent) {
if($_POST["q1"] == $id) {
// Do something special here
}
}
}
// Render HTML
<form method="post" action="">
<fieldset>
<legend>Continents</legend>
<select id="q1" name="q1">
<!-- Notice that I echo only variables not all of the html -->
<?php foreach($continents as $id => $continent) { ?>
<option value="<?php echo $id; ?>"><?php echo $continent; ?></option>
<?php } ?>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
Beautiful thing about my solution is that now you can create whatever array you want and your code will always work.
You can for example SELECT data from database and store them in $continents variable.
$query = "SELECT id, name FROM continents";
$result = mysql_query($query);
$continents = array();
while($row = mysql_fetch_assoc($result)) {
$continents[$row["id"]] = $row["name"];
}
Cool right? :)
I found the solution finally!!!
Replace
$q2 = $_POST[$continents[$i-1]]
with
$q2 = $_POST["{$continents[$i-1]}"];
That solution I wanted!!

Categories