I have a countdown script that works 100% successfully. It's pulling the data down from the database but I'm having a few issues with a cronjob script. I want it to add a week to the community_night date.
When I try adding to this, it keeps resetting the date to 0000-00-00 however I can see that the query should be working correctly.
<?php
$con = mysqli_connect("localhost","","","") or die("Error in the consult.." . mysqli_error($connect));
$result = mysqli_query($con,"SELECT * FROM Community_Night");
while($row = mysqli_fetch_array($result))
$dbDate = $row['community_night'];
$date = strtotime($dbDate . ' + 1 week');
$setDate = date('d-m-Y',$date);
$sql = "UPDATE Community_Night SET community_night =" . $setDate;
if (mysqli_query($con, $sql)) {
echo "Record updated successfully: " . $sql;
} else {
echo "Error updating record: " . mysqli_error($con);
}
?>
the SQL column is set to DATE.
I haven't tested this, but it should work:
$sql = "UPDATE Community_Night SET community_night = community_night + INTERVAL 1 WEEK";
Bear in mind this will update all the rows in the table.
Related
Is this possible to do?
I have a query that is selecting all records from a table, which the results is based on a time field. This is a schedule of games for 17 weeks. What I am trying to do is create an array of just the current weeks games that have expired. In my code below, every expired game shows in the array. Is it possible to just create this separate array to only include the current week games? I still need to capture all the data, but am trying to create an array of data just for the current week as seen in the row expired portion of the code.
$latestweek = getCurrentWeek(); //gives the current week
for($wk=1;$wk<=17;$wk++){
$games = array();
$sql = "select s.*, (DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > gameTimeEastern or DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > '" . $cutoffDateTime . "') as expired ";
$sql .= "from " . DB_PREFIX . "schedule s ";
$sql .= "where s.weekNum = " . $wk . " ";
$sql .= "order by s.gameTimeEastern, s.gameID";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$e = 0;
while ($row = $query->fetch_assoc()) {
$games[$row['gameID']]['gameID'] = $row['gameID'];
$games[$row['gameID']]['homeID'] = $row['homeID'];
$games[$row['gameID']]['visitorID'] = $row['visitorID'];
$games[$row['gameID']]['sWinner'] = $row['result'];
$games[$row['gameID']]['startTime'] = $row['gameTimeEastern'];
if ($row['expired']){
$survGamesExpired_h[$e]=$row['homeID'];
$survGamesExpired_v[$e]=$row['visitorID'];
}
$expiredTeams = array_merge((array)$survGamesExpired_h,(array)$survGamesExpired_v);
}
}
I have a table that has a column called hdate. I have set this withe the date property.
I have a form :
<H2>Search By Date</H2></div>
<form name="attdate" action="datesearch.php" method="post">
From :<input id="datepicker" name = "startd"> To: <input id="datepicker1" name = "endd">
<input type="submit" value="Search Records">
Now my intention is to allow the user to enter a start date and end date and obtain all the rows with these date.
<?php
include 'config.php';
$startd = ($_POST['startd']);
$endd = ($_POST['endd']);
$startd = mysqli_real_escape_string($con,$startd);
$endd = mysqli_real_escape_string($con,$endd);
$sql = "SELECT * FROM handover WHERE hdate AND >= '" . $startd . "' AND <'"
.$endd. "' + ‘ 23:59:59’"
;
$result = mysqli_query($con,$sql);
$count=mysqli_num_rows($result);
if($count==0 ){
echo "</br></br></br></br></br></br></br><p> No Matching results found</p>";
}
else{
while($row = mysqli_fetch_array($result)) {
echo 'here are your results';
My dates are going in to the database for hdate and in the same format set by the datepicker.
I am successfully getting results querying other columns. Any ideas? note time added after reading on a couple of sites. I have read other questions on stacked but they do not relate to user input.
When testing with the current set up I am not getting errors just the "No matches so I guess the query is valid but not set up right to query my hdate column.
I can change column easily so if the simplest solution is to use timestamp or something then it okay to do so.
<?php
include 'config.php';
$startd = ($_POST['startd']);
$endd = ($_POST['endd']);
$startd = mysqli_real_escape_string($con,$startd);
$endd = mysqli_real_escape_string($con,$endd);
$startd = '2014-11-18';
$endd = '2014-11-20';
$sql = "SELECT * FROM handover WHERE hdate >= date('" . $startd . "') AND hdate <
ADDDATE(date('" . $endd . "'), INTERVAL 1 DAY)";
echo $sql; // run in mys
$result = mysqli_query($con,$sql);
$count=mysqli_num_rows($result);
if($count==0 ){
echo "</br></br></br></br></br></br></br><p> No Matching results found</p>";
}
else{
while($row = mysqli_fetch_array($result)) {
Im still just getting a "no matches " echo
Assuming that $startd and $endd are in your basic MySQL date format of YYYY-MM-DD, then:
$sql = "SELECT * FROM handover WHERE hdate >= date('" . $startd . "') AND hdate < ADDDATE(date('" . $endd . "'), INTERVAL 1 DAY)";
Otherwise, you'll probably have to perform some string operations to reformat $startd and $endd into YYYY-MM-DD before using the above.
My tip for how to debug this stuff in general would be hardcode some values in the variables and print the SQL out, then run it in the console and mess with it until you can get it to work as expected:
$startd = '2014-11-18';
$endd = '2014-11-20';
$sql = "SELECT * FROM handover WHERE hdate >= date('" . $startd . "') AND hdate < ADDDATE(date('" . $endd . "'), INTERVAL 1 DAY)";
echo $sql; // run in mysql console and see how it works before moving forward in php
Then after that works, upgrade to using the POST parameters and printing the SQL out, and you'll know whether it looks right or not based on whether it matches what was working before.
Please note your SQL syntax has been corrected above, as you had AND twice where its only needed once, and you were missing the reference to hdate after your second AND.
I' am running a query on table tblpages_copy to get the row datecreated and updating tblpages_copy2 in the same loop. For some reason in datecreated in the tblpages_copy2 it enters todays date.
I have checked the $date2 values and its prefectly fine its a correct value. The type of the datecreated in tblpages_copy is string
HERE IS THE QUERY
$i=0;
$q = mysql_query( "SELECT datecreated FROM tblpages_copy" );
while($row = mysql_fetch_array($q)){
$datecreated = $row["datecreated"];
$date = strtotime($datecreated);
$date2 = date('Y-m-d H:i:s', $date);
$q2 = mysql_query( 'UPDATE tblpages_copy2 SET datecreated = ' . $date2 . ' ');
if($q2){
echo $i++ .' '. $q2 . ' ' . $date2 . "<br/>";
} else {
echo mysql_error() . "<br/>";
}
}
MySQL error dump is
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'xx' at line 1
What I suspect is that the Update query was wrong. I have tried few ways yet no luck
$q2 = mysql_query( "UPDATE tblpages_copy2 SET datecreated = '$date2'");
$q2 = mysql_query( "UPDATE tblpages_copy2 SET datecreated = ". $date2);
Try doing this, surrounding $date2 with ' and also using "'s for the query instead of ''s:
$q2 = mysql_query("UPDATE tblpages_copy2 SET datecreated = "' . $date2 . '" ");
Change
mysql_query( 'UPDATE tblpages_copy2 SET datecreated = ' . $date2 . ' ');
to
mysql_query( 'UPDATE tblpages_copy2 SET datecreated = "' . $date2 . '"');
and I bet it works. Dates are strings like anything else and must be quoted.
Try
'UPDATE tblpages_copy2 SET datecreated = "' . $date2 . '"'
Don`t use mysql_* functions. mysql_* functions are deprecated. You can use PDO and Mysqli
I'm trying to update a range of dates with a Shift_ID but the Shift_ID that is entered depends upon what day of the week it is. So, for example, if I have a range of dates $from = "2012-12-01" $to = "2012-12-28" I'm trying to make it so that I can select a check box (for example: value="Fri" name = "offdays[]") to indicate what day is an offdays. If an offdays is matched with a day in the set range, then the Shift_ID = "6" otherwise it is a work day and Shift_ID = "3"
Hopefully this will all make sense in a minute, I'm doing my best to explain it as well as give you some useful variables.
So here is my code:
if(isset($_POST['submit']))
{
if(isset($_POST['offdays']))
{
//$off is set through the config settings//
$shift = mysqli_real_escape_string($_POST['shift']);
$from = mysqli_real_escape_string($_POST['from']);
$to = mysqli_real_escape_string($_POST['to']);
$emp_id = mysqli_real_escape_string($_POST['emp_id']);
foreach($_POST['offdays'] as $checkbox)
{
echo "Checkbox: " . $checkbox . "<br>";//error checking
$sql = ("SELECT Date FROM schedule WHERE (Date BETWEEN '$from' AND '$to') AND (Emp_ID = '$emp_id')");
if(!$result_date_query = $mysqli->query($sql))
{
echo "INSERT ERROR 1 HERE";
}
echo "SELECT SQL: " . $sql . "<br>";//error checking
while($row = $result_date_query->fetch_assoc())
{
echo "Date: " . $row['Date'] . "<br>";//error checking
$date = date('D',strtotime($row['Date']));
if($date == $checkbox)
{
echo "MATCHED! Date: " . $date . " Checkbox: " . $checkbox . "<br>";//error checking
$sql = ("UPDATE schedule SET Shift_ID = '$off' WHERE Date = '" . $row['Date'] . "' AND Emp_ID = '$emp_id'");
if(!$result_update_offdays_query = $mysqli->query($sql))
{
echo "INSERT ERROR 2 HERE";
}
echo "UPDATE DAYS OFF SQL: " . $sql . "<br><br>";//error checking
}
else
{
echo "NOT MATCHED! Date: " . $date . " Checkbox: " . $checkbox . "<br>";//error checking
$sql = ("UPDATE schedule SET Shift_ID = '$shift' WHERE Date = '" . $row['Date'] . "' AND Emp_ID = '$emp_id'");
if(!$result_update_shift_query = $mysqli->query($sql))
{
echo "INSERT ERROR 3 HERE";
}
echo "UPDATE SHIFT SQL: " . $sql . "<br><br>";//error checking
}
}
}
}
}
When I look at my printed echo statements, everything is perfect! When a date lands on a Friday, it shows that it's entering Shift_ID = "6" and any other day shows Shift_ID = "3". The problem is the tables don't reflect what my statements are telling me, they all just get updated to Shift_ID = "3".
In the end I want to be able to select more than one offdays, but when I select more than one, it overwrites my previous day during the next loop, which makes sense.
I've got no idea what is going on, if anyone can give me a clue, I would really appreciate it.
UPDATE: When I add exit; after the days off update, like this:
echo "UPDATE DAYS OFF SQL: " . $sql . "<br><br>";//error
exit;
it does update the day off to Shift_ID = "6", so it must be something happening after that.
EDIT: It appears as though the problem was with user permissions. I can't explain why, but after deleting the user and creating a new one with full permissions, things started to work. I chose #andho's answer as he helped me get to the answer in the chat forum and also added a way to clean up my code.
This doesn't solve your issue, but simplifies the solution!
if your offdays variable is as follows: $offdays = array('Fri', 'Sun');
You can first set all dates in range to $shift in one query:
UPDATE Schedule SET Shift_ID = '$shift' WHERE Date BETWEEN '$from' AND '$to' AND Emp_ID = '$emp_id'
Then you can loop through the foreach ($offdays as $offday) { and update those offdays:
UPDATE Schedule SET Shift_ID = '$off' WHERE Date BETWEEN '$from' AND '$to' AND Emp_ID = '$emp_id' AND DATE_FORMAT(Date, '%a') = '$offday'
That should update all the $offday's in the range to $off.
This will reduce your loops and queries, which gives leaner code.
$sql = ("SELECT `Date` FROM schedule WHERE (`Date` BETWEEN '$from' AND '$to') AND (Emp_ID = '$emp_id')");
Use backtick(`) to all the fieldname date because it is reserved in mysql.
So, my table has a bunch of codes in it and I don't want more than one of the same code in the table so I have it as "UNIQUE."
But, at the same time, I want them to be able to bump their code once every hour.
function some_more_custom_content() {
$output="<BR>";
ob_start();
if ($_REQUEST['code'] != "") {
$code = $_REQUEST['code'];
$query="INSERT INTO `fc` (`code`,`datetime`) values ('" . mysql_real_escape_string($code) . "', now())";
$result=mysql_query($query) or die(mysql_error());
$seconds = time() - strtotime($fetch_array["datetime"]);
if($sql){
echo("Intserted " . htmlentities($code) ." into the top.");
}else{
if ($seconds < 60*60) {
echo ("The code " . htmlentities($code) ." was updated less than an hour ago.");
} else {
$query="DELETE FROM `fc` (`code`,`datetime`) values ('" . mysql_real_escape_string($code) . "', now())";
echo ("Inserted " . htmlentities($code) ." into the top.");
}
}}
Now, I tried to get it so that when the code works it submits the code as normal, which I think works.
Now, if it gets a code that is already there I get the duplicate error "Duplicate entry 'Bob' for key 1"
But, I just want it to delete the old query that it found and to resubmit if it's been more than one hour since last submission.
Any ideas what I am doing wrong?
You can just update the date/time field in the database to reflect the time of the "bump".
if ($_REQUEST['code'] != "")
{
$code = $_REQUEST['code'];
$sql = "SELECT * FROM fc WHERE code = '" . mysql_real_escape_string($code) . "'";
$result = mysql_query($sql);
if (mysql_num_rows($result))
{
$row = mysql_fetch_array($result);
$seconds = time() - strtotime($row["datetime"]);
if ($seconds > 60*60)
{
$sql = "UPDATE fc SET datetime = NOW() WHERE code = '" . mysql_real_escape_string($code) . "'";
mysql_query($sql);
echo("Intserted " . htmlentities($code) ." into the top.");
}
else
{
echo ("The code " . htmlentities($code) ." was updated less than an hour ago.");
}
}
else
{
$sql = "INSERT INTO fc (code, datetime) VALUES ('" . mysql_real_escape_string($code) . "', NOW())";
mysql_query($sql);
echo("Intserted " . htmlentities($code) ." into the top.");
}
}
(I probably shouldn't have re-written your code for you, and there are a few more improvements that could be made, but that reflects the best changes I can make with the minimum of changes (does that make any sense?))
Frank, you do not need two different queries
$query="INSERT INTO `fc` (`code`,`datetime`)
values ('" . mysql_real_escape_string($code) . "', now())
ON DUPLICATE KEY UPDATE datetime = case when now() - interval 1 hour > `datetime`
then NOW()
else `datetime`
end";