从数据库查询填充复选框

时间:2012-10-03 11:44:20

标签: php mysql

如果数据库中该字段的值为1,我正在尝试选中一个复选框。

我有:

<?php 

$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];

if ($selectedSPK) {
    $Priorityquery = "SELECT  Priority FROM Data WHERE SPKCustNo  = '$selectedSPK' ";
    $Priorityresult = mysql_query($Priorityquery);
    $row = mysql_fetch_array($Priorityresult);
    $checked = $Priorityresult['Priority'];
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1" 
<?php if ($checked == 1) echo ' checked'; ?> />

但没有任何快乐,任何想法?

6 个答案:

答案 0 :(得分:1)

试试这个:

您没有使用查询返回的行...

<?php 
    $selectedSPK=$_POST['SPKSelect'];
    $assigned = $_POST['Sales_Exec'];
    $date = $_POST['DateSelect'];
    if ($selectedSPK)
    {
        $Priorityquery = "SELECT  Priority FROM Data WHERE SPKCustNo  = '$selectedSPK' ";
        $Priorityresult = mysql_query($Priorityquery);
        $row = mysql_fetch_array($Priorityresult);
        //$checked = $Priorityresult['Priority']; // <------ this is where you went wrong...
        $checked = $row['Priority']; // <------ this will fix where u went wrong!
    }
    ?>
    <input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1){echo ' checked'; }?>

答案 1 :(得分:1)

你应该使用

<?php if ($checked == 1){echo "checked='checked'"; }

以及

$checked = $Priorityresult['Priority']; 

 $checked = $row['Priority'];

答案 2 :(得分:1)

我认为你有一个错误......试试这个

<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($row['Priority'] == 1) echo ' checked'; ?> />

答案 3 :(得分:0)

以这种方式试试

<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1) echo "checked='checked'"; ?> />

答案 4 :(得分:0)

变化

<?php if ($checked == 1) echo ' checked'; ?>

<?php if ($checked == 1) echo ' checked="checked"'; ?>

$checked = $Priorityresult['Priority'];$checked = $row['Priority'];

答案 5 :(得分:0)

应为checked="checked"

<?php if ($checked == 1) echo "checked='checked'"; ?>