PHP:isset和!空的捷径? [英] PHP: a short cut for isset and !empty?
问题描述
我想知道是否有更好的想法来解决下面的问题,
我有一个带有多个输入字段的表单,比如
< input name =pg_titletype =textvalue =/>
< input name =pg_subtitletype =textvalue =/>
< input name =pg_descriptiontype =textvalue =/>
< input name =pg_backdatetype =textvalue =/>
等
但是有时在我的表单中不需要某些输入字段,例如,我只需要我的数据库注入的页面标题,
< input name =pg_titletype =textvalue =/>
等
我有另一个php页面来处理 $ _POST
data,
$ pg_title = null;
$ pg_subtitle = null;
$ pg_description = null;
$ pg_backdate = null;
if(isset($ _ POST ['pg_title'])&&!empty($ _ POST ['pg_title']))$ pg_title = $ _POST ['pg_title'];
if(isset($ _ POST ['pg_subtitle'])&&!empty($ _ POST ['pg_subtitle']))$ pg_subtitle = $ _POST ['pg_subtitle'];
if(isset($ _ POST ['pg_description'])&&!empty($ _ POST ['pg_description']))$ pg_description = $ _POST ['pg_description'];
if(isset($ _ POST ['pg_backdate'])&&!empty($ _ POST ['pg_backdate']))$ pg_backdate = $ _POST ['pg_backdate'];
每次我都要检查 $ _ POST
不是空的,否则它的变量将被设置为 null
,这样我不会注入一个空的空间到我的分贝。
我发现 是否有任何默认的php函数到'缩短'上面的过程?或者我必须编写一个用户定义函数来处理这个问题? 或者有另外一种方法可以做到这一点? 感谢。 编辑: 非常感谢帮助。 只需在我的php页面中处理$ _POST数据的额外代码, 当您看到 感谢: - ) 然后使用: I wonder if there any better ideas to solve the problem below, I have a form with a number of input fields, such as, But sometimes don't need certain input fields above in my form, for instance, I only need the page title for my db injection, And I have another php page to handle the Every time I will have to check if the I find the Is there any default php function to 'shorten' the process above? Or do I have to write an user define function to handle this? Or maybe there is another way doing this? Thanks. EDIT: Thanks so much guys for the help. Just some extra code in my php page that handle the $_POST data, as you see that Thanks :-) You can use a simple function Then use:
这篇关于PHP:isset和!空的捷径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! isset
和 $!$!
$ b
$ sql =
UPDATE root_pages
SET
pg_url =?,
pg_title =?,
pg_subtitle =?,
pg_backdate =?,
pg_description =?,
...
updated_by =?
WHERE pg_id =?
;
$ result = $ connection-> run_query($ sql,array(
$ pg_url,
$ pg_title,
$ pg_subtitle,
$ pg_backdate ,
$ pg_description,
...
$ pg_id
));
$ pg_subtitle,$ pg_backdate,$ pg_description等
总是出现在我的查询中。所以如果我得到 $ pg_subtitle =''
而不是 $ pg_subtitle = null
,那么当我没有数据时,我的数据库记录将为该列有一个空的空间。
$ p $函数post_value_or($ key,$ default = NULL){
return isset($ _ POST [$ key])&& !empty($ _ POST [$ key])? $ _POST [$ key]:$ default;
}
$ pg_title = post_value_or('pg_title');
//或使用默认
$ pg_title = post_value_or('pg_title','无标题');
<input name="pg_title" type="text" value="" />
<input name="pg_subtitle" type="text" value="" />
<input name="pg_description" type="text" value="" />
<input name="pg_backdate" type="text" value="" />
etc
<input name="pg_title" type="text" value="" />
etc
$_POST
data,$pg_title = null;
$pg_subtitle = null;
$pg_description = null;
$pg_backdate = null;
if(isset($_POST['pg_title']) && !empty($_POST['pg_title']) ) $pg_title = $_POST['pg_title'];
if(isset($_POST['pg_subtitle']) && !empty($_POST['pg_subtitle']) ) $pg_subtitle = $_POST['pg_subtitle'];
if(isset($_POST['pg_description']) && !empty($_POST['pg_description']) ) $pg_description = $_POST['pg_description'];
if(isset($_POST['pg_backdate']) && !empty($_POST['pg_backdate']) ) $pg_backdate = $_POST['pg_backdate'];
$_POST
of certain input field is set and not empty
, otherwise its variable will be set to null
, so that I won't inject an empty space into my db.isset
and !empty
in the if-condition are very repetitive when I have a long list of variable to handle.$sql = "
UPDATE root_pages
SET
pg_url = ?,
pg_title = ?,
pg_subtitle = ?,
pg_backdate = ?,
pg_description = ?,
...
updated_by = ?
WHERE pg_id = ?
";
$result = $connection->run_query($sql,array(
$pg_url,
$pg_title,
$pg_subtitle,
$pg_backdate,
$pg_description,
...
$pg_id
));
$pg_subtitle, $pg_backdate, $pg_description, etc
always present in my query. so if I get $pg_subtitle = ''
instead of $pg_subtitle = null
when there is no data in it, my db record will have an empty space for that column.function post_value_or($key, $default = NULL) {
return isset($_POST[$key]) && !empty($_POST[$key]) ? $_POST[$key] : $default;
}
$pg_title = post_value_or('pg_title');
// OR with a default
$pg_title = post_value_or('pg_title', 'No Title');