How use the function NOW() in PHP

柔情痞子 提交于 2019-12-24 07:32:55

问题


I have a temporary table SQL. I have a filed that give me the name of the user that modified any information by get_current_user. I added another field type=DATETIME to recovre the date and the hour of the user modifcation.

In PHPI added it like all my function, but it got an error. The code is the following:

         $sql = "INSERT INTO  `...`.`correspondants_bis` 
    (`code_client` , `name`, `surname` ,`phone`, `fax`, `email`, `added_by`, `Date`, `condition`)

        VALUES ('" . $correspondent->getCodeClient() . "',
     '" . str_replace("'", "''", $correspondent->getName()) . "',
     '" . str_replace("'", "''", $correspondent->getSurname()) . "',
     '" . $correspondent->getPhone() . "', 
    '" . $correspondent->getFax() . "',
     '" . $correspondent->getEmail() . "', 
    '" . $user . "','" . NOW() . "',
    '1'
        );";

the error is:

Fatal error: Call to undefined function..... 
Attempted to call function "NOW" from namespace....

I know that NOW() is an SQL function, just I got you an example of insert because I don't know how I add it, and this following my select function:

 public function getCorrespondentByIdBis($id) {
        $statement = "SELECT * FROM `correspondants_bis` WHERE `id`='" . $id . "'";
        $result = $this->_db->query($statement);
        while ($data = $result->fetch()) {
            $correspondent = new CorrespondentBis($data['id'], $data['code_client'], $data['name'], $data['surname'], $data['phone'], $data['fax'], $data['email'], get_current_user(), NOW(), 0);
        }
       }
return $correspondent;
} 

Can you tell me please, how I can change my code to resolve this issue ?


回答1:


now is not php function so replace

NOW()

to

date('Y-m-d H:i:s') 

so your final code

$correspondent = new CorrespondentBis($data['id'], $data['code_client'], $data['name'], $data['surname'], $data['phone'], $data['fax'], $data['email'], get_current_user(), date('Y-m-d H:i:s'), 0);

2.no need to use quote , just use NOW() so your final query

$sql = "INSERT INTO  `...`.`correspondants_bis` 
     (`code_client` , `name`, `surname` ,`phone`, `fax`, `email`, `added_by`, `Date`, `condition`)

     VALUES ('" . $correspondent->getCodeClient() . "',
     '" . str_replace("'", "''", $correspondent->getName()) . "',
     '" . str_replace("'", "''", $correspondent->getSurname()) . "',
     '" . $correspondent->getPhone() . "', 
     '" . $correspondent->getFax() . "',
     '" . $correspondent->getEmail() . "', 
     '" . $user . "',
      NOW(),
     '1'
      );";

for more information

http://www.mysqltutorial.org/mysql-now/




回答2:


NOW() is a SQL function, so you don't need it in PHP:

$sql = "INSERT INTO  `...`.`correspondants_bis` 
    (`code_client` , `name`, `surname` ,`phone`, `fax`, `email`, `added_by`, `Date`, `condition`)

        VALUES ('" . $correspondent->getCodeClient() . "',
     '" . str_replace("'", "''", $correspondent->getName()) . "',
     '" . str_replace("'", "''", $correspondent->getSurname()) . "',
     '" . $correspondent->getPhone() . "', 
    '" . $correspondent->getFax() . "',
     '" . $correspondent->getEmail() . "', 
    '" . $user . "',NOW(),
    '1'
        );";

Sidenote: Building your SQL statements in this way, makes you susceptible to SQL injections. Better use prepared statements.



来源:https://stackoverflow.com/questions/44802996/how-use-the-function-now-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!