PHP if / else语句radiobutton更新行

时间:2015-11-16 22:03:05

标签: php

我有一个if else语句,通过选择radiobutton来更新表中的某些行。但如果我提交我得到错误,查询失败。 所以如果选中radiobutton(studenten),请用列(functie)“2”更新所有行。

PHP代码:

$host="localhost"; // Host naam 
$gebruikersnaam="root"; // gebruikersnaam
$wachtwoord=""; // wachtwoord 
$db_naam="mailing"; // Db naam
$tbl_naam="users"; // Table na

$con= mysqli_connect("$host", "$gebruikersnaam", "$wachtwoord")or die("cannot connect"); 
 mysqli_select_db($con, "$db_naam")or die("cannot select DB");

// Get values van form 
 $onderwerp=$_POST['onderwerp'];
$bericht=$_POST['bericht'];
$datum = date('Y-m-d H:i:s');

$query1 ="SELECT * FROM users WHERE FUNCTIE = '1'";
$query2 ="SELECT * FROM users WHERE FUNCTIE = '2'";
$testResult = mysqli_query($con, $query1) or die('Err, query failed'); 
$testResult = mysqli_query($con, $query2) or die('Err, query failed'); 


if(isset($_POST['radio']) and ($_POST['radio'] == '1'))
{
$query1 ="INSERT into $tbl_naam(onderwerp, bericht, datum)VALUES('$onderwerp', '$bericht',' $datum ')";
$result = mysqli_query($con, $query1) or die('Error, query1 failed');


die;
} elseif(isset($_POST['radio']) && $_POST['radio'] == '2') {
$query2 =" INSERT into $tbl_naam(onderwerp, bericht, datum)VALUES('$onderwerp', '$bericht',' $datum ')";
$result = mysqli_query($con, $query2) or die('Error, query2 failed');
die;
}

这是我的HTML表单:

<form action="sendmail.php" method="post">

        <div class="form-group">
  <div class="col-md-12">
<label  class="control-label">
        Aan *
        <input type="radio" name="radio" id="studenten" value="2">Studenten
        <input type="radio" name="radio" id="docenten" value="1">Docenten     

</label>
</div>
</div>


<div class="form-group">
<div class="col-md-12">
<label  class="control-label">
        Onderwerp *
            <input class="form-control" type="text" name="onderwerp"      placeholder="Onderwerp" autocomplete="off" <?php  echo    isset($fields['email']) ? 'value ="' . e($fields['email']) .  '"' : '' ?>>

</label>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label  class="control-label">
        Bericht *
        <textarea class="form-control" name="bericht" placeholder="plaats hier uw bericht" rows="6"></textarea>

</label>
</div>
</div>
<div class="form-group">
      <div class="col-md-12">
        <input class="btn btn-success"  type="submit" value="Versturen">
</div>
      </div>
    </form>

1 个答案:

答案 0 :(得分:0)

php $ _POST获取输入字段值由其名称和两个单选按钮的名称相同,尝试这可能是它的工作将此代码添加到您的html文件

    <input type="radio" name="radio" id="studenten" value="2">Studenten
    <input type="radio" name="radio" id="docenten" value="1">Docenten
    <input type="hidden" name="valueToSubmit" id="radioValue" value="">

    <script>    

        var hidden = document.getElementById("radioValue");

        if (document.getElementById('studenten').checked) {
           hidden.value = document.getElementById('studenten').value;
        }

        if (document.getElementById('docenten').checked) {
           hidden.value = document.getElementById('docenten').value;
        }

   </script>

如果条件来自

,请更改您的PHP
if(isset($_POST['radio']) and ($_POST['radio'] == '1'))

elseif(isset($_POST['radio']) && $_POST['radio'] == '2')

if(isset($_POST['valueToSubmit']) && ($_POST['valueToSubmit'] == '1'))

else if(isset($_POST['valueToSubmit']) && ($_POST['valueToSubmit'] == '2'))