首页 > 解决方案 > 如何在单选按钮中从数据库加载值?

问题描述

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

这是我尝试的代码:

	<?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; ?>"但我怀疑它是否正确..我的想法不多了..有人可以帮助我吗?

标签: javascriptphphtmlmysqldatabase

解决方案


做一个变量$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>

推荐阅读