时间:2021-07-01 10:21:17 帮助过:1人阅读
输出提示
switch ($_REQUEST)
{
case empty($_REQUEST['name']):
echo" 名程必填";
case empty($_REQUEST['reviewer']):
echo" 用户名必填";
case empty($_REQUEST['review']):
echo" 评论必填";
}
//录入数据
$mysql = new SaeMysql();
$sql = "SELECT * FROM `remark` LIMIT 10";
$data = $mysql->getData( $sql );
$name = strip_tags( $_REQUEST['name'] );
$reviewer = $_REQUEST['reviewer'];
$review = $_REQUEST['review'];
$sql = "INSERT INTO `remark` ( `name`, `review`, `reviewer`, `time`) VALUES ('" . $name . "' , '" . $review . "' , '" . $reviewer . "' , NOW() ) ";
$mysql->runSql($sql);
if ($mysql->errno() != 0)
{
die("Error:" . $mysql->errmsg());
}
$mysql->closeDb();
echo $mysql->errmsg()
?>
用户提交表单的时候会出现多个变量,如何对变量每一个变量进行非空判断,并在所有变量均为非空后才把所有数据录入数据库。
我只知道可以使用empty()进行辨别并输出提示但是不知道如何实现
所有变量均为非空后才把所有数据录入数据库。
如下,我用switch可以实现在有未填项时输出提示,但是无法实现
所有变量均为非空后再录入数据。
输出提示
switch ($_REQUEST)
{
case empty($_REQUEST['name']):
echo" 名程必填";
case empty($_REQUEST['reviewer']):
echo" 用户名必填";
case empty($_REQUEST['review']):
echo" 评论必填";
}
//录入数据
$mysql = new SaeMysql();
$sql = "SELECT * FROM `remark` LIMIT 10";
$data = $mysql->getData( $sql );
$name = strip_tags( $_REQUEST['name'] );
$reviewer = $_REQUEST['reviewer'];
$review = $_REQUEST['review'];
$sql = "INSERT INTO `remark` ( `name`, `review`, `reviewer`, `time`) VALUES ('" . $name . "' , '" . $review . "' , '" . $reviewer . "' , NOW() ) ";
$mysql->runSql($sql);
if ($mysql->errno() != 0)
{
die("Error:" . $mysql->errmsg());
}
$mysql->closeDb();
echo $mysql->errmsg()
?>
function is_empty_variable($variable){
return (!isset($variable) || trim($variable)==='');
}
如果是$_REQUEST数组的话:
foreach($_REQUEST as $key => $value)
{
if(is_empty_variable($value))
{
throw new \Exception("{$key} 为空");
}
}
$error = array(
"name" => "名称必填",
"reviewer" => "用户名必填",
"review" => "评论必填"
);
foreach($error as $name => $str) {
if(!isset($_REQUEST[$name]) || trim($_REQUEST[$name]) === "")
die($str);
}
说个题外的,也许你会有启发
isset($_REQUEST['name']) && $_REQUEST['name']