检查php上的空字段

时间:2015-07-07 17:54:44

标签: php mysql loops

我在我的数据库中得到了一些插入动作,并希望用foreach循环检查php。我不明白代码中的错误,因为没有成功或失败的结果返回。任何帮助将不胜感激。

<?php
$PlaceName = $_POST['placeName'];
$PlaceAddress = $_POST['addressArea'];
$PlacePhone = $_POST['placePhone'];
$PlaceWebsite = $_POST['placeWebsite'];
$OOnWeekday = $_POST['openweekDay'];
$OOnWeekEnd = $_POST['openweekEnd'];
$COnWeekday = $_POST['closeweekDay'];
$COnWeekEnd = $_POST['closeweekEnd'];
$Lati = $_POST['latitude'];
$Longi = $_POST['longitude'];
$btnAddPlace = $_POST['addPlacebut'];


$dbQueryI = $nesnePDO->prepare("INSERT INTO Places
                       (Place_Name,Place_Address,Place_Phone,Place_Web,
                        OOnWeek_Day,OOnWeek_End,COnWeek_Day,COnWeek_End,
                        Lati_P,Longi_P) 
                VALUES (:P_N,:P_A,:P_P,:P_W,
                        :OOWD_P,:OOWE_P,:COWD_P,:COWE_P,
                        :L_P,:L2_P)");


$dbQueryI->bindParam(":P_N",$PlaceName);
$dbQueryI->bindParam(":P_A",$PlaceAddress);
$dbQueryI->bindParam(":P_P",$PlacePhone);
$dbQueryI->bindParam(":P_W",$PlaceWebsite);
$dbQueryI->bindParam(":OOWD_P",$OOnWeekday);
$dbQueryI->bindParam(":OOWE_P",$OOnWeekEnd);
$dbQueryI->bindParam(":COWD_P",$COnWeekday);
$dbQueryI->bindParam(":COWE_P",$COnWeekEnd);
$dbQueryI->bindParam(":L_P",$Lati);
$dbQueryI->bindParam(":L2_P",$Longi);


$Fields = array($PlaceName,$PlaceAddress,$PlacePhone,$PlaceWebsite,
                $OOnWeekday,$OOnWeekEnd,$COnWeekday,$COnWeekEnd,
                $Lati,$Longi);
$Errors = false;
foreach ($Fields as $fieldname ) 
{
    if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) 
    {
        $response['Errors'] = 0;
        $response['message'] = "Some fields are empty so fill them..";
        die(json_encode($response));
        $Errors = true; //Yup there are errors
    }

}

if(!$Errors)
{
        $dbQueryI->execute();
        $response['Errors'] = 1;
        $response['message'] = "That's it..";
        header('Refresh: 3; url=addplaces.php');
        die(json_encode($response));

}
?>

对不起,我没有清楚地解释这个问题,因为我是afk。我是从手机发布的。 问题只是想控制字段是否为空。我有一个表格供用户使用,并希望收集一些信息。这不是整个代码,但这些是我要检查的输入。我没有关于任何添加的记录的结果。我尝试了一些不同的方法,但它们不能很好地工作。即使一些记录完全是空的插入。只是想检查所有字段是否填充然后插入。

1 个答案:

答案 0 :(得分:1)

执行此操作时:

if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) 

我认为它总是如此,因为$fieldname已经设置为post参数的,而不是参数的名称。由于json_encode()位于die()内,您可能看不到错误消息。

如果你想检查是否还没有给出任何这些,你可以将这部分代码移到脚本的顶部,但需要进行一些更改。

$Fields = array('placeName', 'addressArea', 'placePhone', 'placeWebsite',
                'openweekDay', 'openweekEnd', 'closeweekDay', 'closeweekEnd', 
                'latitude','longitude');
// Use the names as strings here

$Errors = false;
foreach ($Fields as $fieldname ) 
{
    // You can just use empty() here, because it already includes checking isset()
    if(empty($_POST[$fieldname])) {
        $response['Errors'] = 0; // Not sure why errors = 0 here
        $response['message'] = "Some fields are empty so fill them..";
        $response = json_encode($response); // Separate json_encode from die
        die($response);

        // This will never execute because the script has already died
        $Errors = true; //Yup there are errors
    }

}

然后你不必担心之后检查$Errors,因为如果有错误,脚本就已经死了。