When I tried to save the user input in the database, if the user uses an apostrophe ( ‘ ) then in the database it is saved with a slash ( / ) before the apostrophe. So for example if the user enters “Luigi’s”, in the database it will be saved as “Luigi/’s” and I’ve been tweaking my code so much that now the information is being saved as “Luigi///’s”
Please see below how I’m submitting the data:
global $wpdb;
$table_questions = $wpdb->prefix . 'qw_questions';
$table_quizzes = $wpdb->prefix . 'qw_quizzes';
// Handle form submission to create a new question
if (isset($_POST['qw_create_question'])) {
$quiz_id = intval($_POST['qw_quiz_id']);
$question_type = sanitize_text_field($_POST['qw_question_type']);
$question_text = isset($_POST['qw_question_text']) ? esc_sql(sanitize_textarea_field($_POST['qw_question_text'])) : '';
echo '<script>alert("' . $question_text . '")</script>';
$answer_options = isset($_POST['qw_answer_options']) ? esc_sql(sanitize_textarea_field($_POST['qw_answer_options'])) : '';
$answer_images = isset($_POST['qw_answer_images']) ? esc_sql(sanitize_text_field(implode(',', $_POST['qw_answer_images']))) : '';
$score = (isset($_POST['qw_score']) && is_numeric($_POST['qw_score']) && $_POST['qw_score'] !== '') ? intval($_POST['qw_score']) : 1;
$correct_answer = isset($_POST['qw_correct_answer']) ? esc_sql(sanitize_textarea_field($_POST['qw_correct_answer'])) : '';
$question_timer = isset($_POST['qw_question_timer']) ? intval($_POST['qw_question_timer']) : null;
// If no order has been assigned, assign a new one
$highest_question_order = $wpdb->get_var($wpdb->prepare("SELECT MAX(question_order) FROM $table_questions WHERE quiz_id = %d", $quiz_id));
if ($highest_question_order === null) {
$highest_question_order = 1;
} else {
$highest_question_order += 1;
}
$question_order = (isset($_POST['qw_question_order']) && is_numeric($_POST['qw_question_order']) && $_POST['qw_question_order'] !== '') ? intval($_POST['qw_question_order']) : $highest_question_order;
$instructions = isset($_POST['qw_instructions-text']) ? esc_sql(sanitize_textarea_field($_POST['qw_instructions-text'])) : '';
// If no group id has been assigned, assign a new one
$highest_group_id = $wpdb->get_var($wpdb->prepare("SELECT MAX(group_id) FROM $table_questions WHERE quiz_id = %d", $quiz_id));
if ($highest_group_id === null) {
$highest_group_id = 1;
} else {
$highest_group_id += 1;
}
$group_id = (isset($_POST['qw_group_id']) && is_numeric($_POST['qw_group_id']) && $_POST['qw_group_id'] !== '') ? intval($_POST['qw_group_id']) : $highest_group_id;
$media_url = isset($_POST['qw_media_url']) ? esc_sql(sanitize_text_field($_POST['qw_media_url'])) : '';
$data = [
'quiz_id' => $quiz_id,
'question_type' => $question_type,
'question_timer' => $question_timer,
'question_order' => $question_order,
'instructions' => $instructions,
'media_url' => $media_url,
'group_id' => $group_id
];
if ($question_type != "instructions") {
$data['question_text'] = $question_text;
$data['answer_options'] = $answer_options;
$data['answer_images'] = $answer_images;
$data['score'] = $score;
$data['correct_answer'] = $correct_answer;
}
echo '<script>alert("' . $question_text . '")</script>';
echo '<script>alert("' . $data . '")</script>';
$result = $wpdb->insert($table_questions, $data);
if ($result) {
echo '<div class="updated"><p>Question created successfully.</p></div>';
} else {
echo '<div class="error"><p>Error creating question. Please try again.</p></div>';
}
}
in this case the
$question_text
$answer_options
are being saved with those additional ( // )
Correctly save an apostrophe in the database of wordpress