如何在单选按钮中从数据库加载值?

时间:2018-10-03 12:52:41

标签: javascript php html mysql database

我需要知道您是否可以执行此功能...我有一个数据库,该数据库的“布尔”字段接受0或1个值,并且我有一个HTML页面,其中有一个带有OK或No值的radioButton我必须能够在radioButton中加载布尔值,因此,如果在数据库中HTML页面上的布尔变量为1(执行SELECT之后),则在数据库中,如果HTML中布尔变量为0,radioButton将具有OK值。页面(执行SELECT之后),单选按钮将没有值

这是我尝试的代码:

	<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$idAffidataria = $_GET['idAffidataria'];

$sql1 = "SELECT * FROM Affidataria WHERE idAffidataria = '{$idAffidataria}'";



    $result = $conn->query($sql1);


    $details = $result->fetch_array();




    $savedNomeCantiere = $details["nomeCantiere"];
    $savedAddettoSicurezza = $details["addettoSicurezza"];
    $savedMailAffidataria = $details["mailAffidataria"];
    $savedContrattoDiAppalto = $details["contrattoDiAppalto"]; // <--- BOOLEAN <----
                 
        



$result1 = $conn->query($sql1);

echo($nomeCantiere);

?>


<html>
<body>

<table>
 <tr>
    
    <td colspan="2" bgcolor="#CDECFD" style="font-weight: bold">Cantiere</td>
    <td colspan="4" bgcolor="#CDECFD"><input type="text" class="form-control" style="width: 100%;" name="cantiereAffidataria" id="cantiereAffidataria" value="<?php echo $savedNomeCantiere; ?>"/>&nbsp;</td>
  </tr>
  <tr>
  <td colspan="2" bgcolor="#CDECFD" style="font-weight: bold">ContrattoDiAppalto</td>
  <td bgcolor="#B35556"><form action="">
    

       OK <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" /> <BR>
        NO  <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" checked/>
       
   
</form></td>
  </tr>
</table>

换句话说,我必须能够根据数据库中SELECT提供的数据更改单选按钮

我试图通过<?php echo $savedContrattoDiAppalto; ?>"赋予它价值,但是我怀疑它是正确的..我的想法已经用完了..有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

设置变量$checked = 'checked'

然后创建一个条件以检查它是1还是0

$checked = 'checked';
if($savedContrattoDiAppalto != 1){
    $checked = ''; // Default is checked, if it isn't 1 then it's empty and will not check your radio.
}

并将该变量应用于输入属性。

    <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" 
value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" $checked/>

完整代码应为:

<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$idAffidataria = $_GET['idAffidataria'];

$sql1 = "SELECT * FROM Affidataria WHERE idAffidataria = '{$idAffidataria}'";



    $result = $conn->query($sql1);


    $details = $result->fetch_array();




    $savedNomeCantiere = $details["nomeCantiere"];
    $savedAddettoSicurezza = $details["addettoSicurezza"];
    $savedMailAffidataria = $details["mailAffidataria"];
    $savedContrattoDiAppalto = $details["contrattoDiAppalto"];
    $checked = 'checked';             
    if($savedContrattoDiAppalto != true){
        $checked = ''; // if not true then not checked
    }    



$result1 = $conn->query($sql1);

echo($nomeCantiere);

?>


<html>
<body>

<table>
 <tr>

    <td colspan="2" bgcolor="#CDECFD" style="font-weight: bold">Cantiere</td>
    <td colspan="4" bgcolor="#CDECFD"><input type="text" class="form-control" style="width: 100%;" name="cantiereAffidataria" id="cantiereAffidataria" value="<?php echo $savedNomeCantiere; ?>"/>&nbsp;</td>
  </tr>
  <tr>
  <td colspan="2" bgcolor="#CDECFD" style="font-weight: bold">ContrattoDiAppalto</td>
  <td bgcolor="#B35556"><form action="">


       OK <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)"  $checked/> <BR>
        NO  <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="<?php echo $savedContrattoDiAppalto; ?>" onchange="color(this)" $checked/>


</form></td>
  </tr>
</table>