I am having a problem inserting DATETIME into MySQL using PHP.
First, I am fetching a date from the news table:
$statement = $db->query("SELECT utcPublishedDate FROM News WHERE idNews = $newsID");
$row = $statement->fetch(PDO::FETCH_ASSOC);
$utcPublishedDate = $row["utcPublishedDate"];
Then I am doing some date manipulation on that date due to some code requirements, eventually this date is changing to something else.
Now, I have to update the table, so:
$statement = $db->query("UPDATE News SET utcPublishedDate = $utcPublishedDate WHERE idNews = $newsID");
This is causing an error. If I do this:
$statement = $db->query("UPDATE News SET utcPublishedDate = NOW() WHERE idNews = $newsID");
The statement will execute without a problem.
Now, how do I solve the error I am getting? Obviously It is something to do with the fact there are spaces in my DATETIME field. Suggestions please? Thanks.
try changing:
$statement = $db->query("UPDATE News SET utcPublishedDate = $utcPublishedDate WHERE idNews = $newsID");
to:
$statement = $db->query("UPDATE News SET utcPublishedDate = '$utcPublishedDate' WHERE idNews = $newsID");
Convert the DATETIME via php first so that it will be Mysql valid datetime. Use this:
$utcPublishedDate = date('Y-m-d H:i:s', strtotime($utcPublishedDate)); $statement = $db->query("UPDATE News SET utcPublishedDate = '$utcPublishedDate' WHERE idNews = $newsID");
Cheers
Related
I am trying to do an update query in php to update my database but the query is not working. It is probably something simple.
$query = "UPDATE Events
SET charity_name = '$charity_name' ,
charity_reg = $charity_reg ,
Event_Name = '$event_tit',
Event_Status_Code = '$event_stat',
Start_Date = $event_dat,
Hours = $event_hour,
location = '$event_loc',
Other_Details = $event_content,
event_image = $imageData,
image_name = '$imageName',
max_available_spaces = $event_spaces,
Event_type = '$eve_category',
event_cost = $event_cost,
event_organiser = '$event_organiser'
WHERE Event_ID = $the_event_id";
You are not putting quotes ('') around some values, that might be a problem unless all thoses values are boolean/ints. Make sure to put quotes around all values, like '$imageData' instead of $imageData Also watch out for sql injections when you are directly inputting the values in your query. Better to use prepared statements
$query = "UPDATE Events
SET charity_name = '$charity_name' ,
charity_reg = '$charity_reg' ,
Event_Name = '$event_tit',
Event_Status_Code = '$event_stat',
Start_Date = '$event_dat',
Hours = '$event_hour',
location = '$event_loc',
Other_Details = '$event_content',
event_image = '$imageData',
image_name = '$imageName',
max_available_spaces = '$event_spaces',
Event_type = '$eve_category',
event_cost = '$event_cost',
event_organiser = '$event_organiser'
WHERE Event_ID = $the_event_id;";
EDIT: as #dWinder mentioned: if $the_event_id is not an integer, make sure to also put quotes around that value.
Here is my code for updating the date field for all records that have the field WorkoutID = $currentWorkoutID. When I run the query the dates change to 0000-00-00 and not to the current date. How can I fix this? Btw DB::getInstance() executes the query. I think something is wrong with the actual query?
$currentWorkoutID = $_SESSION['GlobalWorkoutID'];
echo $currentWorkoutID;
$date = date("y/m/d");
echo $date;
$sql = "UPDATE workout SET Date = ".$date." WHERE WorkoutID = ".$currentWorkoutID."";
DB::getInstance()->query($sql);
include single quotes ' for date
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
^ ^
i agrre with the answer
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
because maybe the type of Date is String ,
hope to help you,thank you
Try this
$sql = "UPDATE workout SET `Date` = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
Because date is a reserved keyword
I am trying to create a check-in/check-out table in my database. My check-in form works without issue, inserting the time into my database. The problem occurs when I try to check out. Everything is good on the first entry...
But when I try to check in and check out again, this happens...
So far so good, but when I check out...
Currently, my code updates the out column and totalTime column of all matching child_id's.
Here is my code:
// Select the correct child from the database
$sql_childID = "SELECT id FROM child
WHERE firstName = '$childFirstName'
AND lastName = '$childLastName'";
$result = $pdo->query($sql_childID);
$row = $result->fetch();
$var = $row['id'];
// Insert the check out time for the child
$query = "UPDATE checkinout
SET `out` = :nowTime
WHERE child_id = $var
AND `in` IS NOT NULL";
$statement = $pdo->prepare($query);
$statement->bindValue(':nowTime', date("YmjHis"));
$statement->execute();
// Select check in time for specified child
$sql_inTime = "SELECT `in` FROM checkinout
WHERE child_id = $var";
$inResult = $pdo->query($sql_inTime);
$inRow = $inResult->fetch();
$inTime = strtotime($inRow['in']);
// Select the check out time for specified child
$sql_outTime = "SELECT `out` FROM checkinout
WHERE child_id = $var";
$outResult = $pdo->query($sql_outTime);
$outRow = $outResult->fetch();
$outTime = strtotime($outRow['out']);
// Find total hours
$totalTime = abs($outTime - $inTime)/(60*60);
// Update totalHours column for specified child
$queryTotalTime = "UPDATE checkinout
SET totalTime = :totalTime
WHERE child_id = $var
AND 'out' IS NOT NULL";
$statement = $pdo->prepare($queryTotalTime);
$statement->bindValue(':totalTime', $totalTime);
$statement->execute();
I think you could do all of this in your first update statement using TIMESTAMPDIFF rather than figuring the total time with PHP:
UPDATE checkinout
SET
out = NOW(),
totalTime = TIMESTAMPDIFF(SECOND, `in`, NOW()) / 3600
WHERE
child_id = $var
AND out IS NULL
The criteria WHERE out IS NULL will only update rows that do not have a value in the out column yet.
IF you have MySQL Db THEN sql will be
SELECT TIMESTAMPDIFF(HOUR,in,out) from checkinout;
I have the following code and it works fine when updating the score and date. But it won't update the row's name or country. Does this have something to do with the php string??? Very confused!
$userName = "John";
$userCountry = "USA";
$lowestScoreId = 99;
$userPoints = 500;
include 'config.php';
$currentTime = time();
mysql_query("UPDATE highScores SET name = $userName WHERE id='$lowestScoreId'");
mysql_query("UPDATE highScores SET score = $userPoints WHERE id='$lowestScoreId'");
mysql_query("UPDATE highScores SET country =$userCountry WHERE id='$lowestScoreId'");
mysql_query("UPDATE highScores SET date = $currentTime WHERE id='$lowestScoreId'");
You forgot the quotes around the values you set. And you can do that in 1 query.
UPDATE highScores
SET `name` = '$userName',
`score` = '$userPoints',
`country` = '$userCountry',
`date` = '$currentTime'
WHERE id='$lowestScoreId'"
You should do this in one statement.
$userName = "John";
$userCountry = "USA";
$lowestScoreId = 99;
$userPoints = 500;
include 'config.php';
$currentTime = time();
mysql_query("UPDATE highScores SET name = '$userName', score = '$userPoints', country = '$userCountry', date = '$currentTime' WHERE id='$lowestScoreId'");
Also, you shouldn't use the PHP mysql_ functions anymore. Have a look at MySQLi which is newer, faster and has more features.
PHP/MySQL (CodeIgniter)
I would like to add new interest_keywords in the exist database value.
here is my code
$query = 'SELECT u_interest_keyword FROM '.T_USER_ACCOUNT.' WHERE u_id = "'.$u_id.'"';
$result = $this->db->query($query);
$result_keyword = $result.','.$personal_keyword;
$query = 'UPDATE '.T_USER_ACCOUNT.' SET u_interest_keyword = "'.$result_keyword.'" WHERE u_id = "'.$u_id.'"';
$this->db->query($query);
It just replaces a new keyword in the database.
Can you tell me why it doesn't work?
$this->db->query returns object when read type queries are run.
So, you have to do something like this after $result = $this->db->query($query);
$result_row = $result->row();
Then Rectify this:
$result_keyword = $result_row->u_interest_keyword. ',' .$personal_keyword;
$row = $result->row();
$result_keyword = $row->u_interest_keyword.','.$personal_keyword;