2012-12-28 47 views
0

我有一個非常有趣的問題。只有一部分值被輸入到我的數據庫中。插入數據庫的字符串不完整

我有一個包含表單的頁面。用戶將填寫表格並提交。所有的POST值都是正確的並提交給數據庫。一個特定值($ 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()); 
    } 
    } 
+0

你打電話給你的插入腳本一次或你有一些週期?你可以請張貼代碼如何調用你的插入功能? – user15

+0

@middaparka我同意你的意見,所提交的文件的長度可能不夠 – SaidbakR

+2

嘗試在插入查詢之後打印出查詢,看它是否正確。如果是,請嘗試在sql客戶端上手動運行它,看它是否正確插入信息。這將幫助你找出問題出在PHP端還是sql端 – GrayB

回答

1

這看起來可能是罪魁禍首:

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

這是在在foreach結束()循環:

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

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

你直接插入該值到這裏的DB:

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

但你的循環總是從創建循環的最後一次迭代的字符串。它似乎沒有連接以前的值。你可能。需要的東西是這樣的:

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

(注意「=」)

但是,你在你的嵌套foreach循環使用$critera一樣,所以你必須調整該位,以及。這樣的事情應該很容易:

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

我希望有幫助!

+0

謝謝!我會試一試,讓你知道。 – REF

0

您的循環中有多個邏輯錯誤,它會設置$criteria的值:您嘗試循環使用$value而不需要;如果可以的話,總會將$string2設置爲最終的$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; 

+0

第一個反應是解決了我的問題!你們很棒!謝謝。呼叫關閉 – REF

相關問題