使用PHP复选框的最佳方法

时间:2010-09-01 18:34:35

标签: php html

首先我是菜鸟,所以请原谅我的基本问题。我正在尝试使用html复选框告诉我何时检查序列号。问题是无论我做什么我都会收到错误告诉我“注意:未定义索引:在XYZ中sncheck”。

我尝试使用三元运算符来设置发布时的值(如果没有选中复选框),但它仍然在我身上失败。任何建议都是

这是代码,在我下面使用一个名为sncheck的复选框和一个ok值。谢谢!

if (isset($_POST['submit'])){
    $gov = mysqli_real_escape_string( $dbc, trim($_POST['gov']));
    $pctype = mysqli_real_escape_string( $dbc, trim($_POST['pctype']));
    $compname = mysqli_real_escape_string( $dbc, trim($_POST['compname']));
    $username = mysqli_real_escape_string( $dbc, trim($_POST['username']));
    $compmodel = mysqli_real_escape_string( $dbc, trim($_POST['compmodel']));
    $serialnumber = mysqli_real_escape_string( $dbc, trim($_POST['serialnumber']));
    $sncheck = ($_POST['sncheck']== 'ok') ? $_POST['sncheck'] : '';
    $purchdate = mysqli_real_escape_string( $dbc, trim($_POST['purchasedate']));
    $os = mysqli_real_escape_string( $dbc, trim($_POST['os']));
    $memory = mysqli_real_escape_string( $dbc, trim($_POST['memory']));
    $monitor1 = mysqli_real_escape_string( $dbc, trim($_POST['monitor1']));
    $monitor2 = mysqli_real_escape_string( $dbc, trim($_POST['monitor2']));
    $warranty = mysqli_real_escape_string( $dbc, trim($_POST['warranty']));
    $warrantyend = mysqli_real_escape_string( $dbc, trim($_POST['warrantyend']));
    $status = mysqli_real_escape_string( $dbc, trim($_POST['status']));
    $notes = mysqli_real_escape_string( $dbc, trim($_POST['notes']));

    if (dateformat($purchdate) == 1 && $sncheck == "ok"){
            $querysn = "SELECT serialnumber FROM inventory" . 
                                " WHERE serialnumber = '" . $serialnumber . "'";
            $data = mysqli_query($dbc, $querysn) or die('Error querying the Database for Serial Numbers');
            if (mysqli_num_rows($data) == 0){
                $queryupdate = "UPDATE inventory SET government = '$gov', pctype = '$pctype', compname = '$compname'," . 
                                    " username = '$username', compmodel = '$compmodel', serialnumber = '$serialnumber', purchased = '$purchdate'," . 
                                    " operatingsystem = '$os', `memory` = '$memory', monitor1 = '$monitor1'," . 
                                    " monitor2 = '$monitor2', warranty = '$warranty', warrantyend = '$warrantyend', `status` = '$status', notes = '$notes'" . 
                                    " WHERE serialnumber = '" . $_POST['serialnumber'] . "'";
                mysqli_query($dbc, $queryupdate);
                echo 'PC was sucessfully saved';
            }
            else{
                echo '<p class="error">The serial number has already been used, please try another</p>';
            }
    }
    else{
        echo '<p class="error">The date format does not match</p>';
    }

    if (dateformat($purchdate) == 1){
            $querysn = "SELECT serialnumber FROM inventory" . 
                                " WHERE serialnumber = '" . $serialnumber . "'";
            $data = mysqli_query($dbc, $querysn) or die('Error querying the Database for Serial Numbers');
            if (mysqli_num_rows($data) == 0){
                $queryupdate = "UPDATE inventory SET government = '$gov', pctype = '$pctype', compname = '$compname'," . 
                                    " username = '$username', compmodel = '$compmodel', purchased = '$purchdate'," . 
                                    " operatingsystem = '$os', `memory` = '$memory', monitor1 = '$monitor1'," . 
                                    " monitor2 = '$monitor2', warranty = '$warranty', warrantyend = '$warrantyend', `status` = '$status', notes = '$notes'" . 
                                    " WHERE serialnumber = '" . $_POST['serialnumber'] . "'";
                mysqli_query($dbc, $queryupdate);
                echo 'PC was sucessfully saved';
            }
            else{
                echo '<p class="error">The serial number has already been used, please try another</p>';
            }
    }
    else{
        echo '<p class="error">The date format does not match</p>';
    }
}

2 个答案:

答案 0 :(得分:2)

已编辑的答案:根据您编辑的问题,尝试更改:

$sncheck = ($_POST['sncheck']== 'ok') ? $_POST['sncheck'] : '';

为:

$sncheck = isset($_POST['sncheck']);

如果选中复选框,则会在$_POST中设置。如果没有检查它将不会。

原始答案:

很难在没有看到代码的情况下回答你的问题,所以我只是在黑暗中刺伤,但这里就是这样。这是一个例子:

<?php

$serial_numbers = array('1','2','3'); // Put your real serial numbers here...

if(isset($_REQUEST['submit']))
{
    if(isset($_REQUEST['serial_numbers']) && is_array($_REQUEST['serial_numbers']))
    {
        foreach($_REQUEST['serial_numbers'] as $serial)
        {
            // Do something with this serial number
            echo "You checked $serial<br>";
        }
    }
}
else
{
    // Show a form to select serial numbers:

    echo "<form action=\"$PHP_SELF\">";
    foreach($serial_numbers as $serial)
    {
        $serial_ht = htmlspecialchars($serial);
        echo "<input type=\"checkbox\" name=\"serial_numbers[]\" value=\"$serial_ht\">$serial_ht<br>";
    }

    echo '<input type="submit" name="submit" value="Submit">';
}

这是一个完整的猜测,因为我不确定你在做什么。但这将采用一系列序列号并显示一个包含它们的表单,每个表单旁边都有复选框。提交表单后,所选的序列号将位于$_REQUEST['serial_numbers'](这将是一个数组)。该脚本将循环遍历此数组的每个成员并将这些序列号打印回来。不是很有用,但同样,它只是一个例子。如果您想要更多指示,请发布一些代码,我会编辑我的答案。

答案 1 :(得分:2)

使用isset($_GET['id']);isset($_POST['id'])检查表单中是否提交了某个字段(例如复选框)。

(但请注意,文本字段和其他一些表单输入将为"",因此将被视为使用上述方法“提交”

修改
此外,您没有收到错误,而是通知。通知并不意味着出现了问题,但是你应该做一些事情而不是你实际做的事情等等,以使PHP脚本更好。但是,警告是“非致命错误”,错误只是致命错误,会导致脚本停止执行。