I have the following:
public function update($table, $data, $wheredata)
{
ksort($data);
$fieldDetails = NULL;
$whereDetails = NULL;
foreach($data as $key=> $value) {
$fieldDetails .= "`$key`=:$key,";
}
$fieldDetails = rtrim($fieldDetails, ',');
$wcnt = count($wheredata);
echo "===".$wcnt."===";
$arrcount = $wcnt - 1;
$kount = -1;
foreach($wheredata as $keyw=> $valuew) {
$kount = $kount + 1;
if ($kount < $arrcount)
{
$whereDetails .= "`$keyw`=:$keyw" . " and ";
}
else
{
$whereDetails .= "`$keyw`=:$keyw,";
}
//$whereDetails .= "`$keyw`=:$keyw,";
}
$whereDetails = rtrim($whereDetails, ',');
$sth = $this->prepare("UPDATE $table SET $fieldDetails WHERE $whereDetails");
echo "UPDATE " . $table . " SET " . $fieldDetails . " WHERE " . $whereDetails;
echo "<br>";
foreach ($data as $key => $value) {
echo $key . "===" . strip_tags($value) . "<br>";
$sth->bindValue(":$key", self::fixstr($value)); //////////////////////////////////////this line////////////////////////////////////////////////////////
//$sth->bindValue(":$key", $this->fixstr($value));////////////////////////////////////////////////or this line///////////////////////////////////////////
}
foreach ($wheredata as $keyw => $valuew) {
$sth->bindValue(":$keyw", strip_tags($valuew));
}
$sth->execute();
}
and :
public function fixstr($rvalue) {
$this->rvalue = empty($rvalue) && !is_numeric($rvalue) ? NULL : trim(strip_tags($rvalue));
return $this->rvalue;
}
This works. But if I call the function using:
$sth->bindValue(":$key", $this->fixstr($value));
and change the function to:
public function fixstr($rvalue) {
$rvalue = empty($rvalue) && !is_numeric($rvalue) ? NULL : trim(strip_tags($rvalue));
return $rvalue;
}
This also works. Which is the correct way to call a function in the same class?
Just for info: I use this so 0000-00-00 doesn’t show in a date field that should be null.
But it works on all string/varchar fields.