如何将单选按钮选择解析到数据库

时间:2018-09-05 14:22:49

标签: php database wordpress forms radio-button

您好,我是新手,我想问一下如何解析用户从单选按钮到数据库的值。我尝试这样做,但是当我进入数据库时​​,我看不到值,仅看到文本。我究竟做错了什么?你能帮我么?欢迎任何建议。我有以下代码:

<?php
/*
Template Name: Form
*/
?>
<?php global $pc_theme_object; /* Reference theme framework class */ ?>
<?php get_header(); ?>

<form action="" id="postjob" method="post">
    <fieldset>
        <legend>blalala;</legend>
<input type="hidden" name="question1" value="blalala;"/>
        <div>
            <input type="radio" id="Α. Ίδια" name="answer1" checked />
            <label for="Α. Ίδια">Α. Ίδια</label>
        </div>

        <div>
            <input type="radio" id="Β. Διαφορετικές" name="answer1" />
            <label for="Β. Διαφορετικές">Β. Διαφορετικές</label>
        </div>

        <div>
            <input type="radio" id="Γ. Δεν γνωρίζω" name="answer1" />
            <label for="Γ. Δεν γνωρίζω">Γ. Δεν γνωρίζω</label>
        </div>


    </fieldset>
	
	
	
	 <fieldset>
        <legend>rororor</legend>

<input type="hidden" name="question2" value="rororor"/>

        <div>
            <input type="radio" id="Α. Ναι" name="answer2" checked />
            <label for="Α. Ναι">Α. Ναι</label>
        </div>

        <div>
            <input type="radio" id="Β. Όχι" name="answer2" />
            <label for="Β. Όχι">Β. Όχι</label>
        </div>

         <div>
            <input type="radio" id="Γ. Δεν γνωρίζω" name="answer1" />
            <label for="Γ. Δεν γνωρίζω">Γ. Δεν γνωρίζω</label>
        </div>


		 
    </fieldset>
	 <button type="submit" name="submit">Submit</button>
</form>

<?php
$servername ="localhost";
$username = "username";
$password = "password";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $db_name);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


$question1 = $_POST['question1'];
$answer1 = $_POST['answer1'];
$question2= $_POST['question2'];
$answer2 = $_POST['answer2'];

 global $wpdb;

if(isset($_POST['submit'])){
        $wpdb->insert( 'survey_test', array( 'question1' =>
        $_POST['question1'], 'answer1' => $_POST['answer1'], 'question2' =>
        $_POST['question2'], 'answer2' => $_POST['answer2'] ),
        array( '%s', '%s', '%s', '%s' ) );
    }
echo "Connected successfully";
?>

<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:0)

您在输入字段中缺少value属性。添加与id属性具有相同值的value属性,它们将在$ _POST变量中可用。

答案 1 :(得分:0)

尝试将值属性添加到所有复选框输入中

代替此

<input type="radio" id="Α. Ίδια" name="answer1" checked />

使用此

<input type="radio" id="Α. Ίδια" name="answer1" value="Α. Ίδια" checked />