douli1872 2018-09-05 14:22
浏览 83
已采纳

我该如何解析数据库中的单选按钮选择

Hello I am newbie and I would like to ask how can I parse the value that the user makes from the radio button to database. I tried to do it but when I go to database I don't see the value, only the text on. What am I doing wrong? can you please help me? Any suggestions are welcome. I have this code:

<?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(); ?>

</div>
  • 写回答

2条回答 默认 最新

  • dpno17028 2018-09-05 14:31
    关注

    Try adding value attributes to all your checkboxes inputs

    Instead of this

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

    Use this

    <input type="radio" id="Α. Ίδια" name="answer1" value="Α. Ίδια" checked />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向