2017-10-19 95 views
1

我試圖在wordpress中允許雙引號和單引號將一些標題保存到mysql數據庫中。所以,我希望能夠插入字符串,例如:在wordpress數據庫中插入單引號和雙引號

A man's dog walked into the bar 
    The "main reason" it does not work 
    It's time to announce "The Show" in an hour 

我的代碼看起來像這樣:

$question_id = filter_var($_REQUEST['question_id'], FILTER_SANITIZE_NUMBER_INT); 
    $feedback_correct = preg_replace("/[^a-zA-Z0-9'\"?_\. !&-]+/","",sanitize_text_field($_REQUEST['feedback_correct'])); 
    $feedback_incorrect = preg_replace("/[^a-zA-Z0-9'\"?_\. !&-]+/","",sanitize_text_field($_REQUEST['feedback_incorrect'])); 
    //preg_replace is to strip out any characters we dont want in the title like "<>|}{[]/" 
    $data = array(
    'feedback_correct' => $feedback_correct, 
    'feedback_incorrect' => $feedback_incorrect 
    ); 

    $update_feedback = $eot_quiz->updateQuestion($data, $question_id); 

而且updateQuestion功能:

​​

行程到數據庫後,我的字符串看起來像:

A man 

*字符串單引號後切斷

The "main reason" it does not work 

*看行用雙引號

It 

*字符串單引號

我如何獲得這些字符串插入後切斷正確的在數據庫?提前致謝。

顯示代碼:

 <?php 
     $quiz_question = $eot_quiz->get_question_by_id($question_id); 

     ?> 
     <div class="bs"> 
     <div class="panel panel-default"> 
      <form method="POST" action="#"> 
       <div class="panel-heading"> 
        <h3 class="panel-title"><?= $quiz_question['quiz_question']?></h3> 
       </div> 
       <div class="panel-body"> 

       <div class="form-group"> 
        <label for="feedback_correct">Feedback for correct answer</label> 
        <input type="text" class="form-control" name="feedback_correct" placeholder="Correct Feedback" value='<?= $quiz_question['feedback_correct']?>'> 
       </div> 
       <div class="form-group"> 
        <label for="feedback_incorrect">Feedback for incorrect answer</label> 
        <input type="text" class="form-control" name="feedback_incorrect" placeholder="Incorrect Feedback" value='<?= $quiz_question['feedback_incorrect']?>'> 
       </div> 
       <input type='hidden' name='question_id' value="<?= $question_id ?>" /> 
       <input type='hidden' name='quiz_id' value="<?= $quiz_id ?>" /> 
       <input type='hidden' name='subscription_id' value="<?= $subscription_id ?>" /> 
       <input type='hidden' name='feedback' value="true" /> 
       <button type="submit" class="btn btn-default">Update Feedback</button> 

     </div> 
     <div class="panel-footer"><a href="/dashboard/?part=update_quiz_questions&question_id=<?= $question_id?>&quiz_id=<?= $quiz_id?>&subscription_id=<?= $subscription_id ?>" class="btn btn-success pull-right">Take me back to the Question</a><div style="clear:both"></div></div> 
     </form> 
     </div> 
    </div> 
+1

[Bobby Tables](http://bobby-tables.com/) – ctwheels

+0

不,這裏沒有bobby表。 'wpdb'代碼使用正確的綁定參數。 –

+0

我試過bobby表: $ result = $ wpdb-> update(TABLE_QUIZ_QUESTION,$ data,array('ID'=> $ id),array('%s','%s')); 相同的結果 –

回答

1

使用esc_attr你想要一個元素裏面的東西屬性的任何時間:

<input type="text" class="form-control" name="feedback_correct" placeholder="Correct Feedback" value='<?php 
//Always escape before echoing to an html attribute 
esc_attr($quiz_question['feedback_correct']); 
?>'> 
0

什麼是基類的$wpdb?找出其逃生機制並使用它

或使用addslashes()

請不要試圖使用preg_replace,你可能無法處理所有需要轉義的字符。 (這不僅僅是撇號和報價。)