有人可以批评我的代码吗? [英] Could someone please critque my code?
问题描述
大家好。首先让我说我是php的新手,我正在寻找不使用OOP的最佳编码方式。我有以下代码,我想知道是否有更好的方法来做到这一点。我的大脑的逻辑方面说这不是真的。
我想要做的是错误检查。我有13个字段需要验证,你可以看到,我有13个POST变量。脚本结束检查所有字段是否为空,然后将插入到数据库中。
有人可以提供帮助吗?我将非常感激。
[PHP] if(isset($ _ POST [''inc''])){
if($ _ POST [ ''inc''] ==""){
$ _POST [''err1''] =" err1";
}
}
if(isset($ _ POST [''source''])){
if($ _ POST [''source''] == "){
$ _POST [''err2''] =" err2";
}
}
if(isset($ _ POST [''happen_Date''])){
if($ _ POST [''happen_Date''] ==""){
$ _POST [''err3''] =" err3" ;;
}
}
if(isset ($ _POST [''happen_Time''])){
if($ _ POST [''happen_Time''] ==""){
$ _POST [''err4''] =" err4" ;;
}
}
if(isset($ _ POST [''ap' '))){
if($ _ POST [''ap''] ==""){
$ _POST [''err5 ''] =" err5" ;;
}
}
if(isset($ _ POST [''first_Name''])) {
if($ _ POST [''first_Name''] ==""){
$ _POST [''err6''] =" err6" ;
}
}
if(isset($ _ POST [''last_Name''])){
if($ _ POST [''last_Name''] ==""){
$ _POST [''err7''] =" err7";
}
}
if(isset($ _ POST [''address1''])){
if($ _ POST ['' address1''] ==""){
$ _POST [''err8''] =" err8";
}
}
if(isset($ _ POST [''address2''])){
if($ _ POST [''address2''] ==" "){
$ _POST [''err9''] =" err9" ;;
}
}
if(isset($ _ POST [''zip_Code''])){
if($ _ POST [''zip_Code''] ==""){
$ _POST [''err10''] =" err10";
}
}
if (isset($ _ POST [''phone_Type''])){
if($ _ POST [''phone_Type''] ==""){
$ _POST [''err11''] =" err11" ;;
}
}
if(isset($ _ POST ['' area_Code''])){
if($ _ POST [''area_Code''] ==""){
$ _POST [''err12'' ] =" err12" ;;
}
}
if(isset($ _ POST [''phone_Number''])){
if($ _ POST [''phone_Number''] ==""){
$ _POST [''err13''] =" err13"; <如果(isset($ _ POST [''inc''])&& (isset($ _ POST [''source''])&&(isset($ _ POST [''happen_Date''])&&(isset($ _ POST [''happen_Time''])&& ;(isset($ _ POST [''ap''])&&(isset($ _ POST [''first_Name''])&&(isset($ _ POST [''last_Name''])& &(isset($ _ POST [''address1''])&&(isset($ _ POST [''address2''])&&(isset($ _ POST [''zip_Code''])& ;&(isset($ _ POST [''phone_Type''])&&(isset($ _ POST [''area_Code''])&&&(isset($ _ POST [''phone_Number'']) ))))))))))))){
if($ _ POST [''inc'']!==""){
if($ _ POST [''source'']!==""){
if($ _ POST [''happen_Date'']!==""){
if($ _ POST [''happen_Time'']!==""){
if($ _ POST [''ap'']!==" "){
if($ _ POST [''first_Name'']!==""){
if($ _ POST [' 'last_Name'']!==""){
if($ _ POST [''address1'']!==""){
if($ _ POST [''address2'']!==""){
if($ _ POST [''zip_Code'']!==""){
if($ _ POST [''phone_Type'']!==""){
if($ _ POST [''area_Code'']!==" "){
if($ _ POST [''phone_Number'']!==""){
echo" INSERT";
}
}
}
}
}
}
}
}
}
}
}
}
}
} [/ PHP]
Hi everyone. Let me start by saying I am pretty new to php and I am looking for the optimal way to code without using OOP. I have the following code that I wrote and I would like to know if there is a better way to do this. The logical side of my brain says that this is not really right.
What I am trying to do is error checking. I have 13 fields that need to be validated and as you can see, I have 13 POST vars. The end of the script checks that all the fields are not blank and then will insert into the db.
Can someone please offer a hand? I would be very grateful.
[PHP] if(isset($_POST[''inc''])){
if($_POST[''inc''] == ""){
$_POST[''err1''] = "err1";
}
}
if(isset($_POST[''source''])){
if($_POST[''source''] == ""){
$_POST[''err2''] = "err2";
}
}
if(isset($_POST[''occurred_Date''])){
if($_POST[''occurred_Date''] == ""){
$_POST[''err3''] = "err3";
}
}
if(isset($_POST[''occurred_Time''])){
if($_POST[''occurred_Time''] == ""){
$_POST[''err4''] = "err4";
}
}
if(isset($_POST[''ap''])){
if($_POST[''ap''] == ""){
$_POST[''err5''] = "err5";
}
}
if(isset($_POST[''first_Name''])){
if($_POST[''first_Name''] == ""){
$_POST[''err6''] = "err6";
}
}
if(isset($_POST[''last_Name''])){
if($_POST[''last_Name''] == ""){
$_POST[''err7''] = "err7";
}
}
if(isset($_POST[''address1''])){
if($_POST[''address1''] == ""){
$_POST[''err8''] = "err8";
}
}
if(isset($_POST[''address2''])){
if($_POST[''address2''] == ""){
$_POST[''err9''] = "err9";
}
}
if(isset($_POST[''zip_Code''])){
if($_POST[''zip_Code''] == ""){
$_POST[''err10''] = "err10";
}
}
if(isset($_POST[''phone_Type''])){
if($_POST[''phone_Type''] == ""){
$_POST[''err11''] = "err11";
}
}
if(isset($_POST[''area_Code''])){
if($_POST[''area_Code''] == ""){
$_POST[''err12''] = "err12";
}
}
if(isset($_POST[''phone_Number''])){
if($_POST[''phone_Number''] == ""){
$_POST[''err13''] = "err13";
}
}
if(isset($_POST[''inc'']) && (isset($_POST[''source'']) && (isset($_POST[''occurred_Date'']) && (isset($_POST[''occurred_Time'']) && (isset($_POST[''ap'']) && (isset($_POST[''first_Name'']) && (isset($_POST[''last_Name'']) && (isset($_POST[''address1'']) && (isset($_POST[''address2'']) && (isset($_POST[''zip_Code'']) && (isset($_POST[''phone_Type'']) && (isset($_POST[''area_Code'']) && (isset($_POST[''phone_Number''])))))))))))))){
if($_POST[''inc''] !==""){
if($_POST[''source''] !==""){
if($_POST[''occurred_Date''] !==""){
if($_POST[''occurred_Time''] !==""){
if($_POST[''ap''] !==""){
if($_POST[''first_Name''] !==""){
if($_POST[''last_Name''] !==""){
if($_POST[''address1''] !==""){
if($_POST[''address2''] !==""){
if($_POST[''zip_Code''] !==""){
if($_POST[''phone_Type''] !==""){
if($_POST[''area_Code''] !==""){
if($_POST[''phone_Number''] !==""){
echo "INSERT";
}
}
}
}
}
}
}
}
}
}
}
}
}
} [/PHP]
推荐答案
_POST [''inc'' ])){
if(
_POST[''inc''])){
if(
_POST [''inc''] ==""){
_POST[''inc''] == ""){
_POST [''err1''] =" err1";
}
}
if(isset(
_POST[''err1''] = "err1";
}
}
if(isset(
这篇关于有人可以批评我的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!