使用复选框状态更新SQL Server

时间:2015-07-24 13:50:20

标签: php checkbox

我有从SQL服务器获取复选框状态的页面,请参阅代码:

while( $row2 = sqlsrv_fetch_array( $stmt2, SQLSRV_FETCH_ASSOC) ) {
    echo "<form>";
    echo "<tr>"
        $checked = $row2['has_loan'] == 1 ? "checked='checked'" : '';
        $checked2 = $row2['has_telephone'] == 1 ? "checked='checked'" : ''; 
        $checked3 = $row2['has_remote'] == 1 ? "checked='checked'" : ''; 
        echo "<td><input type=checkbox name=loan $checked></td>"; 
        echo "<td><input type=checkbox name=telephone $checked2></td>";
        echo "<td><input type=checkbox name=remote $checked3></td>";
        echo "<td><input class=form-control id=input-Default type=submit name=recordupdate value=Update></td>";
    echo "</tr>"
    echo "</form>";
    sqlsrv_free_stmt( $stmt2);
?>

当在屏幕上勾选/取消勾选复选框时,我有一个按钮,在我的数据库上运行更新查询。

参见代码:

if(isset($_POST['recordupdate'])){
    $sup_code = $_POST['psupcode'];
    $checked = ($data['checkinsat'] == '1') ? 'checked' : '';
    if (!empty($_POST['loan'])) {
        $truel = 1;
    }else {
        $truel = 0;
    }

    if (!empty($_POST['telephone'])) {
        $truet = 1;
    }else {
        $truet = 0;
    }

    if (!empty($_POST['remote'])) {
        $truer = 1;
    }else {
        $truer = 0;
    }

    $updateQuery = "UPDATE table
SET has_loan='$truel', has_telephone='$truet',has_remote='$truer'
WHERE sup_code='$_POST[psupcode]'";
sqlsrv_query( $conn, $updateQuery );
}

我的代码无法正常运行并且不断更新,其值为&#39; 0&#39;

1 个答案:

答案 0 :(得分:0)

这是简单的表格

<form action="pageb.php" method="POST">

    <input type='checkbox' name='telephone' value='1' checked/>

    <input type='checkbox' name='telephone1' value='1' />

    <input type="submit" name='submit' value="submit" >
</form>

和php

if($_POST['submit']){
    if (isset($_POST['telephone'])) {
        $truel = $_POST['telephone'];
}else {
    $truel = 0;
}
if (isset($_POST['telephone1'])) {
    $truela = $_POST['telephone1'];
}else {
    $truela = 0;
}
echo $truel;
echo '<br>'.$truela;
}