带有条件字段的PHP表单

时间:2015-04-02 18:10:51

标签: javascript php jquery mysql forms

我正在尝试创建一个PHP表单,将记录插入到记录具有条件开始和结束日期的数据库中。我有两个单选按钮设置,您可以选择:

  1. 无结束日期
  2. 选择一系列日期和时间选项,这些选项分为开始日期和结束日期和时间。
  3. 我设置了Javascript,因此默认情况下您只能看到第一个选项。如果选择第二个单选按钮,则会显示其他字段。当您点击提交按钮时,会出现以下错误:

      

    列数与第1行的值计数不匹配

    有谁能告诉我标记中出现了什么问题?

    以下是该页面的代码:

    <?php require_once('../Connections/mysqlconnect.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO keywords (id, station, keyword, url, target, start_date, end_date) VALUES (%s, %s, %s, %s, %s, %s)",
                           GetSQLValueString($_POST['id'], "int"),
                           GetSQLValueString($_POST['station'], "text"),
                           GetSQLValueString($_POST['keyword'], "text"),
                           GetSQLValueString($_POST['url'], "text"),
                           GetSQLValueString($_POST['target'], "text"),
                           GetSQLValueString($_POST['unlimited'], "text"),
                           GetSQLValueString(date("Y-m-d H:i:s", strtotime($_POST['startDay'] . $_POST['start-hour'] . ':' . $_POST['start-minute'] . $_POST['start-meridian'])), "date"),
                           GetSQLValueString(date("Y-m-d H:i:s", strtotime($_POST['endDay'] . $_POST['end-hour'] . ':' . $_POST['end-minute'] . $_POST['end-meridian'])), "date"));
    
      mysql_select_db($database_mysqlconnect, $mysqlconnect);
      $Result1 = mysql_query($insertSQL, $mysqlconnect) or die(mysql_error());
    
      $insertGoTo = "manage.php?mess=3";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
    
    mysql_select_db($database_mysqlconnect, $mysqlconnect);
    $query_Recordset1 = "SELECT * FROM keywords";
    $Recordset1 = mysql_query($query_Recordset1, $mysqlconnect) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 = mysql_num_rows($Recordset1);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Ad Inventory:: Add New Campaign</title>
    
            <script type="text/javascript" src="http://yourligas.yourli.com/datepicker/jquery-ui-1.10.4.custom/js/jquery-1.10.2.js"></script>
            <script type="text/javascript" src="http://yourligas.yourli.com/datepicker/jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.js"></script>
            <script type="text/javascript" src="http://yourligas.yourli.com/datepicker/jquery-ui-1.10.4.custom/js/jquery-ui-.custom.min.js"></script>
            <script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="http://yourligas.yourli.com/datepicker/jquery-ui-1.10.4.custom/css/ui-lightness/jquery-ui-1.10.4.custom.css">
            <link rel="stylesheet" type="text/css" media="screen" href="http://yourligas.yourli.com/datepicker/jquery-ui-1.10.4.custom/css/ui-lightness/jquery-ui-1.10.4.custom.min.css">
            <script>
      $(function() {
        $( "#startdatepicker" ).datepicker({
          showOn: "button",
          buttonImage: "http://yourligas.yourli.com/datepicker/jquery-ui-1.10.4.custom/development-bundle/demos/images/calendar.gif",
          buttonImageOnly: true,
          changeMonth: true,
          changeYear: true
        });
      });
      $(function() {
        $( "#enddatepicker" ).datepicker({
          showOn: "button",
          buttonImage: "http://yourligas.yourli.com/datepicker/jquery-ui-1.10.4.custom/development-bundle/demos/images/calendar.gif",
          buttonImageOnly: true,
          changeMonth: true,
          changeYear: true
        });
      });
      </script>
    
    
    
    <style>
    html,body{font-family:Arial, Helvetica, sans-serif;font-size:14px;margin:0 auto;background:#EFEFEF;}
    h1{margin-left:0;font-size:36px;}
    a{text-decoration:none;color:#036;font-weight:bold;font-size:16px;}
    a:visited{color:#036}
    td{
        font-family: Arial, Helvetica, sans-serif;
        color: #585858;
    }
    tr.heading, .heading a{color:#fff; font-size:14px;font-weight:bold}
    tr.heading td{background:#036!important;color:#ffffff;}
    tr.alt td {
        background-color: #B9DCFF;
    }
    tr td {
        background-color: #D7EBFF;padding:10px;
    }
    .nav{list-style:none;margin:0;padding:0;text-align:right;}
    .nav li{display:inline-block;padding:0 10px;}
    .confirm-message{
        width:800px;
        margin:0 auto;
        border:1px solid #004A0D;
        padding:15px;
        text-align:center;
        background-color: #B9FFC6;
    }
    .error{
        color:#ff0000;
        font-style:italic;
    }
    </style>
    
    <link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
     <p><h1 align="center" style="font-size:30px;">CONNOISSEUR LI:: Keyword Management</h1></p>
    <p>&nbsp;</p>
    <?php
    // define variables and set to empty values
    $campaignErr = $startdateErr = $enddateErr = "";
    $campaign = $startdate = $enddate = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    
      if (empty($_POST["campaign"]))
        {$campaignErr = "Please Specify a Campaign Name!";}
      else
        {$campaign = test_input($_POST["campaign"]);}
    
      if (empty($_POST["startDay"]))
        {$startdateErr = "Please Specify a Start Date!";}
      else
        {$startdate = test_input($_POST["startDay"]);}
    
      if (empty($_POST["endDay"]))
        {$enddateErr = "Please Specify an End Date!";}
      else
        {$enddate = test_input($_POST["endDay"]);}
    
    }
    
    ?>
    
    <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1" style="font-size:12px;">
      <table align="center" border="0" cellpadding="10" cellspacing="0" style="margin:0 auto;">
        <tr valign="middle">
          <td nowrap="nowrap" align="right">Station:</td>
          <td>
          <select name="station" style="font-size:12px;">
            <option value="WBZO" selected="selected">WBZO</option>
            <option value="WHLI">WHLI</option>
            <option value="WKJY">WKJY</option>
            <option value="WALK">WALK</option>
            <option value="WWSK">WWSK</option>
          </select></td>
        </tr>
        <tr valign="middle">
          <td nowrap="nowrap" align="right">Keyword:</td>
          <td><span id="sprytextfield1">
            <input type="text" name="keyword" value="" size="62" required="required" style="font-size:12px;"  />
            <span class="textfieldRequiredMsg">Please specify a keyword!</span></span>
          </td>
        </tr>
        <tr valign="middle">
          <td nowrap="nowrap" align="right">URL:</td>
          <td><span id="sprytextfield1">
            <input type="text" name="url" value="" size="62" required="required" style="font-size:12px;" maxlength="2083"  />
            <span class="textfieldRequiredMsg">Please specify a URL!</span></span>
          </td>
        </tr>
        <tr valign="middle">
          <td nowrap="nowrap" align="right"></td>
          <td>
          <input name="target" type="checkbox" value="blank" /> Open Link in New Window?
    
                </td>
        </tr>
    <tr valign="middle">
    <td></td>
    <td>
    
    
    
    <div>
    
        <input type="radio" name="schedule" value="true" class="schedule" checked="checked" /> No End Date Yet<br /><br />
    
        <input type="radio" name="schedule" value="false" class="schedule" /> Schedule a Start and End Date
    </div>
    
     </td>
    </tr>
    <tr valign="middle" id="start-schedule">
    <td>Start Date:</td>
    <td>
     <span id="sprytextfield2">
    <input name="startDay" id="startdatepicker" style="font-size:12px;" /><style>.ui-datepicker-trigger{position:relative;left:3px;top:2px;}</style>
    &nbsp;&nbsp;@&nbsp;
    <?php
    echo "<select name=\"start-hour\" style='width:50px'>";
    $i = 1;
    while ( $i <= 12 ) {
       $i = sprintf("%02d",$i);
       echo "<option value=".$i." selected='12'>".$i."</option>";
       $i++;
    }
    echo "</select>";
    echo ":&nbsp;";
    echo "<select name=\"start-minute\" style='width:50px'>";
    $i = 0;
    while ( $i <= 59 ) {
       $i = sprintf("%02d",$i);
       echo "<option value=".$i.">".$i."</option>";
       $i++;
    }
    echo "</select>";
    
    ?>
    
    
    &nbsp;
    <select name="start-meridian" style="width:50px;font-size:12px;"><option value="am" selected="selected">AM</option><option value="pm">PM</option></select>
    <span class="textfieldRequiredMsg">Please specify a start date!</span>
    <span class="textfieldInvalidFormatMsg">Invalid date format. Please specify your date as mm/dd/yyyy!</span>
    </span>
    
    </td>
    </tr>
    <tr valign="middle" id="end-schedule">
    <td>End Date:</td>
    <td>
    <span id="sprytextfield3">
    <input name="endDay" id="enddatepicker" style="font-size:12px;" /><style>.ui-datepicker-trigger{position:relative;left:3px;top:2px;}</style>
    &nbsp;&nbsp;@&nbsp;
    <select name="end-hour" style="width:50px">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11" selected="selected">11</option>
    <option value="12">12</option>
    </select>
    
    <?php
    echo ":&nbsp;";
    echo "<select name=\"end-minute\" style='width:50px'>";
    $i = 0;
    while ( $i <= 59 ) {
       $i = sprintf("%02d",$i);
       echo "<option value=".$i." selected=\"59\">".$i."</option>";
       $i++;
    }
    echo "</select>";
    
    ?>
    
    
    &nbsp;
    <select name="end-meridian" style="width:50px;font-size:12px;"><option value="am">AM</option><option value="pm" selected="selected">PM</option></select>
    <span class="textfieldRequiredMsg">Please specify an end date!</span>
    <span class="textfieldInvalidFormatMsg">Invalid date format. Please specify your date as mm/dd/yyyy!</span>
    </span>
    
    
    </td>
    </tr>
        <tr valign="baseline">
          <td nowrap="nowrap" align="right">&nbsp;</td>
          <td align="right"><input type="submit" value="Insert" style="font-size:14px;padding:3px 5px;" /></td>
        </tr>
      </table>
      <input type="hidden" name="id" value="" />
      <input type="hidden" name="MM_insert" value="form1" />
    </form>
    <p>&nbsp;</p>
    <p align="center"><b><a href="javascript: history.go(-1)">&crarr; Cancel</a></b></p>
    
    <script>
    $(document).ready(function(){
        $("#start-schedule").css("display","none");
            $(".schedule").click(function(){
            if ($('input[name=schedule]:checked').val() == "false" ) {
                $("#start-schedule").slideDown("fast"); //Slide Down Effect
            } else {
                $("#start-schedule").slideUp("fast");  //Slide Up Effect
            }
         });
        $("#end-schedule").css("display","none");
            $(".schedule").click(function(){
            if ($('input[name=schedule]:checked').val() == "false" ) {
                $("#end-schedule").slideDown("fast"); //Slide Down Effect
            } else {
                $("#end-schedule").slideUp("fast");  //Slide Up Effect
            }
         });
    });
    </script>
    
    <script type="text/javascript">
    <!--
    var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
    var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "date", {format:"mm/dd/yyyy", validateOn:["blur"]});
    var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "date", {format:"mm/dd/yyyy", validateOn:["blur"]});
    //-->
    </script>
    </body>
    </html>
    
    <?php
    mysql_free_result($Recordset1);
    ?>
    

1 个答案:

答案 0 :(得分:0)

我很抱歉,但你的代码很糟糕(一点点)。阅读并理解您的代码正在做什么非常糟糕。您需要按逻辑部分拆分代码。一部分是由php编写的服务器端。对于mysql来说,最好使用PDO。前部需要仅包含html。其他文件中包含cssjavascript,并且包含在html中。

或者,所有这些你可以用一些框架来做,而不是自己写。