dmhall0 Posted December 8, 2011 Share Posted December 8, 2011 I have a form with a series of text boxes where the User enters specific data, be it text, numeric, or date/time. It is not mandatory to fill in every box, but when I submit the form to be put into a mysql table, I get errors such as "Incorrect time value: '' for column 'starttime' at row 1" "Incorrect decimal value: '' for column 'workkj' at row 1" How can I setup the mysql table to accept blank answers? Or is there something I need to do in php to fix this? Sorry if this is simple... I'm still learning. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/ Share on other sites More sharing options...
The Little Guy Posted December 8, 2011 Share Posted December 8, 2011 can you show your query? Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1295772 Share on other sites More sharing options...
dmhall0 Posted December 8, 2011 Author Share Posted December 8, 2011 Here is the code: // Add workout to the database if (isset($_POST['submit'])) { // Grab the data from POST $username = $_SESSION['username']; $wotype = cleaninput($dbc, $_POST['wotype']); $wodate = isset($_REQUEST["wodate"]) ? $_REQUEST["wodate"] : ""; $woname = cleaninput($dbc, $_POST['woname']); $bikename = cleaninput($dbc, $_POST['bikename']); $starttime = cleaninput($dbc, $_POST['starttime']); $duration = cleaninput($dbc, $_POST['duration']); $distance = cleaninput($dbc, $_POST['distance']); $workkj = cleaninput($dbc, $_POST['workkj']); $spdavg = cleaninput($dbc, $_POST['spdavg']); $spdmax = cleaninput($dbc, $_POST['spdmax']); $hravg = cleaninput($dbc, $_POST['hravg']); $hrmax = cleaninput($dbc, $_POST['hrmax']); $cadavg = cleaninput($dbc, $_POST['cadavg']); $cadmax = cleaninput($dbc, $_POST['cadmax']); $pwravg = cleaninput($dbc, $_POST['pwravg']); $pwrmax = cleaninput($dbc, $_POST['pwrmax']); $temp = cleaninput($dbc, $_POST['temp']); $terrain = cleaninput($dbc, $_POST['terrain']); $weather = cleaninput($dbc, $_POST['weather']); $notes = cleaninput($dbc, $_POST['notes']); if (!empty($wodate) && !empty($duration) && !empty($distance)) { $query = "INSERT INTO u_rides (username, wotype, wodate, woname, bikename, starttime, " . " duration, distance, workkj, spdavg, spdmax, hravg, hrmax, cadavg, cadmax, pwravg, pwrmax, " . " terrain, temp, weather, notes) " . " VALUES ('$username', '$wotype', '$wodate', '$woname', '$bikename', '$starttime', " . " '$duration', '$distance', '$workkj', '$spdavg', '$spdmax', '$hravg', '$hrmax', '$cadavg', '$cadmax', '$pwravg', '$pwrmax', " . " '$terrain', '$temp', '$weather', '$notes')"; mysqli_query($dbc, $query) or die('Connection Error: '.mysqli_error($dbc)); Here is my cleaninput function: function cleaninput($dbc, $var) { $var=stripslashes($var); $var=htmlentities($var); $var=strip_tags($var); $var=mysqli_real_escape_string($dbc, $var); $var=trim($var); return $var; Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1295833 Share on other sites More sharing options...
fenway Posted December 8, 2011 Share Posted December 8, 2011 Funny, that's code -- not the query. Echo $query. Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1295879 Share on other sites More sharing options...
dmhall0 Posted December 8, 2011 Author Share Posted December 8, 2011 Ha... sorry. Here is the query. INSERT INTO u_rides (username, wotype, wodate, woname, bikename, starttime, duration, distance, workkj, spdavg, spdmax, hravg, hrmax, cadavg, cadmax, pwravg, pwrmax, terrain, temp, weather, notes) VALUES ('dhall', 'Cycling', '2011-12-08', 'Test Ride', 'Tarmac SL', '3:34', '3:23', '45', '', '', '', '', '', '', '', '', '', '', '', '', '') So I need it to accept the blanks, or fill them with something I guess. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1295945 Share on other sites More sharing options...
fenway Posted December 8, 2011 Share Posted December 8, 2011 You need to fill them with the default values -- which you can do, just use the literal DEFAULT (no quotes). Or, better yet, don't include them in the column list, and then mysql will do it for you. Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1295963 Share on other sites More sharing options...
dmhall0 Posted December 8, 2011 Author Share Posted December 8, 2011 So how do I write the query to include them when there is a value, yet not include them if there isn't? Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1295992 Share on other sites More sharing options...
fenway Posted December 9, 2011 Share Posted December 9, 2011 So how do I write the query to include them when there is a value, yet not include them if there isn't? A hash would be the easiest way. Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1296046 Share on other sites More sharing options...
dmhall0 Posted December 9, 2011 Author Share Posted December 9, 2011 I am sorry but I don't follow. How would I get the hash in the variable if the User leaves it blank? Sorry if this is all elementary mysql stuff. Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1296048 Share on other sites More sharing options...
fenway Posted December 9, 2011 Share Posted December 9, 2011 For each non-blank value, assign a new key to the hash. Then simply go through the keys -- the field names -- to build your query string. Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1296227 Share on other sites More sharing options...
dmhall0 Posted December 9, 2011 Author Share Posted December 9, 2011 Sorry but I am still not following. Could I see an example? Please don't hate me! ha Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/#findComment-1296234 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.