pakenney38 Posted December 21, 2007 Share Posted December 21, 2007 Say I've got a form variable set to a new variable like so: $newvar = $_POST['newvar']; If the newvar form field contained no value, is there any way to prevent PHP from sending $newvar as '' in a MYSQL statement and instead send a NULL value for $newvar? Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/ Share on other sites More sharing options...
fenway Posted December 21, 2007 Share Posted December 21, 2007 It depends on your INSERT statement... if you're blindly quoting this value, you're stuck. You'd need to check in in-line... could you post just the relevant code snippet (NOT the whole file)? Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420689 Share on other sites More sharing options...
Cagecrawler Posted December 21, 2007 Share Posted December 21, 2007 <?php $newvar = ($_POST['newvar'] == '')? NULL : $_POST['newvar']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420701 Share on other sites More sharing options...
fenway Posted December 21, 2007 Share Posted December 21, 2007 Actually, you'd still need to quote the non-NULL value, so that's not it. Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420703 Share on other sites More sharing options...
pakenney38 Posted December 21, 2007 Author Share Posted December 21, 2007 Well ideally, I would like to just do something like $query = "INSERT INTO table (blah1, blah2, blah3, newvar) VALUES ($blah1, $blah2, $blah3, $newvar)"; mysql_query($query); But if the form field associated with $newvar was not filled in, I would like it to not submit a value to MySQL. If there is some easy way of doing this without going through the whole if ($newvar != '') before I run my query, then I would like to know how to do that. I suppose I could just use $_POST['newvar'] in my MySQL query, but in my situation, I can't always do it that way. Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420704 Share on other sites More sharing options...
pakenney38 Posted December 21, 2007 Author Share Posted December 21, 2007 <?php $newvar = ($_POST['newvar'] == '')? NULL : $_POST['newvar']; ?> Wow that is interesting, but what does it mean? Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420710 Share on other sites More sharing options...
pakenney38 Posted December 21, 2007 Author Share Posted December 21, 2007 I guess this is some sort of case check kinda like if ($_POST['newvar'] == '') {$newvar == NULL;} else {$newvar == $_POST['newvar'];} Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420716 Share on other sites More sharing options...
fenway Posted December 21, 2007 Share Posted December 21, 2007 This should work: $query = "INSERT INTO table (blah1, blah2, blah3, newvar) VALUES ($blah1, $blah2, $blah3, ( $_POST['newvar'] == '' ? NULL : $newvar) )"; But why aren't you quoting your strings? Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420721 Share on other sites More sharing options...
pakenney38 Posted December 21, 2007 Author Share Posted December 21, 2007 Oh I just quickly wrote that as an example. Sorry. It's quoted in the actual project. Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420726 Share on other sites More sharing options...
pakenney38 Posted December 21, 2007 Author Share Posted December 21, 2007 This is obviously my first exposure to the ternary operator in PHP, but I love it. What I needed was something like this: $newvar = (empty($_POST['newvar'])) ? NULL : $_POST['newvar']; Just knowing that the ternary operator exists is going to make my life so much easier. Quote Link to comment https://forums.phpfreaks.com/topic/82715-solved-make-blank-form-fields-always-send-null-to-mysql/#findComment-420728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.