使用 php 显示提交后错误消息的逻辑方法

发布于 2024-09-15 16:39:57 字数 1542 浏览 5 评论 0原文

//deal with individual form section posts
//-->Faction Name
if(isset($_POST['factionname'])){
    $unsani_faction_name = $_POST['faction'];
    $new_faction_name = str_replace(",", "", $unsani_faction_name);
    $faction_name = mysql_real_escape_string($new_faction_name);
    $faction_name = preg_replace('/\s\s+/', ' ', $faction_name);//strips excess white space
    $faction_name = stripslashes($faction_name);//strips slashes from name

    //remove special chars except: "& $ £ ^ - ( )"
    $faction_name = preg_replace('/[^a-z0-9\s£&$\^\(\)-]/i', '', $faction_name);  
    $string_length = strlen($faction_name);

    if($string_length < 0 || $string_length > 20) { 
        echo '<strong>Error:</strong> Property name needs to be between 1-20 characters.&nbsp;'; 
    }else { 
        $sql = mysql_query("SELECT * FROM ".TBL_USERPROPBANKS." WHERE prop_name='$prop_name'"); 
        $num_rows = mysql_num_rows($sql);
        if ($num_rows > 0) { 
            echo '<strong>Error:</strong> Bank with the same name in existance.&nbsp;'; 
        }else {  
            mysql_query("UPDATE ".TBL_USERPROPBANKS." SET prop_name='$prop_name' WHERE prop_id='$bankid'"); 
            header("Location: bank_cp.php?bankid=".$bankid."&section=settings");
        }
    }

我正在使用上述方法解决我的错误。 (在您看来)最合乎逻辑的方法是什么:

  1. 计算错误数量
  2. 并在布局的单独部分中回显/打印它们以在列表中显示每个错误消息?

目前我能想到的就是将空值分配给唯一的变量,然后如果它不满足我的验证要求,则用唯一的错误消息填充它(将有 20 多个不同的错误)。对这个有什么想法吗?

//deal with individual form section posts
//-->Faction Name
if(isset($_POST['factionname'])){
    $unsani_faction_name = $_POST['faction'];
    $new_faction_name = str_replace(",", "", $unsani_faction_name);
    $faction_name = mysql_real_escape_string($new_faction_name);
    $faction_name = preg_replace('/\s\s+/', ' ', $faction_name);//strips excess white space
    $faction_name = stripslashes($faction_name);//strips slashes from name

    //remove special chars except: "& $ £ ^ - ( )"
    $faction_name = preg_replace('/[^a-z0-9\s£&$\^\(\)-]/i', '', $faction_name);  
    $string_length = strlen($faction_name);

    if($string_length < 0 || $string_length > 20) { 
        echo '<strong>Error:</strong> Property name needs to be between 1-20 characters. '; 
    }else { 
        $sql = mysql_query("SELECT * FROM ".TBL_USERPROPBANKS." WHERE prop_name='$prop_name'"); 
        $num_rows = mysql_num_rows($sql);
        if ($num_rows > 0) { 
            echo '<strong>Error:</strong> Bank with the same name in existance. '; 
        }else {  
            mysql_query("UPDATE ".TBL_USERPROPBANKS." SET prop_name='$prop_name' WHERE prop_id='$bankid'"); 
            header("Location: bank_cp.php?bankid=".$bankid."§ion=settings");
        }
    }

I'm working out my errors using the above method.
What is (in your opinion) the most logical way to:

  1. Counting the number of errors
  2. And echoing/printing them inside a separate section of my layout to show each error message in a list?

All I can think of at the moment is assigning null values to unique vars then filling it with the unique error message if it does not meet my validation requirements (there will be 20+ different errors). Any ideas on this one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2024-09-22 16:39:57

我将创建一个空数组,将错误推送到其中,然后简单地从中创建一个字符串并显示它。

$errors = array();
...
if($string_length < 0 || $string_length > 20) {
  $errors[] = '<strong>Error:</strong> Property name needs to be  between 1-20 characters. ';
}
...
if($num_rows > 0) {
  $errors[] = '<strong>Error:</strong> Bank with the same name in existance. ';
}

// lower in a place you display the errors
echo implode('<br />', $errors);

I would create an empty array onto which I would push the errors and later on simply make a string out of it and display it.

$errors = array();
...
if($string_length < 0 || $string_length > 20) {
  $errors[] = '<strong>Error:</strong> Property name needs to be  between 1-20 characters. ';
}
...
if($num_rows > 0) {
  $errors[] = '<strong>Error:</strong> Bank with the same name in existance. ';
}

// lower in a place you display the errors
echo implode('<br />', $errors);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文