dongxiao3694 2015-11-30 16:39
浏览 11
已采纳

将我的收音机的值插入数据库

I can insert the values of the radio's in the database but it only works if I have selected the radio 'vrouw' and if select 'man' it does not insert into the database.

Php function:

if (isset($_POST['submitForm'])) {

    $dbhost = "localhost";
    $dbname = "schoolopdrachten";
    $dbusername = "root";
    $dbpassword = "";

    $link = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbusername,$dbpassword);

    $stmt = $link->prepare("INSERT INTO javascript (naam, gender, land, provincie, suboptionman, suboptionvrouw) VALUES (:naam, :gender, :land, :provincie, :achternaam, :suboptievrouw)");

    $stmt->bindParam(':naam', $_POST['inputName']);
    $stmt->bindParam(':gender', $_POST['keuzegender']);
    $stmt->bindParam(':land', $_POST['select1']);
    $stmt->bindParam(':provincie', $_POST['select2']);
    $stmt->bindParam(':achternaam', $_POST['keuzeMan']);
    $stmt->bindParam(':suboptievrouw', $_POST['keuzeVrouw']);

    $stmt->execute();
}

Html:

<div class="container">

    <form method="post" id="form" class="form-horizontal">
        <fieldset>
            <!-- Text input-->
            <div class="form-group">
                <label class="col-md-4 control-label" for="inputName">Naam</label>
                <div class="col-md-4">
                    <input id="inputName" name="inputName" type="text" placeholder="" class="form-control  input-md formFunction">
                    <p id="errorNaam" class="help-block formError"></p>

                </div>
            </div>

            <!-- Radio Keuze -->
            <div class="form-group">
                <label class="col-md-4 control-label">Geslacht</label>
                <div class="col-md-4">
                    <div class="radio">
                        <label for="radioMan">
                            <input class="formFunction" type="radio" name="keuzegender" id="radioMan" value="man">
                            Man
                        </label>
                        <label for="radioVrouw">
                            <input class="formFunction" type="radio" name="keuzegender" id="radioVrouw" value="vrouw">
                            Vrouw
                        </label>
                    </div>
                    <p id="errorRadioGroup1" class="help-block formError"></p>

                </div>
            </div>

            <!-- Keuze als man is gekozen -->
            <div id="keuzeMan" class="form-group hidden">
                <label class="col-md-4 control-label" for="optionMan">Achternaam</label>
                <div class="col-md-4">
                    <input id="optionMan" name="keuzeMan" type="text" placeholder="" class="form-control input-md formFunction">
                    <p id="errorKeuzeMan" class="help-block formError"></p>
                </div>
            </div>

            <!-- Keuze als vrouw is gekozen -->
            <div id="keuzeVrouw" class="form-group hidden">
                <label class="col-md-4 control-label">Leeftijd</label>
                <div class="col-md-4">
                    <label class="radio-inline" for="optionVrouw1">
                        <input class="formFunction" type="radio" name="keuzeVrouw" id="optionVrouw1" value="Jonger dan 50">
                        Jonger dan 50
                    </label>
                    <label class="radio-inline" for="optionVrouw2">
                        <input class="formFunction" type="radio" name="keuzeVrouw" id="optionVrouw2" value="Ouder dan 50">
                        Ouder dan 50
                    </label>
                    <p id="errorKeuzeVrouw" class="help-block formError"></p>

                </div>
            </div>

            <!-- Select Land -->
            <div class="form-group">
                <label class="col-md-4 control-label" for="selectLand">Land</label>
                <div class="col-md-4">
                    <select id="selectLand" name="select1" class="form-control  formFunction">
                        <option value="empty">--Kies Land--</option>
                        <option value="nederland">Nederland</option>
                        <option value="belgie">België</option>
                    </select>
                    <p id="errorSelectLand" class="help-block formError"></p>

                </div>
            </div>

            <!-- Select Provincie -->
            <div class="form-group">
                <label class="col-md-4 control-label" for="selectProvincie">Provincie</label>
                <div class="col-md-4">
                    <select id="selectProvincie" name="select2" class="form-control formFunction">
                        <option value="empty">--Kies provincie--</option>
                        <option value="overijssel">Overijssel</option>
                        <option value="noordHolland">Noord-Holland</option>
                    </select>
                    <p id="errorSelectProvincie" class="help-block formError"></p>

                </div>
            </div>

            <!-- Multiple Checkboxes -->
            <div class="form-group">
                <label class="col-md-4 control-label" ></label>
                <div class="col-md-4">
                    <div class="checkbox">
                        <label>
                            <input class="formFunction" type="checkbox" name="checkboxes" id="checkBox" value="checkBox">
                            Check het formulier & enable de verstuur button
                        </label>
                    </div>
                </div>
            </div>

            <!-- Button -->
            <div class="form-group">
                <label class="col-md-4 control-label"></label>
                <div class="col-md-4">
                    <button id="submitForm" name="submitForm" class="btn btn-success" disabled="disabled">Verstuur</button>
                </div>
            </div>

        </fieldset>
    </form>

</div>

Database:

enter image description here

  • 写回答

2条回答 默认 最新

  • douyu0852 2015-11-30 16:59
    关注

    What you should do in your processing page is check if the radio buttons are set first, then if they are, get the value.

    Like this...

    if( isset($_POST['keuzegender']) ) {
        $radio_value = $_POST['keuzegender'];
    }
    

    Other than that, your code looks correct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题