php echo exit()和可选字段

时间:2016-09-12 00:19:18

标签: php mysql echo exit

两件小事:

我有这个PHP控制脚本用于将可选字段更新到数据库中:

if(strlen($nome) == ""){$_POST['nome'] = $nome_vecchio; }
elseif(strlen($nome) < 2 || strlen($nome) > 20 )
    {echo('Se vuoi modificare il campo nome, assicurati che lo 
         stesso sia superiore ai 2 caratteri ed inferiore ai 20!</br>'); exit();}
  1. 如何在表单的同一页面中打印echo中的脚本内容?
  2. 如果我删除退出脚本“Se vuoi modificare il campo nome ...”打印在同一页面中,但数据库填充错误信息。好像控制脚本只用于打印它, 没有阻止用户。好像控制脚本只会打印我回显中包含的短语我该如何解决?

    1. 可选字段的脚本
    2. if(strlen($ nome)==“”){$ _ POST ['nome'] = $ nome_vecchio; }

      不起作用:我希望如果用户想要更新是名字,他必须插入一个包含2和20个字母的单词,但是如果他不想更新字段,他也不能填写它,并且数据库不应覆盖任何内容,保留先前输入的名称(例如,在注册阶段) 我怎么能这样做?

      编辑:

      感谢您的回答,我也尝试使用@Michael Johnson方法和@Valkyrurr方法,但没有。

      1. 剧本:

        标题('位置:“errore.php”')

      2. 不起作用!在页面errore.php我把:

        <?php
        // Includo la connessione al database
        require('config.php');
        
        if(!isset($_SESSION['login']))
        { header('Location: login.php'); exit; }
        
        echo('Se vuoi modificare il campo nome, assicurati che lo 
                     stesso sia superiore ai 2 caratteri ed inferiore ai 20!<br>');
        ?>
        

        但它比较:拒绝访问!错误403 ...

        和2.这个脚本:

        $nome = trim($nome); 
        if (strlen($nome) == 0) {
        $_POST['nome'] = $nome_vecchio;}
        

        用“”覆盖我数据库中的字段。

        为了更好地解释我处理代码的方式,我发布了你所写的所有内容,也许这是我从未见过的另一个错误,或者我从未认为是错误!我希望你能理解我的意大利面英文! :)

            if(isset($_POST['modifica_dati'])) 
            {
        $nome = $_POST['nome']; 
            $query_nome =  mysql_query("SELECT nome FROM utenti WHERE id = '" .             $_SESSION['login'] . "' LIMIT 1");  
            $row_nome = mysql_fetch_array($query_nome);
            $nome_vecchio = $row_nome['nome'];
        
            $nome = trim($nome); // Make sure there are no extra spaces at beginning or end
            if(strlen($nome) == 0){$_POST['nome'] = $nome_vecchio; }
            if(strlen($nome) < 2 || strlen($nome) > 20 )
                {
                  header('Location: "errore.php"')
                 echo('Se vuoi modificare il campo nome, assicurati che lo 
                     stesso sia superiore ai 2 caratteri ed inferiore ai 20!</br>');      exit();} 
        
            if (is_numeric($nome)) 
                {echo('Se vuoi modificare il campo nome, assicurati che lo 
                     stesso non cntenga numeri al suo interno!<br>'); exit();}   
        
            // verifico che il nome non contenga caratteri nocivi
            elseif (!preg_match('/^[A-Za-z \'-]+$/i',$nome)) {
                echo 'Il nome contiene caratteri non ammessi!<br>'; exit();}
           }
        

        哪里错了?

2 个答案:

答案 0 :(得分:1)

试试这些:

  1. 对于您的第二个if()语句,在echo exit()之前,发出重定向呼叫。像这样,header('Location: "script_name_here.php"')
  2. 条件中的strlen()会返回一个int,因此请将其与0进行比较,而不是""。像这样,if(strlen($nome) == 0){ }

答案 1 :(得分:1)

在表单页面上显示消息可以通过几种方式完成。如果显示具有处理它的相同脚本的表单,则可以将该消息放入变量中并稍后显示。当然,您需要额外的代码来防止数据库被更改(可能类似于添加$_POST['nome'] = $nome_vecchio;而不是退出。

如果脚本是独立的,也可以按@Valkyrurr的建议重定向。您需要提供一种指示错误的方法。

如其他地方所述,strlen()未正确使用。我建议像

这样的东西
$nome = trim($nome); // Make sure there are no extra spaces at beginning or end
if (strlen($nome) == 0) {
    $_POST['nome'] = $nome_vecchio;
}