vendredi 24 juillet 2015

Updating SQL Server with Checkbox Status

I have page that pulls checkbox status from a SQL server, see code:

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);
?>

When the checkbox is ticked / unticked on screen I have a button which runs an update query on my database.

See code:

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 );
}

My code doesn't work and constantly updates with a value of '0'




Aucun commentaire:

Enregistrer un commentaire