Re: entering null value to mysql

From: Date: Sat, 07 Oct 2000 19:42:17 +0000
Subject: Re: entering null value to mysql
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-3397@lists.php.net to get a copy of this message
Build your SQL statement like this: $sql = "insert into " . $table_name; $sql .= " (id_number,name,date,goal_1,goal_2,goal_3,goal_4,wu) values ("; if (strlen($id_number) > 0) $sql .= "'" . $id_number . "', "; else $sql .= "NULL, "; if (strlen($name) > 0) $sql .= "'" . $name . "', "; else $sql .= "NULL, "; if (strlen($date) > 0) $sql .= "'" . $date . "', "; else $sql .= "NULL, "; if (strlen($goal_1) > 0) $sql .= "'" . $goal_1 . "', "; else $sql .= "NULL, "; if (strlen($goal_2) > 0) $sql .= "'" . $goal_2 . "', "; else $sql .= "NULL, "; if (strlen($goal_3) > 0) $sql .= "'" . $goal_3 . "', "; else $sql .= "NULL, "; if (strlen($goal_4) > 0) $sql .= "'" . $goal_4 . "', "; else $sql .= "NULL, "; if (strlen($wu) > 0) $sql .= "'" . $wu . "'"; else $sql .= "NULL"; $sql .= ")"; The solution has absolutely nothing to do with the addslashes function. However, addslashes should be used on any value that may be input by a user (and may contain characters special to SQL). Also, whenever you show data onto a webpage that has come in from an uncontrolled source (such as an input type="text" field on a webpage), you'll want to show it only after passing it through htmlspecialchars(). But that's a different topic. Doug At 10:06 AM 10/7/00 -0400, Scott M Wagner wrote: >Hello >I'm having a problem entering null values into mysql from a php script. >I'm that the solution has something to do with the addslashes function >since null values must be entered into mysql without quotes. So far >nothing has worked though. Here is one of my failed attempts: >$goal_1 = addslashes($goal_1); >$goal_2 = addslashes($goal_2); >$goal_3 = addslashes($goal_3); >$goal_3 = addslashes($goal_4); >$wu = addslashes($wu); >$sql = " >insert $table_name (id_number,name,date,goal_1,goal_2,goal_3,goal_4,wu) >values('$id_number','$name','$date','$goal_1','$goal_2','$goal_3', > 'goal_4','$wu') >"; >Any help would be appreciated. >Thanks, >Scott Wagner

« previous php.db (#3397) next »