In my MySQL database, I have a table with structure
在我的MySQL数据库中,我有一个具有结构的表
username - varchar
insert_time - timestamp
This table was created in MySQL using the phpMyAdmin tool and for the insert_time
column, I have mentioned default value as 0000-00-00 00:00:00
.
这个表是使用phpadmin工具在MySQL中创建的,对于insert_time列,我提到默认值为000000 -00 00:00。
Now the problem is, I have to update this default value with the current timestamp later on, using a PHP script.
现在的问题是,稍后我必须使用PHP脚本使用当前时间戳更新这个默认值。
I tried doing the following PHP code:
我尝试了以下PHP代码:
$update_query = 'UPDATE db.tablename SET insert_time=now() '.
'WHERE username='.$somename;
When the PHP script is run, it fails, and is unable to insert anything into the database.
当PHP脚本运行时,它将失败,并且无法向数据库中插入任何内容。
What am I doing wrong?
我做错了什么?
5 个解决方案
#1
50
What error message are you getting?
您将得到什么错误消息?
I'd guess your actual error is because your php variable isn't wrapped in quotes. Try this
我猜你的实际错误是因为你的php变量不是用引号括起来的。试试这个
$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='" .$somename . "'";
#2
13
Your usage of now() is correct. However, you need to use one type of quotes around the entire query and another around the values.
您对now()的用法是正确的。但是,需要在整个查询中使用一种引号,在值中使用另一种引号。
You can modify your query to use double quotes at the beginning and end, and single quotes around $somename
:
您可以修改查询,使其在开头和结尾使用双引号,在$somename附近使用单引号:
$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='$somename'";
#3
7
Forgot to put the variable in the sql statement without quotations.
忘记在sql语句中不使用引号。
$update_query =
"UPDATE db.tablename SET insert_time=NOW() WHERE username='" .$somename."'";
#4
4
This format is used to get current timestamp and stored in mysql
此格式用于获得当前时间戳并存储在mysql中。
$date = date("Y-m-d H:i:s");
$update_query = "UPDATE db.tablename SET insert_time=".$date." WHERE username='" .$somename . "'";
#5
1
Don't like any of those solutions.
不喜欢这些解决方案。
this is how i do it:
我就是这样做的:
$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='"
. sqlEsc($somename) . "' ;";
then i use my own sqlEsc function:
然后我使用我自己的sqlEsc函数:
function sqlEsc($val)
{
global $mysqli;
return mysqli_real_escape_string($mysqli, $val);
}
#1
50
What error message are you getting?
您将得到什么错误消息?
I'd guess your actual error is because your php variable isn't wrapped in quotes. Try this
我猜你的实际错误是因为你的php变量不是用引号括起来的。试试这个
$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='" .$somename . "'";
#2
13
Your usage of now() is correct. However, you need to use one type of quotes around the entire query and another around the values.
您对now()的用法是正确的。但是,需要在整个查询中使用一种引号,在值中使用另一种引号。
You can modify your query to use double quotes at the beginning and end, and single quotes around $somename
:
您可以修改查询,使其在开头和结尾使用双引号,在$somename附近使用单引号:
$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='$somename'";
#3
7
Forgot to put the variable in the sql statement without quotations.
忘记在sql语句中不使用引号。
$update_query =
"UPDATE db.tablename SET insert_time=NOW() WHERE username='" .$somename."'";
#4
4
This format is used to get current timestamp and stored in mysql
此格式用于获得当前时间戳并存储在mysql中。
$date = date("Y-m-d H:i:s");
$update_query = "UPDATE db.tablename SET insert_time=".$date." WHERE username='" .$somename . "'";
#5
1
Don't like any of those solutions.
不喜欢这些解决方案。
this is how i do it:
我就是这样做的:
$update_query = "UPDATE db.tablename SET insert_time=now() WHERE username='"
. sqlEsc($somename) . "' ;";
then i use my own sqlEsc function:
然后我使用我自己的sqlEsc函数:
function sqlEsc($val)
{
global $mysqli;
return mysqli_real_escape_string($mysqli, $val);
}