Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/252741-insert-empty-fields-gives-errors/
Share on other sites

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;

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.