处理POST时有时会丢失textarea值

时间:2013-06-20 21:44:50

标签: php post textarea

我遇到textarea帖子值有时无法处理的问题。问题是间歇性地我缺少textarea 27和30的一些值。它似乎不是浏览器问题,它似乎不是数据长度的问题。

这是表单html:

  <div id="surveycontainer">
      <form method="POST" action="blog/questions" name="exitform">
      <input id="pnum" type="hidden" name="pagenum" value="3"/>

      <div class="qsection">
        <p> 
          27.  Suppose that a friend from another nation comes to see you and doesn’t know about the U.S. economy. How would you <span style="font-style:italic">describe the economy</span> to your friend?  Please actually write the words you would say as specifically as possible.
        </p>
        <textarea id="mlq1" name="27" cols="50" rows="5"></textarea>
      </div>

      <div class="qsection">
        <p> 
          28. What is your opinion about the future of the U.S. economy? Do you think that over the next few months the U.S. economy will improve, become worse, or stay the same?
        </p>
          <input type="radio" name="28" value="-2"><span class="radiotext">-2 (Greatly worse)</span><br/>
          <input type="radio" name="28" value="-1"><span class="radiotext">-1 (Worse)</span> <br/>
          <input type="radio" name="28"  value="0"><span class="radiotext">0 (Stay the same)</span> <br/>
          <input type="radio" name="28" value="1"><span class="radiotext">1 (Better)</span> <br/>
          <input type="radio" name="28" value="2"><span class="radiotext">2 (Much better)</span>
      </div>

      <div class="qsection">
        <p> 
          29. What are the reasons for your prediction? Explain or list them.
        </p>
        <textarea id="mlq2" name="29" cols="50" rows="5"></textarea>
      </div>

      <div class="qsection">
        <p> 
          30. What do you suggest as a solution to improve the U.S. economy?
        </p>
        <textarea id="mlq3" name="30" cols="50" rows="5"></textarea>
      </div>

这是处理php:

private function processAnswers($pid) 
  {
    date_default_timezone_set('America/Chicago');
    $created = date("Y-m-d H:i:s");

    foreach($_POST as $key=>$value) {
      if ($key == "pagenum")
        continue;
      // echo "$key: $value \n";
      $sql = "insert into default_answers (participant_id, answer_id, answer_value, created) values (" . $pid . ",'" . htmlspecialchars($key) . "','" . htmlspecialchars($value) . "', '" . $created . "')"; 
      // echo $sql;
      $this->db->query($sql);
    }   
  }

这是调用它的代码:

if (isset($_POST['pagenum'])) {
  $pagenum = $this->input->post('pagenum');

  $this->processAnswers($pid);
}

由于

1 个答案:

答案 0 :(得分:0)

谢谢Mock Daear,就是这样。 mysql_real_escape_string(用htmlspecialchars)。当输入中有单引号时就会发生这种情况。