Can't update Date field in MySql using PHP - php

I have a DATE field in my table, and I'm trying to update it using the following code:
$query = mysqli_query($conn,$sql);
$todaydate = date("Y-m-d");
$sqlDate = date('Y-m-d', strtotime($todaydate));
$sql="UPDATE Library SET Loaned=1, LoanedDate=$sqlDate WHERE BookId=$bookId";
$query = mysqli_query($conn,$sql);
It updates the "Loaned" field fine, but always sets the Date field to "0000-00-00". Can anyone indicate what I'm doing wrong?

Youre missing ' quotes in your update query
Just use something like this
$sql="UPDATE Library SET Loaned=1, LoanedDate='$sqlDate' WHERE BookId='$bookId'";
$query = mysqli_query($conn,$sql);

You must have to add Quotes to the values... other wise the date may seem like an invalid integer to mysql. Here's how:
$query = mysqli_query($conn,$sql);
$todaydate = date("Y-m-d", time()); //<== DON'T FORGET THE 2ND ARGUMENT TO date(): TIME-STAMP. YOU MAY USE: time()
$sqlDate = date('Y-m-d', strtotime($todaydate));
$sql ="UPDATE Library SET Loaned=1, LoanedDate='{$sqlDate}' WHERE BookId='{$bookId}'";
$query = mysqli_query($conn,$sql);

Why don't you use only MySQL function to update date
Use this
$sql = "UPDATE Library SET Loaned = 1, LoanedDate = DATE(NOW())
WHERE BookId = '".$bookId."'";

Change your query with this code
$sql="UPDATE Library SET Loaned=1, LoanedDate= current_date() WHERE BookId='".$bookId."'";
Also check data structure in database.

This query is working fine:
$current_date = strtotime(date('Y-m-d H:i:s'));
$sql = "UPDATE user SET dt_added = '".$current_date."' WHERE id = '$id' ";

You're not concatenating the Date string properly in the SQL query.
Use ' and . operator to concatenate the string. Like this,
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$sqlDate."' WHERE BookId='".$bookId."'";
Altough it's good practice to use Prepared statement to pass arguments in SQL query.
Learn more about PHP Prepared Statements

Looking closer, I would just do:
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$todaydate."' WHERE BookId='".$bookId."'";
instead of
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$sqlDate."' WHERE BookId=$bookId";

Related

Trying to UPDATE SQL DATE column via HTML form; Is this even possible? [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 3 years ago.
When I run this Script the strtotime and date functions work, but when the SQL query runs the date column in the db remains blank.
$date = mysqli_real_escape_string($conn, $_POST['date']);
$day1 = strtotime($date);
$day1 = date('Y-m-d', $day1);
$id = 1;
echo $day2;
$sql = "UPDATE essay SET date = $day1 WHERE id = $id";
You have to add a quote over the $day1 like this way :
$sql = "UPDATE essay SET date = '$day1' WHERE id = '$id'";
Another way to do it by concatenate :
$sql = "UPDATE essay SET date = ".$day1." WHERE id = ".$id;
Unless an SQL field is an integer type or similar numeric type, data written to it should be quoted in an insert statement. In this case, your $day1 is something like "2019-04-18" so your SQL should read:
$sql = "UPDATE essay set date = '$day1' where id = $id";
The single quote should allow the query to succeed. Note that debugging this sort of thing is fairly easy, but isn't taught in some tutorials; if the query fails, try logging or echoing the MySQL(i) error:
$query = $db->query($sql);
if (!$query) echo $db->error;

How to update timestamp in a mysql database?

There is some error when I am executing this php code:
$today = date("Y-m-d H:i:s");
$sql = "UPDATE `deposit_admin_report` SET `READING`=$item,`Timestamp`=$today WHERE `METER_NUMBER`='NP-1353-'";
Error:
UPDATE `deposit_admin_report` SET `READING`=395,`Timestamp`=2015-11-27 09:08:33 WHERE `METER_NUMBER`='NP-1353-'
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 '09:08:33 WHERE `METER_NUMBER`='NP-1353-'' at line 1
The column Timestamp has a type "timestamp" and default is "null" in the mysql table.
Will appreciate if anyone could help me out.
Thanks in advance
here is what you need:
$today = date("Y-m-d H:i:s");
$sql = "UPDATE `deposit_admin_report` SET `READING`=$item,`Timestamp`='".$today."' WHERE `METER_NUMBER`='NP-1353-'";
Also there is another way:
Simply use NOW()
Like this:
$sql = "UPDATE deposit_admin_report SET READING='$item',Timestamp=NOW()
WHERE METER_NUMBER='NP-1353-'";
That's it :)
use MySQL NOW() or change this $item to this '$item'
$sql = "UPDATE `deposit_admin_report` SET `READING`='$item',`Timestamp`=NOW()
WHERE `METER_NUMBER`='NP-1353-'";
$today = date("Y-m-d H:i:s");
$sql = "UPDATE `deposit_admin_report` SET `READING`= '" . $item . "',`Timestamp`= '" . $today . "' WHERE `METER_NUMBER`='NP-1353-'";
It sees the space in your date as a seperate word, and not as value of your timestamp.
Also, it is way easier to just use NOW()
$sql = "UPDATE `deposit_admin_report` SET `READING`= '" . $item . "',`Timestamp`= 'NOW()' WHERE `METER_NUMBER`='NP-1353-'";
Date field accepts a string which you should enclose in single quotes.
$sql = "UPDATE `deposit_admin_report` SET `READING`= $item, `Timestamp`= '$today' WHERE `METER_NUMBER`='NP-1353-'";
The question is already answered. Would like to bring into notice the caveat of using NOW() in Master / Slave architecture.
If timezone of slave server is different than that of master, the timestamp value stored on master will be different than the one on slave.
Its recommended to use following steps:
date_default_timezone_set('UTC'); // UTC is an example
$today = date("Y-m-d H:i:s");
$sql = "
UPDATE `deposit_admin_report`
SET `READING`=$item,`Timestamp`='".$today."'
WHERE `METER_NUMBER`='NP-1353-'
";

php mysql_query use variable as field name

I know that i shouldn't use mysql_query for make database query, but i need to modify an existing code.
What i need to do is to pass a php variable as field name of sql query.
I've try in this way:
$my_field = "field_name";
mysql_query("UPDATE my_table SET ".$my_field." =somevalue") or die(mysql_error());
but i've noticed that it's wrong, because resulting query is
UPDATE my_table SET =somevalue
What's the correct way to do it?
you missed the closing quotes, change to:
mysql_query("UPDATE my_table SET ".$my_field." =somevalue") or die(mysql_error());
for checking, add the statement to variable and echo it, as:
$my_field = "field_name";
$query = "UPDATE my_table SET ".$my_field." =somevalue");
echo $query; //see the output to check if it shows correct statement
try this
$my_field = "my_field";
$my_value = "my_value;
$query = "UPDATE my_table SET $my_field=$my_value");
php allows variables to work inside double quotes

Cannot rename MySQL table

Please help me to discover syntax error in my rename_table script. What i want is add date to the table name, but something goes wrong.
Now here's the code:
$date = date('d-m-Y');
$query = "RENAME order TO order".$date;
if(mysql_query($ren)){
...
You have to use backticks for order as it is a reserved keyword. Also you are executing the query wrongly.
if(mysql_query($ren))
^
Replace $ren with $query as your query is stored in a variable $query, not $ren..
So try with
$query = "RENAME TABLE `order` TO order".$date;
if(mysql_query($query))
change
$query = "RENAME order TO order".$date;
to
$query = "RENAME `order` TO `order".$date."`";
You cant use - sign as table name, use _ or dmy format 06nov2014
Try this
$date = date('d-m-Y');
$query = "RENAME `order` TO `order".$date."`";
if(mysql_query($ren))

Calling NOW() function in MySQL

I'm using php for making queries for mysql. Here is one:
UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = "$resEmail"
curDate - DateTime type. The problem is that after this query curDate of given email is
0000-00-00 00:00:00
What's wrong?
Your PHP probably looks like this now:
$sql = 'UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = "$resEmail"';
The single quotes prevent the variable's value from being substituted into the string. You will need to change it to this:
$sql = "UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = '$resEmail'";
You should also be aware that you may have an SQL injection vulnerability here. Consider using mysql_real_escape_string to escape the email address.
$sql = "UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = '" .
mysql_real_escape_string($resEmail) . "'";
Since you're using timedate function which is based on proper timestamp, try setting it with Timestamp(Now()).

Categories