插入数据库的字符串不完整

时间:2012-12-28 17:02:30

标签: php mysql pdo

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

我有一个包含表单的页面。用户将填写表格并提交。所有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() );
    }
  } 

2 个答案:

答案 0 :(得分: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 . ' | ' );

(注意“。=”)

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

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

我希望有所帮助!

答案 1 :(得分:0)

你的循环中有多个逻辑错误,它设置$criteria的值:你试图在$value上循环而不需要;如果可以的话,它会始终将$string2设置为最终$value。您还有$criteria作为第二个内部foreach循环的持有者以及您想要使用的内容。最后你要为每个循环设置$critera,而不是附加到它,并且你的输出会产生误导,因为你在循环时回显,而不是从循环外回显$criteria的最终结果: / p>


 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;