gpt4 book ai didi

php - 检查 php 上的空字段

转载 作者:行者123 更新时间:2023-11-29 12:07:13 27 4
gpt4 key购买 nike

我对我的数据库进行了一些插入操作,并想使用 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了。我是用手机发的。问题只是想控制字段是否为空。我有一个用户表单,想收集他们一些信息。这不是完整的代码,但这些是我要检查的输入。我没有关于任何添加记录的结果。我尝试了一些不同的方法,但效果不佳。甚至有些记录完全插入为空。只是想检查所有字段是否填充,然后插入。

最佳答案

当你这样做时:

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,因为如果有错误,脚本就已经死了。

关于php - 检查 php 上的空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31275948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com