Php 如何在查询中使用函数值

Php 如何在查询中使用函数值,php,html,mysql,mysqli,Php,Html,Mysql,Mysqli,我试图在我的一个查询中使用time函数…我有如下查询,用于在我的一个DATETIME字段中添加自定义时区时间 $upd_qry = "update tbl_quotes set qu_status='".$_GET['status']."', qu_time= $getDatetimeNow where _quid='".$_GET['quotes_id']."'"; $result=mysqli_query(

我试图在我的一个查询中使用time函数…我有如下查询,用于在我的一个DATETIME字段中添加自定义时区时间

$upd_qry = "update tbl_quotes 
                set qu_status='".$_GET['status']."', qu_time= $getDatetimeNow  
                where _quid='".$_GET['quotes_id']."'";
        $result=mysqli_query($mysqli,$upd_qry);
使用自定义时区获取时间的函数如下所示

function getDatetimeNow() {
        $tz_object = new DateTimeZone('Brazil/East');
        //date_default_timezone_set('Brazil/East');

     $datetime = new DateTime();
        $datetime->setTimezone($tz_object);
        return $datetime->format('Y\-m\-d\ h:i:s');
        }
但我的问题不起作用…这有什么错


谢谢

您需要将返回的函数值分配给另一个变量:

$getDatetimeNow = getDatetimeNow();

$upd_qry = "update tbl_quotes 
set qu_status='".$_GET['status']."', qu_time=".$getDatetimeNow." 
where _quid='".$_GET['quotes_id']."'";
$result=mysqli_query($mysqli,$upd_qry);
或者直接调用它—如果您知道不需要在其他地方使用它的值—如下所示:

"update tbl_quotes
set qu_status='".$_GET['status']."', qu_time=".getDatetimeNow()." 
where _quid='".$_GET['quotes_id']."'"
更新: 您迫切需要使用语句,因为您的代码易受以下攻击:


不能使用变量之类的函数。函数与参数一起使用。这意味着它们必须与变量容器一起使用,如$variable、$anotherVariable

所有包含但不包含参数的函数都需要此符号

$updateQuery = "UPDATE table_name SET field_name = " . functionName() . " WHERE id='" . (int) $_GET['id'] . "'";

$result = mysqli_query($mysqli, $updateQuery);

将查询添加到一个隐藏的html元素中,如See about parametrised querys you's Right…非常感谢:虽然这个答案可能解决OP的问题,但仅仅发布代码答案可能对OP或未来用户没有帮助。请详细说明您的答案,并提供一个清晰的解释,说明如何帮助OP实现他们所追求的解决方案。
$updateQuery = "UPDATE table_name SET field_name = " . functionName() . " WHERE id='" . (int) $_GET['id'] . "'";

$result = mysqli_query($mysqli, $updateQuery);