php, How to set date to a month? - php

I have a calendar and I'm adding recurring events to it, My problem is that the user can set a "repeat for x amount of months" variable, this variable is my $repeat_value and if it exceeds 12 i need to handle it in a way that the date year increases by 1, and i should set the month to 1, then add remaining months... how can i do this?
my code is setup like this:
for ($i = 1; $i < $repeat_value; $i++)
{
if ($repeat_type == "month")
{
if ($i > 12) //month is greater then 12, we must increase the year +1 and set month to jan
{
//please help!
}
else
{
$t_temp_start = date('Y-m-d h:m:s',strtotime($s_start . '+ ' . $i . ' months'));
$t_temp_end = date('Y-m-d h:m:s',strtotime($s_end . '+ ' . $i . ' months'));
}
$database->justquery("INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end . "', '" . $url . "');");
}
else if ($repeat_type == "year")
{
$t_temp_start = date('Y-m-d h:m:s',strtotime($s_start . '+ ' . $i . ' years'));
$t_temp_end = date('Y-m-d h:m:s',strtotime($s_end . '+ ' . $i . ' years'));
$database->justquery("INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end . "', '" . $url . "');");
}
}
I hope this makes sense, help appreciated!

The fact that there is more than 12 months has absolutely no importance.
Simply put:
for ($i = 1; $i < $repeat_value; $i++)
{
if ($repeat_type == "month")
{
$t_temp_start = date('Y-m-d h:m:s',strtotime($s_start . '+ ' . $i . ' months'));
$t_temp_end = date('Y-m-d h:m:s',strtotime($s_end . '+ ' . $i . ' months'));
$database->justquery("INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end . "', '" . $url . "');");
}
else if ($repeat_type == "year")
{
$t_temp_start = date('Y-m-d h:m:s',strtotime($s_start . '+ ' . $i . ' years'));
$t_temp_end = date('Y-m-d h:m:s',strtotime($s_end . '+ ' . $i . ' years'));
$database->justquery("INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end . "', '" . $url . "');");
}
}
You can try this:
// these two lines produce exactly the same result
print date('Y-m-d h:m:s',strtotime('today + 14 months'));
print date('Y-m-d h:m:s',strtotime('today + 1 year + 2 months'));
Also, I thought you could try this simpler/shorter way of doing what you want:
$db_query = '';
for ($i = 1; $i < $repeat_value; $i++)
{
$t_temp_start = data('Y-m-d h:m:s', strtotime($s_start.' + '.$i.' '.$repeat_type.'s'));
$t_temp_end = $t_temp_start + $s_end - $s_start;
db_query .= "INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end . "', '" . $url . "');";
}
if (!empty($db_query))
$database->justquery($db_query);
Which potentially saves you lots of database queries (which should never be placed inside loops), and makes your code easier to read (by saving you a conditional statement). It also enables you to accept repeats each year, month, but also week, day, hour and so on without changing a single byte of your code.
In a more general point of view, you should notice when you have an if() ... else conditional statement where the else is almost exactly a repetition of the if. It is often a sign that there is room for improvement in your code.
And one last word: be careful to SQL injections.

Related

how to check duplicate records in mysql database

$queryb = "SELECT product, imei, country, warranty, config from PRODUCT WHERE product_slno = '$mnserialno' ";
$resultb = mysql_query($queryb, $gndbconn) ;
if(mysql_num_rows($resultb) > 0)
{
$queryc = "UPDATE PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config' WHERE product_slno = '.$mnserialno.' ";
$resutc = mysql_query($queryc, $gndbconn) ;
}
else{
$querya = "INSERT INTO PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config'";
$resulta = mysql_query($querya, $gndbconn) ;
}
I want to check the serial number if that serial number already exist in database so records get update, otherwise it get insert into the database.
but the code inserting the records only, no updation, what is the fault i am not getting,
how to prevent the duplicate entry?
INSERT INTO PRODUCT SET
(`product`, `product_slno`, `imei`, `country`, `warranty`, `config`)
VALUES
('" . $desc . "', '" . $mnserialno . "', '" . $imei . "', '" . $country . "', '" . $warranty . "', '" . $config . "')
ON DUPLICATE KEY UPDATE
product='" . $desc . "',
product_slno='" . $mnserialno . "',
imei='" . $imei . "',
country='" . $country . "',
warranty='" . $warranty . "',
config='" . $config . "'";

How to update time in MySQL, opencart

I am trying to take insert hours and minutes from MySQL in OpenCart
when I am trying to run a normal query in MySQL its running fine
UPDATE `oc_delivery_time` SET `delivery_time_name`="Slot2",`delivery_time_from`=TIME( STR_TO_DATE( "9:30 AM", "%h:%i %p" ) ),`delivery_time_to`=TIME( STR_TO_DATE( "12:00 PM", "%h:%i %p" ) ),`status`="1" WHERE `delivery_time_id`="2"
but when i do it in opencart I am getting this error.
Parse error: syntax error, unexpected ':' in C:\xampp\htdocs\gdr\admin\model\localisation\delivery_time.php
here is my query in opencart model
$this->db->query("UPDATE " . DB_PREFIX . "delivery_time SET delivery_time_name = '" . $this->db->escape($data['delivery_time_name']) . "', delivery_time_from = 'TIME( STR_TO_DATE( " . $this->db->escape($data['delivery_time_from']) . ", "%h:%i %p" ) )', delivery_time_to = '" . $this->db->escape($data['delivery_time_to']) . "', status = '" . (int)$data['status'] . "' WHERE delivery_time_id = '" . (int)$delivery_time_id . "'");
Try this
$this->db->query("UPDATE " . DB_PREFIX . "delivery_time SET delivery_time_name = '" . $this->db->escape($data['delivery_time_name']) . "', delivery_time_from = 'TIME( STR_TO_DATE( " . $this->db->escape($data['delivery_time_from']) . ", \"%h:%i %p\" ) )', delivery_time_to = '" . $this->db->escape($data['delivery_time_to']) . "', status = '" . (int)$data['status'] . "' WHERE delivery_time_id = '" . (int)$delivery_time_id . "'");

Can't UPDATE all column values only few columns are getting updated

I want to update all column values, as you can see from the code. But only the first three columns get updated and last two values get updated.
Home_number
Office_number
Fax_number
does not get updated.
Please help solve the following problem.
if (isset($_POST["submit"]) && $_POST["submit"] !="")
{
$usersCount = count($_POST["F_name"]);
echo "$userCount";
for ($i = 0; $i < $usersCount; $i++)
{
mysql_query("UPDATE contactss set F_name='" .
$_POST["F_name"][$i] . "', L_name='" . $_POST["L_name"][$i] . "', mob_number='" . $_POST["mob_number"][$i] . "', Home_number='" . $_POST["Home_number"][$i] . "', Office_number='" . $_POST["Office_number"][$i] . "', Fax_number='" . $_POST["Fax_number"][$i] . "', email='" . $_POST["email"][$i] . "', Address='" . $_POST["Address"][$i] . "' WHERE C_id='" . $_POST["C_id"][$i] . "'");
}
header("Location:list_user.php");
}

Updating Values on Two Field Pairs Returning Strange Vlaues

I have the following query in my application to fix any unknown boxer ID's when a new boxer is added to my database. To make other queries and function in the system easier, when a bout is added boxer_1 ID is always less than the boxer_2 ID.
The below function seem to work perfectly to update/switch the boxer_1 and boxer_2 fields, but the boxer_1_weight and boxer_2_weight values get strange results and i can't work out why?
"UPDATE bouts b1, bouts b2 SET b1.unknown_global_id = 0,
b1.winner = IF(b2.winner = -3, IF(b1.boxer_1 = 0, '" . $boxer_id . "', b1.boxer_1), IF(b2.winner = -2, IF(b1.boxer_2 = 0, '" . $boxer_id . "', b1.boxer_2), b1.winner)),
b1.boxer_1 = IF(b1.boxer_1 = 0, IF('" . $boxer_id . "' > b1.boxer_2, b1.boxer_2, '" . $boxer_id . "'), IF(b1.boxer_1 < '" . $boxer_id . "', b1.boxer_1, '" . $boxer_id . "')),
b1.boxer_2 = IF(b1.boxer_2 = 0, IF('" . $boxer_id . "' < b2.boxer_1, b2.boxer_1, '" . $boxer_id . "'), IF(b1.boxer_2 > '" . $boxer_id . "', b1.boxer_2, '" . $boxer_id . "')),
b1.boxer_1_weight = IF(b2.boxer_1 = 0, IF('" . $boxer_id . "' > b2.boxer_2, b2.boxer_2_weight, b2.boxer_1_weight), IF(b2.boxer_1 < '" . $boxer_id . "', b2.boxer_1_weight, b2.boxer_2_weight)),
b1.boxer_2_weight = IF(b2.boxer_2 = 0, IF('" . $boxer_id . "' < b2.boxer_1, b2.boxer_1_weight, b2.boxer_2_weight), IF(b2.boxer_2 > '" . $boxer_id . "', b2.boxer_2_weight, b2.boxer_1_weight))
WHERE b1.unknown_global_id = '" . (int)$global_id . "'"
boxer_1/boxer_2 fields are INT(11) and boxer_1_weight/boxer_2_weight fields are decimal(10,1)
As an example, if the weight fields start with weights 186.5 and 199.7, after running the query they both end up as 239.8, but the boxer ID's are correct.
You need a join condition to avoid comparing the selected row in b1 with every other row in b2.
"UPDATE bouts b1
JOIN bouts b2 ON b1.bout_id = b2.bout_id
SET b1.unknown_global_id = 0,
b1.winner = IF(b2.winner = -3, IF(b1.boxer_1 = 0, '" . $boxer_id . "', b1.boxer_1), IF(b2.winner = -2, IF(b1.boxer_2 = 0, '" . $boxer_id . "', b1.boxer_2), b1.winner)),
b1.boxer_1 = IF(b1.boxer_1 = 0, IF('" . $boxer_id . "' > b1.boxer_2, b1.boxer_2, '" . $boxer_id . "'), IF(b1.boxer_1 < '" . $boxer_id . "', b1.boxer_1, '" . $boxer_id . "')),
b1.boxer_2 = IF(b1.boxer_2 = 0, IF('" . $boxer_id . "' < b2.boxer_1, b2.boxer_1, '" . $boxer_id . "'), IF(b1.boxer_2 > '" . $boxer_id . "', b1.boxer_2, '" . $boxer_id . "')),
b1.boxer_1_weight = IF(b2.boxer_1 = 0, IF('" . $boxer_id . "' > b2.boxer_2, b2.boxer_2_weight, b2.boxer_1_weight), IF(b2.boxer_1 < '" . $boxer_id . "', b2.boxer_1_weight, b2.boxer_2_weight)),
b1.boxer_2_weight = IF(b2.boxer_2 = 0, IF('" . $boxer_id . "' < b2.boxer_1, b2.boxer_1_weight, b2.boxer_2_weight), IF(b2.boxer_2 > '" . $boxer_id . "', b2.boxer_2_weight, b2.boxer_1_weight))
WHERE b1.unknown_global_id = '" . (int)$global_id . "'"

escape null values(Nestoria UK)

I am able to retrieve data from a website(Nestoria) and store into my PostGIS database. However not every individual result that is returned have latitude and longitude values and as such they are not stored into the database. I tried using (pg_escape_string) so that it can return null values as double quotes(") but it didn't work. The field type of my lat and long columns in the database is "double precision".
This is a sample output of the error: ("Warning: pg_query() [function.pg-query]: Query failed: ERROR: invalid input syntax for type double precision: "" LINE 1: ...droom garden flat set in a period building with...', '', '') ^ in C:\XAMMP...\database.php on line 12")
Please see the code below for retrieving the data:
<?php
$url = ("http://api.nestoria.co.uk/api?action=search_listings&centre_point=51.5424,-0.1734,2km&listing_type=rent&property_type=all&price_min=min&price_max=max&bedroom_min=0&bedroom_max=0&number_of_results=50&has_photo=1&page=4");
$xml = simplexml_load_file($url);
foreach ($xml->response->listings as $entry) {
echo $entry->attributes()->title;
echo $entry->attributes()->bathroom_number;
echo $entry->attributes()->bedroom_number;
echo $entry->attributes()->datasource_name;
echo $entry->attributes()->guid;
echo $entry->attributes()->img_url;
echo $entry->attributes()->keywords;
echo $entry->attributes()->lister_name;
echo $entry->attributes()->listing_type;
echo $entry->attributes()->price;
echo $entry->attributes()->price_type;
echo $entry->attributes()->property_type;
echo $entry->attributes()->summary;
echo $entry->attributes()->latitude;
echo $entry->attributes()->longitude;
// Process XML file
}
?>
Find below the code to store values into database:
<?php
require 'nestoriauk.php';
// Opens a connection to a PostgresSQL server
$connection = pg_connect("dbname=postgis user=postgres password=xxxx");
// Execute query
foreach ($xml->response->listings as $entry) {
$query = "INSERT INTO nestoriaphp(title, bathroom, bedroom, datasource, guid, image, keywords, lister, listype, price, pricetype, property_type, summary, latitude, longitude) VALUES ('" . pg_escape_string($entry->attributes()->title) . "', '" . pg_escape_string($entry->attributes()->bathroom_number) . "', '" . pg_escape_string($entry->attributes()->bedroom_number) . "', '" . pg_escape_string($entry->attributes()->datasource_name) . "', '" . pg_escape_string($entry->attributes()->guid) ."', '" . pg_escape_string($entry->attributes()->img_url) . "', '" . pg_escape_string($entry->attributes()->keywords) . "', '" . pg_escape_string($entry->attributes()->lister_name) . "', '" . pg_escape_string($entry->attributes()->listing_type) . "', '" . pg_escape_string($entry->attributes()->price) . "', '" . pg_escape_string($entry->attributes()->price_type) . "', '" . pg_escape_string($entry->attributes()->property_type) ."', '" . pg_escape_string($entry->attributes()->summary) . "', '" . pg_escape_string($entry->attributes()->latitude) . "', '" . pg_escape_string($entry->attributes()->longitude) . "')";
$result = pg_query($query);
printf ("These values are inserted into the database - %s %s %s", $entry->attributes()->title, $entry->attributes()->bathroom_number, $entry->attributes()->bedroom_number, $entry->attributes()->datasource_name, $entry->attributes()->guid, $entry->attributes()->img_url, $entry->attributes()->keywords, $entry->attributes()->lister_name, $entry->attributes()->listing_type, $entry->attributes()->price, $entry->attributes()->price_type, $entry->attributes()->property_type, $entry->attributes()->summary, $entry->attributes()->latitude, $entry->attributes()->longitude);
}
pg_close();
?>
Yemi
If a POI has no lat-lon, which value it has? empty string?
assuming that an empty string is returned when there is no lat-lon.
$lon = "NULL";
if($entry->attributes()->longitude != "")
$lon = "'".$entry->attributes()->longitude."'";
$lat = "NULL";
if($entry->attributes()->latitude != "")
$lat = "'".$entry->attributes()->latitude."'";
so query looks like this:
$query = "INSERT INTO nestoriaphp(title, bathroom, bedroom, datasource, guid, image, keywords, lister, listype, price, pricetype, property_type, summary, latitude, longitude) VALUES ('" . pg_escape_string($entry->attributes()->title) . "', '" . pg_escape_string($entry->attributes()->bathroom_number) . "', '" . pg_escape_string($entry->attributes()->bedroom_number) . "', '" . pg_escape_string($entry->attributes()->datasource_name) . "', '" . pg_escape_string($entry->attributes()->guid) ."', '" . pg_escape_string($entry->attributes()->img_url) . "', '" . pg_escape_string($entry->attributes()->keywords) . "', '" . pg_escape_string($entry->attributes()->lister_name) . "', '" . pg_escape_string($entry->attributes()->listing_type) . "', '" . pg_escape_string($entry->attributes()->price) . "', '" . pg_escape_string($entry->attributes()->price_type) . "', '" . pg_escape_string($entry->attributes()->property_type) ."', '" . pg_escape_string($entry->attributes()->summary) . "', " . $lat . ", " . $lon . ")";
Note that the NULL value has no '' in sql.

Categories