i have a codeigniter website where i have simple search box, which is like below:
<form method="post" action="<?php echo base_url()?>homecontroller/search">
<label>Month: </label>
<select class="form-select" id="basicSelect" name="month">
<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>
<label>Year: </label>
<select class="form-select" id="basicSelect" name="year">
<option value="2022">2022</option>
<option value="2021">2021</option>
<option value="2020">2020</option>
<option value="2019">2019</option>
<option value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
</select>
<input style="margin-top:24%" type="submit" class="btn btn-primary ml-1" name="billing" value="SEARCH">
</form>
my controller looks like:
$month=$this->input->post('month');
$year=$this->input->post('year');
$stotal=$year.'-'.$month;
$data['search'] = $this->user->searchbilling($stotal);
and i did the following code in model to fetch results:
public function searchbilling($stotal) {
$this->db->select('*, SUM(quantity) as quantity');
$this->db->from('delivered');
$this->db->like('date', $stotal);
$this->db->group_by('YEAR(date), MONTH(date), customer');
$query = $this->db->get();
$result = $query->result();
return $result; }
however this is noy showing all the results from that particular month, can anyone please tell me whats wrong in here, thanks in advance
You can try like this where("DATE_FORMAT(date,'%Y-%m') =", $stotal)
public function searchbilling($stotal) {
$this->db->select('*, SUM(quantity) as quantity');
$this->db->from('delivered');
$this->db->where("DATE_FORMAT(date,'%Y-%m') =", $stotal);
$this->db->group_by('YEAR(date), MONTH(date), customer');
$query = $this->db->get();
$result = $query->result();
return $result;
}
You can do like this where("DATE_FORMAT(date,'FORMAT YOU NEED') =", $stotal)
public function searchbilling($stotal) {
$this->db->select('*, SUM(quantity) as quantity');
$this->db->from('delivered');
$this->db->where("DATE_FORMAT(date,'FORMAT YOU NEED') =", $stotal);
$this->db->group_by('YEAR(date), MONTH(date), customer');
$query = $this->db->get();
$result = $query->result();
return $result;
}
My assignment is to build a form using php that gets the users first name, last name, street address, city, state, zip code, phone number, quantity of product wanted, and preferred shipping method. I have built the code and validation up to the point of the state; however, the state question is a drop down menu that is supposed to list all the states and have the user select one. When I try this code below that correlates with my state variable my code quits working. I have tried changing my form action to the PHP self post because that seemed to be the most common fix but was unsuccessful with all of my attempts. The if test i have below that posts the state variable was my latest attempt at trying a solution found on this website, so if there is anyway to validate that the user selected a state with the value="0" option that would be great!
<?php
$stateError = "";
if(0 != $_POST['state'])
{
$state = $_POST['state'];
}
else
{
$stateError = "<span class='error'>Enter a valid state from the dropdown menu</span>";
error++;
}
?>
<form action="test.php" method="post">
<p>State:
<select id="statedrop" name="state">
<option value="0" selected>enter state</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">>Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</p>
</form>
To avoid php injections you should keep the possible values for your POST variable as an array. And if something was selected check if the value you got is in the array.
Bad guys will try to manipulate your page otherwise.
<?php
$stateError = "";
$states = array("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL",
"GA","HI","ID","IL","IN","IA","KS","KY","LA","ME",
"MD","MA","MI","MN","MS","MO","MT","NE","NV","NH",
"NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI",
"SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY");
$state_names = array("Alabama","Alaska","Arizona","Arkansas","California",
"Colorado","Connecticut","Delaware","District Of Columbia",
"Florida","Georgia","Hawaii","Idaho","Illinois","Indiana",
"Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts",">Michigan","Minnesota","Mississippi","Missouri",
"Montana","Nebraska","Nevada","New Hampshire","New Jersey",
"New Mexico","New York","North Carolina","North Dakota","Ohio",
"Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina",
"South Dakota","Tennessee","Texas","Utah","Vermont","Virginia",
"Washington","West Virginia","Wisconsin","Wyoming");
// we check if the posted state is in the array states
if(isset($_POST['state']) && in_array($_POST['state'],$states))
{
$state = $_POST['state'];
}
else
{
$stateError = "<span class='error'>Enter a valid state from the dropdown menu</span>";
$error++; // error++ is a typo, it should be $error whatever $error is.
}
echo '<form action="test.php" method="post">';
echo '<p>State:<select id="statedrop" name="state">';
echo '<option>enter state</option>';
foreach ($states as $key => $val){
echo '<option value="'.$val.'" ';
if ($val===$state){
// if state was selected remember the position
echo " selected ";
}
echo '>'.$state_names[$key].'</option>';
}
echo '</select></p></form>';
?>
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']
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm doing a project for my school and I wrote this page but for some reason I get 2 warnings in the end about header info already sent on line 132 but on line 132 all I have is the php tag
I would really appreciate it if I can get some help, I wrote this code in 1 hour but trying to solve this problem for the past 2 hours ...
Finally got tired and decided to ask it
here is my code
<html>
<body>
<form method="POST">
Browse By Author's name starting with :<select name="author">
<option value="">--Select--</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
<option value="J">J</option>
<option value="K">K</option>
<option value="L">L</option>
<option value="M">M</option>
<option value="N">N</option>
<option value="O">O</option>
<option value="P">P</option>
<option value="Q">Q</option>
<option value="R">R</option>
<option value="S">S</option>
<option value="T">T</option>
<option value="U">U</option>
<option value="V">V</option>
<option value="W">W</option>
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option>
</select><br><br>
Browse by Title start with :<select name="title">
<option value="">--Select--</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
<option value="J">J</option>
<option value="K">K</option>
<option value="L">L</option>
<option value="M">M</option>
<option value="N">N</option>
<option value="O">O</option>
<option value="P">P</option>
<option value="Q">Q</option>
<option value="R">R</option>
<option value="S">S</option>
<option value="T">T</option>
<option value="U">U</option>
<option value="V">V</option>
<option value="W">W</option>
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option>
</select><br><br>
Browse by Publisher start with :<select name="publisher">
<option value="">--Select--</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
<option value="J">J</option>
<option value="K">K</option>
<option value="L">L</option>
<option value="M">M</option>
<option value="N">N</option>
<option value="O">O</option>
<option value="P">P</option>
<option value="Q">Q</option>
<option value="R">R</option>
<option value="S">S</option>
<option value="T">T</option>
<option value="U">U</option>
<option value="V">V</option>
<option value="W">W</option>
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option>
</select><br><br>
Browse by Genre:<select name="genre">
<option value="">--Select--</option>
<option value="fiction">Fiction</option>
<option value="non-fiction">non-fiction</option>
<option value="sciencefiction">science fiction</option>
</select><br><br>
Book Award: <select name="bookaward">
<option value="">--Select--</option>
<option value="1">Arthur Ellis Award</option>
<option value="2">Booker Prize</option>
<option value="3">Canadian Jewish Book Awards</option>
<option value="4">Commonwealth Writers Prize</option>
<option value="5">Dayne Ogilvie Prize</option>
<option value="6">Edna Staebler Award</option>
<option value="7">Geoffrey Bilson Award</option>
<option value="8">Gerald Lampert Award</option>
<option value="9">Griffin Poetry Prize</option>
<option value="10">Governor General's Award</option>
<option value="11">Commonwealth Writers Prize</option>
<option value="12">Journey Prize</option>
<option value="13">Lorne Pierce Medal</option>
<option value="14">Wright Awards</option>
<option value="15">Milton Acorn People's Poetry Award</option>
<option value="16">Matt Cohen Award: In Celebration of a Writing Life</option>
<option value="17">Norma Fleck Award</option>
<option value="18">RBC Bronwen Wallace Award for Emerging Writers</option>
<option value="19">Pat Lowther Award</option>
<option value="20">Rogers Writers' Trust Fiction Prize</option>
<option value="21"> Wright Awards</option>
<option value="22">Writers' Trust Distinguished Contribution Award</option>
<option value="23">Writers' Trust Engel/Findley Award</option>
<option value="24">Winterset Award</option>
<option value="25">Hilary Weston Writers' Trust Prize for Nonfiction</option>
<option value="26">Geoffrey Bilson Award</option>
</select><br><br>
<input type="submit" name="submit" value="Browse">
</form>
<?php
error_reporting(E_ERROR|E_WARNING);
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Connection Failed: ' . mysql_error());
}
if (mysql_select_db("elibrary",$con)){
if ($_POST['submit']){
$author = $_POST['author'];
$title = $_POST['title'];
$genre = $_POST['genre'];
$publisher= $_POST['publisher'];
$award= $_POST['bookaward'];
if ($author != "") {
$author1 = "author";
$like1 = "like";
$and1 = "and";
}
if ($title != "") {
$title1= "title";
$like2 = "like";
$and2 = "and";
}
if ($genre != "") {
$genre1="genre";
$like3 = "like";
$and3 = "and";
}
if ($publisher != "") {
$publisher1= "publisher";
$like4 = "like";
$and4 = "and";
}
if ($award != "") {
$award1= "bookawards.id";
$like5 = "=";
$and5 = "and";
}
$browse = ("select * from books,bookawards,bookrelations where books.id = bookrelations.bookid and bookawards.id = bookrelations.bookawardsid '".$and1."' '".$author1."' '".$like1."' '".$author."%' '".$and2."' '".$title1."' '".$like2."' '".$title."%' '".$and3."' '".$genre1."' '".$like3."' '".$genre."%' '".$and4."' '".$publisher1."' '".$like4."' '".$publisher."' '".$and5."' '".$award1."''".$like5."''".$award."'");
setcookie("browse",$browse);
header("Location: shop.php");
exit();
}
}
mysql_close();
?>
</body>
</html>
You need to move your PHP code to top of the page (before HTML).
For more detailed answer:
https://stackoverflow.com/a/8028987/1724762
header() will not work after you have already output so much HTML. It has to be called before any output is sent to browser
According to PHP Manual
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
When you write to the output with echo or any function, PHP has already sent the response body.
Which means that you cannot send headers after.
The same apply for html, the html will be displayed and the output has already started so you cannot call header() after.
The same also apply to closing tags ?>. If you have any single space after the closing tag, it will be ignored by the parser (because outside of the closing tag) and the output will start.
So the best solution is to ommit the closing tags, because the parser doesn't need them.
It's like if you were sending an email and add the address after.
I have this code in html -
<select name="uyear">
<option value="0">YEAR</option>
<option value="1980">1980</option>
<option value="1981">1981</option>
<option value="1982">1982</option>
<option value="1983">1983</option>
<option value="1984">1984</option>
<option value="1985">1985</option>
<option value="1986">1986</option>
<option value="1987">1987</option>
<option value="1988">1988</option>
<option value="1989">1989</option>
<option value="1990">1990</option>
<option value="1991">1991</option>
<option value="1992">1992</option>
<option value="1993">1993</option>
<option value="1994">1994</option>
<option value="1995">1995</option>
<option value="1996">1996</option>
<option value="1997">1997</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="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
</select>
Now i have certain values stored in DB i have to fetch the value from the DB and then show the stored year as selected BUT i cannot write this year drop-down list in php. Now how can i show selected value in the above drop-down WITHOUT writing it in php code??
EDIT : its just one drop down that i have shown here..i have huge numbers of drop down written in html and if i go to change them in php then it will take 3-4 days as there r n numbers of drop down on this page. Thats why i need a solution with which i neednot change the select tag ino php code ans still can show selected as per DB value.
Have PHP write at another place:
<body onload="document.getElementsByName('uyear')[0].selectedIndex=$myDearIndex">
If PHP is out, you'll have to do something in JavaScript... and you may need to do a value-search to select the correct index. I submit that it may be as much of a pain doing a function and then scriptlet for each select as it would be just re-writing your select output loops in PHP.
Untested:
<body onload="setDefaultSelections()">
<script>
function setDefaultSelection(selectName, default) {
var select = document.getElementsByName(selectName)[0];
for (i = 0; i < select.length; i++) {
if (select[i].value == default) {
select.selectedIndex = i;
break;
}
}
}
function setDefaultSelections() {
setDefaultSelection('uyear', '<? $uyear ?>');
setDefaultSelection('ucountry', '<? $country ?>');
...
}
</script>