php - COALESCE and str_replace -
i'm using coalesce update not empty input form sql db , works fine :
$horiz_deux = $_post["horiz_deux"]; $horiz_gauche = $_post["horiz_gauche"]; other ones ... mysql_query("update inscription set horiz_deux = coalesce('$horiz_deux', horiz_deux) id = $dossard"); mysql_query("update inscription set horiz_gauche = coalesce('$horiz_gauche', horiz_gauche) id = $dossard"); other updates... but when i'm trying . replace ,
$horiz_deux = str_replace(".", ",",$_post["horiz_deux"]); it update other input well.
any idea ?
i think because of function http://php.net/manual/en/function.str-replace.php returning string or array. value of $horiz_gauche diffrent null , therefore selected coalesce-statement causing update horiz_gauche in database.
you try , print out value of $horiz_gauche before insert sql-statement confirm contents.
you try:
$horiz_deux = isset($_post["horiz_deux"]) ? $_post["horiz_deux"] : ''; if ($horiz_deux != '') { $horiz_deux = str_replace(".", ",",$horiz_deux); mysql_query("update inscription set horiz_deux = coalesce('$horiz_deux', horiz_deux) id = $dossard"); } this quite cumbersome, write loop iterates on input-fields , checks them valid input:
$myfieldnames = array('horiz_deux','horiz_hauche','something_else'); foreach ($myfieldnames $fieldname) { $value = isset($_post[$fieldname]) ? $_post[$fieldname] : ''; $value = str_replace(".", ",",$value); mysql_query("update inscription set $fieldname = coalesce('$value', $fieldname) id = $dossard"); }
Comments
Post a Comment