如果表单字段为空,则将替代值插入到mysql数据库中

The following form collects data about the color preference of the visitors.

how to insert a predefined alternative value into mysql database if the users submit the blank form.

the thing is to consider here is i don't want to prevent user from submitting the blank form using form validation.

my present backend codes are as following:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO users (color_name) VALUES (%s)",


   GetSQLValueString($_POST['color_name'], "text")),

  mysql_select_db($database_x, $x);
  $Result1 = mysql_query($insertSQL, $x) or die(mysql_error());

  $insertGoTo = "choicecolor.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
  <tr valign="baseline">
    <td align="right" nowrap="nowrap">Enter Color Name:</td>
    <td><input type="text" name="color_name" value="" size="32" /></td>
  </tr>
   <tr valign="baseline">
    <td nowrap="nowrap" align="right"><input type="hidden" name="MM_insert" value="form1" /></td>
                    <td>
                    <input type="submit" value="Submit Color Name" />
                    </td>
                  </tr>
                </table>
              </form>
              </td>
            </tr>
      </table>
</form>

if the visitors submit a blank form without entering any value, a predefined value (for example, "no choice") shall be inserted into database and if the visitors submit a filled form with the inputted value "yellow", the value "yellow", shall be inserted into database.

how to predefine the value in this case using PHP?

to do this in php add something like this

$toInsert = isset($_POST['color_name'])? $_POST['color_name']: 'Your default string';

and then alter this line

$insertSQL = sprintf("INSERT INTO users (color_name) VALUES (%s)", GetSQLValueString($_POST['color_name'], "text"));

to this

$insertSQL = sprintf("INSERT INTO users (color_name) VALUES (%s)", GetSQLValueString($toInsert, "text"));

but you should really be looking at PDO etc first. Also as Dark Wish mentioned it might be better to do this at the database level rather than with php.