duan1930 2012-10-24 10:54
浏览 82

如果表单字段为空,则将替代值插入到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?

  • 写回答

1条回答 默认 最新

  • douye4254 2012-10-24 11:30
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集