如果使用空字段提交表单,则将预定义值更新到mysql数据库中

时间:2013-02-13 16:02:11

标签: php mysql forms

语法

$default_message = "No food name entered";
    if(isset($_POST['food_name']) && !empty($_POST['food_name'])){  
       echo $_POST['$dafult_message'];
    }

应检查数据库中的字段food_name是空还是空,并在使用空字段更新以下表单时尝试将预定义值No food name entered插入数据库但不幸的是它不会更新预定义的值。我看不出任何不合逻辑的东西。任何想法?


if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "formX")) { 

    $default_message = "No food name entered";
    if(isset($_POST['food_name']) && !empty($_POST['food_name'])){  
       echo $_POST['$dafult_message'];
    }

$updateSQL = sprintf("UPDATE foodtable SET food_name=%s WHERE food_id=%s",    
                       GetSQLValueString($_POST['food_name'], "text"),
                       GetSQLValueString($_POST['food_id'], "int"));

  mysql_select_db($database_XYZ, $XYZ);
  $Result1 = mysql_query($updateSQL, $XYZ) or die(mysql_error());

  $updateGoTo = "choicefood.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
      }
  header(sprintf("Location: %s", $updateGoTo));
    exit ();
}  

<form action="<?php echo $editFormAction; ?>" method="post" name="formX" id="formX">
<table align="center">
  <tr valign="baseline">
    <td align="right" nowrap="nowrap">Update Food Name:</td>
    <td><input type="text" name="food_name" value="" size="32" /></td>
  </tr>
   <tr valign="baseline">
    <td nowrap="nowrap" align="right">
     <input type="hidden" name="MM_update" value="formX" />
      <input type="hidden" name="food_id" value="<?php echo $query['food_id']; ?>" />
     </td>
                    <td>
                    <input type="submit" value="Update Food Name" />
                    </td>
                  </tr>
                </table>
              </form>

感谢,

2 个答案:

答案 0 :(得分:0)

试试这个

    $default_message = "No food name entered";

    if ( @$_POST['food_name'] == '' ) {

        $_POST['food_name'] = $default_message;

    }

    echo '<p>', $_POST['food_name'], '</p>;

答案 1 :(得分:0)

您的代码应该按照您的意图运行,但您在这行代码中拼错了"$default_message"

echo $_POST['$dafult_message'];