PHP表单为必填字段

时间:2014-01-22 01:59:48

标签: php sql forms require

我有一个基本的表单,它将数据提交到数据库,我希望它要求提交某些字段,到目前为止,它确认字段为空,但它仍然提交。我似乎无法找到解决方案..

代码

<?
// define variables and set to empty values
$asinErr = $qtyErr = $floorErr = $locErr; 
$asin = $quantity = $floor = $location; 

    # this is processed when the form is submitted
    # back on to this page (POST METHOD)
    if ($_SERVER['REQUEST_METHOD'] == "POST") 
    {
     if (empty($_POST["asin"]))
         {$asinErr = "ASIN is required";}
     else       
         {$asin = addslashes($_POST["asin"]);}

     if (empty($_POST["quantity"]))
         {$qtyErr = "Quantity is required";}
     else      
         {$quantity = addslashes($_POST["quantity"]);}

     if (empty($_POST["floor"]))
         {$floorErr = "Floor is required";}
     else      
         {$floor = addslashes($_POST["floor"]);}

     if (empty($_POST["location"]))
         {$locErr = "Location is required";}
     else      
     {$location = addslashes($_POST["location"]);}

        # setup SQL statement
    $sql  = " INSERT INTO kiva_amnesty_log ";
    $sql .= " (asin, quantity, floor, location, date) VALUES ";
    $sql .= " ('$asin','$quantity','$floor','$location', now()) ";

        #execute SQL statement
        $result = mysql_query($sql, $cid);

        # check for error
        if (mysql_error()) { print "Database ERROR: " . mysql_error(); }

print "<h3><font color=red>New Amnesty Added - View it <a href=amnesty_log_summary.php>HERE</a></font></h3>";
}

?>
<form name="fa" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">   
    <table>
        <tr><td>ASIN:</td><td><input type="text" name="asin" id="asin"><span class="error">* <?php echo $asinErr;?></span></td></tr>
        <tr><td>Quantity:</td><td><input type="text" name="quantity" id="quantity"><span class="error">* <?php echo $qtyErr;?></span></td></tr>
        <tr><td>Floor:</td><td><select name="floor"><option value="1">Floor 1</option><option value="2">Floor 2</option></select><span class="error">* <?php echo $floorErr;?></span></td></tr>
        <tr><td>KIVA Floor:</td><td><input type="radio" value="Yes" name="location">Yes<input type="radio" value="No" name="location">No</select><span class="error">* <?php echo $locErr;?></span></td></tr>
        <tr><td><input type="submit" name="submit" id="submit" value="Submit Amnesty!"></td></tr>
    </table>
</form>

更新

<?
// define variables and set to empty values 
$asinErr = $qtyErr = $floorErr = $locErr = "";
$asin = $quantity = $floor = $location = "";

    $lb_error = 0;

    if ($_SERVER['REQUEST_METHOD'] == "POST")   
{
    if (empty($_POST["asin"])) {
        $asinErr = "ASIN is required";
        $lb_error = 1;
}   else {  
        $asin = addslashes($_POST["asin"]);
}

    if (empty($_POST["quantity"])) {
        $qtyErr = "Quantity is required";
        $lb_error = 1;
}   else {  
        $quantity = addslashes($_POST["quantity"]);
}

    if (empty($_POST["floor"])) {
        $floorErr = "Floor is required";
        $lb_error = 1;
}   else {  
        $floor = addslashes($_POST["floor"]);
}

    if (empty($_POST["location"])) {    
        $locErr = "Location is required";
        $lb_error = 1;
}   else {
        $location = addslashes($_POST["location"]);
}
    if($lb_error) {
    continue;
}
        # setup SQL statement
    $sql  = " INSERT INTO kiva_amnesty_log ";
    $sql .= " (asin, quantity, floor, location, date) VALUES ";
    $sql .= " ('$asin','$quantity','$floor','$location', curdate()) ";

        #execute SQL statement
        $result = mysql_query($sql, $cid);

        # check for error
        if (mysql_error()) { print "Database ERROR: " . mysql_error(); }

2 个答案:

答案 0 :(得分:1)

您想要检查错误变量是否为空。如果不是,那么打破脚本

if(!empty($asinErr) || !empty($qtyErr) || !empty($floorErr) || !empty($locErr) ) {
     break;
}

沿着这些方向发展。

在到达写入数据库的位置之前检查错误

答案 1 :(得分:1)

在顶部定义

$lb_error = 0;

在if / else检查错误的整个过程中,如果出现错误,请指定变量a 1

if (empty($_POST["asin"])) {
         $asinErr = "ASIN is required";
         $lb_error = 1;
}     else   {    
         $asin = addslashes($_POST["asin"]);
}

然后在完成所有这些之后,检查错误并在有任何

时中断
if($lb_error) {
     break;
}