0

我有一个非常有趣的问题。只有部分值被输入到我的数据库中。

我有一个包含表单的页面。用户将填写表格并提交。所有 POST 值都是正确的,并且正在提交到数据库。一个特定的值 ($criteria) 仅提交字符串的最后一部分。如果我回显 $criteria 的值,它就在那里,但是当我在 mysqlAdmin 中查看它时,它只有一部分在那里。

它应该提交:

Communication | 1 | dsafadf | Customer Service | 2 | asdfadf | Dependability | 3 | asdfadsf | Initiative | 4 | dsafadsfa | Job Knowledge | 5 | dsadafsadsf | Judy | 1 | asdfasdf | Punctuality | 2 | asdfdasdfadsf |

但在数据库中我只得到 - | 准时 | 2 | asdfdasdfadsf |

我尝试使用 mysqlAdmin 手动添加值,它工作正常。

如果有其他信息需要帮助我会补充。

感谢大家!!


这是我的插入脚本:


      public function insertReview() {
      $fk_employee = $_POST['fk_employee'];

      // Current Date returned from JQuery and formatted to add to DB.
      $cdate = $_POST['current_date'];
      $current_date = explode("/", $cdate);
        $cmonth = $current_date[0];
        $cday = $current_date[1];
        $cyear = $current_date[2];
        $current_dateA = array($cyear, $cmonth, $cday);
      $review_date = implode("-", $current_dateA);

      // Review Begin Date returned from JQuery Datepicker and formatted to add to DB.            
      $bdate = $_POST['r_period_begin'];
      $begin_date = explode("/", $bdate);
        $bmonth = $begin_date[0];
        $bday = $begin_date[1];
        $byear = $begin_date[2];
        $begin_dateA = array($byear, $bmonth, $bday);
      $r_period_begin = implode("-", $begin_dateA);

      // Review End Date returned from JQuery Datepicker and formatted to add to DB.
      $edate = $_POST['r_period_end'];
      $end_date = explode("/", $edate);
        $emonth = $end_date[0];
        $eday = $end_date[1];
        $eyear = $end_date[2];
        $end_dateA = array($eyear, $emonth, $eday);
      $r_period_end = implode("-", $end_dateA);

    // Criteria 
      $criterias = $_POST['criteria'];
      //var_dump($criterias);
      $criteriaValue = $_POST['criteriaValue'];
      //var_dump($criteriaValue);
      $comments = $_POST['Comments'];

      foreach ($criteriaValue as $key => $value ){
          foreach( $criterias as $criteria ){
              if( $criteria == $key ){
                  $string1 = $key;
                  foreach( $comments as $comment => $comm ){
                      if( $string1 == $comment ){
                          $string3 = $comm;
                      }
                  }
              }
          }
          //echo $key . '<br>';
          foreach ( $value as $result ){
              $string2 = $result;
          }

      $criteria = mysql_real_escape_string( $string1 . ' | ' . $string2 . ' | ' . $string3 . ' | ' );
      echo $criteria;
      }
      // End of Criteria

      $job_knowledge = $_POST['job_knowledge'];
      $jk_comments = $_POST['jk_comments'];
      $work_quality = $_POST['work_quality'];
      $wq_comments = $_POST['wq_comments'];
      $attendance = $_POST['attendance'];
      $attend_comments = $_POST['attend_comments'];
      $initiative = $_POST['initiative'];
      $init_comments = $_POST['init_comments'];
      $communication = $_POST['communication'];
      $comm_comments = $_POST['comm_comments'];
      $dependability = $_POST['dependability'];
      $depend_comments = $_POST['depend_comments'];
      $customer_service = $_POST['customer_service'];
      $cs_comments = $_POST['cs_comments'];
      $overall_rating = $_POST['overall_rating'];
      $additional_comments = $_POST['additional_comments'];
      $goals = $_POST['goals'];

    $conn = parent::connect();
    $sql = "INSERT INTO " . TBL_EMPLOYEE_REVIEW . " (
              fk_employee,
              review_date,
              r_period_begin,
              r_period_end,
              job_knowledge,
              jk_comments,
              work_quality,
              wq_comments,
              attendance,
              attend_comments,
              initiative,
              init_comments,
              communication,
              comm_comments,
              dependability,
              depend_comments,
              customer_service,
              cs_comments,
              overall_rating,
              additional_comments,
              goals,
              criteria
            ) VALUES (
              :fk_employee,
              :review_date,
              :r_period_begin,
              :r_period_end,
              :job_knowledge,
              :jk_comments,
              :work_quality,
              :wq_comments,
              :attendance,
              :attend_comments,
              :initiative,
              :init_comments,
              :communication,
              :comm_comments,
              :dependability,
              :depend_comments,
              :customer_service,
              :cs_comments,
              :overall_rating,
              :additional_comments,
              :goals,
              :criteria
            )";

    try {
      $st = $conn->prepare( $sql );
      $st->bindValue( ":fk_employee", $fk_employee, PDO::PARAM_STR );
      $st->bindValue( ":review_date", $review_date, PDO::PARAM_STR );
      $st->bindValue( ":r_period_begin", $r_period_begin, PDO::PARAM_STR );
      $st->bindValue( ":r_period_end", $r_period_end, PDO::PARAM_STR );
      $st->bindValue( ":job_knowledge", $job_knowledge, PDO::PARAM_STR );
      $st->bindValue( ":jk_comments", $jk_comments, PDO::PARAM_STR );
      $st->bindValue( ":work_quality", $work_quality, PDO::PARAM_STR );
      $st->bindValue( ":wq_comments", $wq_comments, PDO::PARAM_STR );
      $st->bindValue( ":attendance", $attendance, PDO::PARAM_STR );
      $st->bindValue( ":attend_comments", $attend_comments, PDO::PARAM_STR );
      $st->bindValue( ":initiative", $initiative, PDO::PARAM_STR );
      $st->bindValue( ":init_comments", $init_comments, PDO::PARAM_STR );
      $st->bindValue( ":communication", $communication, PDO::PARAM_STR );
      $st->bindValue( ":comm_comments", $comm_comments, PDO::PARAM_STR );
      $st->bindValue( ":dependability", $dependability, PDO::PARAM_STR );
      $st->bindValue( ":depend_comments", $depend_comments, PDO::PARAM_STR );
      $st->bindValue( ":customer_service", $customer_service, PDO::PARAM_STR );
      $st->bindValue( ":cs_comments", $cs_comments, PDO::PARAM_STR );
      $st->bindValue( ":overall_rating", $overall_rating, PDO::PARAM_STR );
      $st->bindValue( ":additional_comments", $additional_comments, PDO::PARAM_STR );
      $st->bindValue( ":goals", $goals, PDO::PARAM_STR );
      $st->bindValue( ":criteria", $criteria, PDO::PARAM_STR );

      $st->execute();
      parent::disconnect( $conn );
    } catch ( PDOException $e ) {
      parent::disconnect( $conn );
      die( "Query failed: " . $e->getMessage() );
    }
  } 
4

2 回答 2

1

这看起来可能是罪魁祸首:

$criteria = mysql_real_escape_string( $string1 . ' | ' . $string2 . ' | ' . $string3 . ' | ' );

它在 foreach() 循环的末尾:

foreach ($criteriaValue as $key => $value ){
    ...

    $criteria = mysql_real_escape_string( $string1 . ' | ' . $string2 . ' | ' . $string3 . ' | ' );
}

您在此处将该值直接插入到数据库中:

$st->bindValue( ":criteria", $criteria, PDO::PARAM_STR );

但是你的循环总是从循环的最后一次迭代中创建一个字符串。它似乎没有连接以前的值。你概率。需要这样的东西:

$criteria .= mysql_real_escape_string( $string1 . ' | ' . $string2 . ' | ' . $string3 . ' | ' );

(注意“。=”)

但是,您也在$critera嵌套的 foreach 循环中使用,因此您也必须调整该位。这样的事情应该很容易:

foreach( $criterias as $crit){
          if( $crit == $key ){
....

我希望这会有所帮助!

于 2012-12-28T17:17:24.063 回答
0

您的循环中有多个逻辑错误,这些错误设置了以下值$criteria:您尝试在$value不需要的情况下循环;如果可以的话,它总是会$string2设置为 final $value。您还拥有$criteria第二个内部 foreach 循环的持有者和您想要使用的内容。最后,您正在设置$critera每个循环,而不是附加到它,并且您的输出具有误导性,因为您在循环时回显,而不是$criteria从循环外部回显最终结果:


 foreach ($criteriaValue as $key => $value ){
          foreach( $criterias as $criteria ){
              if( $criteria == $key ){
                  $string1 = $key;
                  foreach( $comments as $comment => $comm ){
                      if( $string1 == $comment ){
                          $string3 = $comm;
                      }
                  }
              }
          }
          //echo $key . '
'; foreach ( $value as $result ){ $string2 = $result; } $criteria = mysql_real_escape_string( $string1 . ' | ' . $string2 . ' | ' . $string3 . ' | ' ); echo $criteria; }

应该:


foreach ($criteriaValue as $key => $value ){
  foreach( $criterias as $criteriasValue ){
    if( $criteriasValue == $key ){
      $string1 = $key;
      foreach( $comments as $comment => $comm ){
        if( $string1 == $comment ){
          $string3 = $comm;
        }
      }
    }
  }

  $string2 = $result;

  $criteria .= mysql_real_escape_string( $string1 . ' | ' . $string2 . ' | ' . $string3 . ' | ' );

}

echo $criteria;

于 2012-12-28T17:22:28.310 回答