问题
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 PHP
I 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