php mysql not updating row in proper way - php

I have a column which name is possession1, in this column I concat the $month.$year, but problem is when I update only $month then $year value is laps but when I update only $year then $month do not laps but $year update the column with old value and new value like,If month is May and old year is 2013, and when I update only year with 2014 then its showing output May20132014, this is my code
<select id="month" name="month" >
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
</select>
<select id="year" name="year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
$month=#$_POST['month'];
$year=#$_POST['year'];
$arr = array($month, $year);
$possession1 = join("", $arr);
$updateSale="update sale set possession='$possession1' where id='$update_id'";
$r=mysql_query($updateSale);

Try this, Use isset to check the post value,
if(isset($_POST['month']) && isset($_POST['year'])){
$month = $_POST['month'];
$year = $_POST['year'];
$possession1 = $month. " ".$year;
$updateSale="update sale set possession='".$possession1."' where id='$update_id'";
$r=mysql_query($updateSale);
}

Correct your query to this--
$updateSale="update sale set possession='".$possession1."' where id='".$update_id."'";

Related

SELECT multiple WHERE in same column

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')";

how to select where month in mysql and 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']

Create dynamic <select> with PHP

I have a $_SESSION variable $_SESSION['invoiceDate'] that is populated with a value representing YYYYMM.
I need to dynamically build a select element so that I have 6 options going back from the 'invoiceDate' e.g. if $_SESSION['invoiceDate'] = 201602 the output should be...
<select id="invoiceDate" name="invoiceDate">
<option value="201602">Feb 2016</option>
<option value="201601">Jan 2016</option>
<option value="201512">Dec 2015</option>
<option value="201511">Nov 2015</option>
<option value="201510">Oct 2015</option>
<option value="201509">Sep 2015</option>
</select>
My PHP is very basic... I can split the session value and assign the first 4 digits to $year and the other 2 to $month. Then loop 6 times... Each time I echo to output the <option> line concatenating the year and month to make the value and then subtracting 1 from the month and if month=0 then take 1 from year and make month 12. This gives me....
<option value="201602"></option>
<option value="201601"></option>
<option value="201512"></option>
<option value="201511"></option>
<option value="201510"></option>
<option value="201509"></option>
Now I can join the year and month with a hyphen to give
<option value="201602">2016-02</option>
<option value="201601">2016-01</option>
<option value="201512">2015-12</option>
<option value="201511">2015-11</option>
<option value="201510">2015-10</option>
<option value="201509">2015-09</option>
But how do I get a Month Year instead... e.g. Feb 2016 instead of 2016-02?
Why aren't you using DateTime?
$e = '201602';
$date = new DateTime($e);
echo $date->format('M');
outputs Feb
In your case it could be
$e = '201602';
$date = new DateTime($e);
$month = $date->format('n');
for($i=0;$i<6;$i++)
{
echo '<option value="'.$date->format('Ym').'">'.$date->format('M Y').'</option>';
$date = $date->sub(new DateInterval('P1M'));
}
This outputs
<option value="201602">Feb 2016</option>
<option value="201601">Jan 2016</option>
<option value="201512">Dec 2015</option>
<option value="201511">Nov 2015</option>
<option value="201510">Oct 2015</option>
<option value="201509">Sep 2015</option>
Just edit this as you need and don't use that infernal version of date with strtotime...
$date = "201602";
for($i=1;$i<=6;$i++){
echo date('M, Y',strtotime(date('Y-m-d',strtotime($date))." -$i month"));
}
In this loop we are deleting 1 month from your date.
Date Parameters
This is it:
<?php $session_val = "201602";?>
<select id="invoiceDate" name="invoiceDate">
<?php
for($i = 0; $i < 6; $i++){
//$text = date('Y-m', strtotime(date('Y-m-d', strtotime($session_val))." -$i month")); // for 2016-02
$text = date('M Y', strtotime(date('Y-m-d', strtotime($session_val))." -$i month")); //for Feb 2016
$val = date('Ym', strtotime(date('Y-m-d', strtotime($session_val))." -$i month"));
echo '<option value="'.$val.'">'.$text.'</option>';
}?>
</select>
I did this in my local, but when i am going to post i see some one
post similar thing. But I did so i post.

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"];

Get date, explode, use data to select options in <select>

I have three variables set, $date_legible (in the format of F jS, Y, i.e. January 1st, 2011), $date_timestamp (self-explanatory), and $date_normal (in the format of YYYY-MM-DD)
I have three select fields:
<select name="date_mo">
<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>
<select name="date_dy">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="date_yr">
<?php for($year=$curryear;$year<$curryear+51;$year++){ ?>
<option value="<?php echo $year; ?>"><?php echo $year; ?></option>
<?php } ?>
</select>
I know I want to convert the timestamp, but I will ultimately need to explode the output for the $month, $day, and $year. The problem is I don't know how to check the selects to the values and automatically select the correct option.
Can anyone help? Thanks!
UPDATE
I now have one more variable, because I'm having users submit from the front end, and Gravity Forms submits as YYYY-MM-DD.
Prior to diving into the solutions already posted (thank you guys [and gals] by the way), here's a little more information:
I'm running WordPress and have three custom fields as described above. When a user submits a post from the front end, the only custom field that's filled out is $date_normal. This is okay, because a post has to be moderated (i.e. published) from the backend. What I NOW need is on load to set the YYYY-MM-DD to set the options on the select. I'm not asking you guys to alter your answers, I'm sure I can figure it out (though if you can help with this new problem, it would be greatly appreciated).
If I understand your question correctly, you have a timestamp and you need to use the timestamp to select the appropriate select options when you render the page.
As a forewarning - this will make it possible to select invalid dates, for example February 31st (does not exist). You may want to consider re-drawing the year drop-down when the month is changed using javascript. Since you didn't specify any javascript library (and it is not clear how important the possibility of an invalid date is), I will not venture to show code for that, but it would not be terribly difficult.
// using the current time for the sake of the example, your timestamp would take the place of this
$timestamp = time();
// determine the selected month, day, and year
$selected_month = date('n', $timestamp);
$selected_day = date('j', $timestamp);
$selected_year = date('Y', $timestamp);
// now, create the drop-down for months
$month_html = '<select name="date_mo">';
for ($x = 1; $x < 13; $x++) {
$month_html .= '<option value='.$x.($selected_month == $x ? ' selected=true' : '' ).'>'.date("F", mktime(0, 0, 0, $x, 1, $selected_year)).'</option>';
}
$month_html .= '</select>';
// output
print $month_html;
// create the day drop-down
$day_html = '<select name="date_day">';
for ($x = 1; $x < 32; $x++) {
$day_html .= '<option value='.$x.($selected_day == $x ? ' selected=true' : '' ).'>'.$x.'</option>';
}
$day_html .= '</select>';
// output
print $day_html;
// create the year drop-down
$year_html = '<select name="date_year">';
$start_year = date('Y', time());
$max_year = $start_year + 51;
for ($x = $start_year; $x < $max_year; $x++) {
$year_html .= '<option value='.$x.($selected_year == $x ? ' selected=true' : '' ).'>'.$x.'</option>';
}
$year_html .= '</select>';
// output
print $year_html;
Output:
EDIT
If your source data will be in the format of YYYY-MM-DD. In order to translate that into a timestamp, and thus work with the example code I've posted, is to call strtotime on that formatted date value. Thus, the only line in the code above that would need to be changed is $timestamp = strtotime($the_formatted_date);
<?php
// $months = array(1 => 'January', 2 => 'February', ... etc
?>
<select name="date_mo">
<? for ($i = 1, $current = (int) date('n'); $i <= 12; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$months[$i];?></option>
<? } ?>
</select>
<select name="date_dy">
<? for ($i = 1, $current = (int) date('j'); $i <= 31; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$i;?></option>
<? } ?>
<select name="date_yr">
<? for ($i = $current = (int) date('Y'), $until = $i + 50; $i <= $until; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$i;?></option>
<? } ?>
</select>
I would personally convert the 3 fields to a more user-friendly date selector (using for example jQuery UI), but to have the correct option selected when you open the page, you need to add a check to each option.
Example for the year:
<?php
$selected_year = date("Y", $date_timestamp); // get the selected year
for($year=$curryear;$year<$curryear+51;$year++)
{
?>
<option value="<?php echo $year; ?>" <?php echo ($selected_year == $year) ? 'selected' : ''; ?>><?php echo $year; ?></option>
<?php
}
?>

Categories