June 29, 200818 yr Here I am trying to enter the 'price'. If no price is entered I want the table value inputted to be NULL not 0.00 (which is what it is doing no matter what I have tried. The tables 'price' column is set to DECIMAL(6,2) NULL. if (preg_match ('/^\d\.{1,5}$/', $trimmed['price'])) { $pr = mysqli_real_escape_string ($dbc, $trimmed['price']); } else { $pr = mysqli_real_escape_string ($dbc, "NULL"); After the 'else' is not working ... can anyone help?
July 1, 200818 yr I would do it like this: <?php if(!empty($_POST)){ if(!isset($_POST['price'])){ mysql_query("INSERT INTO db_name (price) VALUES('NULL')")or die(mysql_error()); }else{ mysql_query("INSERT INTO db_name (price) VALUES('".$_POST['price']."')")or die(mysql_error()); } }else{ echo"<form method=post action=".$_SERVER['PHP_SELF']."> Price: <input type=text name=price> <input type=submit value=Submit!></form>"; } ?> Maybe I have a typo here, but that's basically what I'd do.
July 1, 200818 yr As long as the default is set in Mysql to NULL, you can insert in by using price = ''.
July 1, 200818 yr Good point, jamest. If the default is NULL you can use this script: <?php if(!empty($_POST)){ mysql_query("INSERT INTO db_name (price) VALUES('".$_POST['price']."')")or die(mysql_error()); }else{ echo"<form method=post action=".$_SERVER['PHP_SELF']."> Price: <input type=text name=price> <input type=submit value=Submit!></form>"; } ?>
July 1, 200818 yr Good point, jamest. If the default is NULL you can use this script: <?php if(!empty($_POST)){ mysql_query("INSERT INTO db_name (price) VALUES('".$_POST['price']."')")or die(mysql_error()); }else{ echo"<form method=post action=".$_SERVER['PHP_SELF']."> Price: <input type=text name=price> <input type=submit value=Submit!></form>"; } ?> No you shouldn't. You should never put any $_GET or $_POST data straight in to the database. It is not secure. You need to do some prior validation to make sure it is safe to put in the database.
July 4, 200818 yr also 'null' is the string null either use: update `table` set `field` = '' or update `table` set `field` = null just not update `table` set `field` = 'null'
Create an account or sign in to comment