PHP在发布时不接受选定的选项值

时间:2018-11-15 09:22:46

标签: php html select

我在更新表单时遇到严重问题,它仅在用户在选择选项菜单中执行操作时才使用所选值,但是当用户在不更改选择值的情况下更新数据时,它将返回默认数据库值,该默认值设置为0

<select class="selectpicker" name="socio_demo_type" id="socio_demo_type">
<?php
$query_sd = "SELECT * FROM insertion WHERE id_insertion = ".$insertion["id"];
$result_sd = mysql_query($query_sd);
$type = mysql_fetch_array($result_sd);
$val = $type["ciblage_socio_demo_type"];
?>
    <option value="0"  <?php if($val == 0) echo 'selected' ?> >Pack affinitaire </option>  
    <option value="1"  <?php if($val == 1) echo 'selected' ?> >Achat ciblé (loggué)`enter code here`</option>
</select>

1 个答案:

答案 0 :(得分:-1)

也传递连接字符串并使用我的sqli语句。

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }    

<select class="selectpicker" name="socio_demo_type" id="socio_demo_type">

    <?php

    $query_sd = "SELECT * FROM insertion WHERE id_insertion = "$insertion["id"];

    $result_sd = mysqli_query($con , $query_sd);
    $type = mysqli_fetch_array($result_sd);
    $val = $type["ciblage_socio_demo_type"];

    ?>
    <option value="0"  <?php if($val == 0) echo 'selected' ?> > Option2 </option>  
    <option value="1"  <?php if($val == 1) echo 'selected' ?> > Option1 </option>
    </select>