Home >Backend Development >PHP Tutorial >Why does nothing appear when submitting an empty submission?

Why does nothing appear when submitting an empty submission?

WBOY
WBOYOriginal
2016-07-06 13:53:261089browse

When I opened this page for the first time, there was no post data. In order to avoid error prompts, I added a judgment if(trim(@$_POST['num'])) on the outside. In order to avoid empty submission, I did it again. A judgment (isset($_POST['yzm'])&&trim($_POST['num'])) If there is no post data, it will prompt that there is no input, but I only enter the verification code and the text box above will write nothing. After submission, nothing will be written. Why is this not prompted? It should prompt that there is no input, right?

<code><form method="post" >
    <input type="text" name="num"><br />
    <input type="text" style="display: inline-block;width: 50px;" name="yzm"></span>
    <img id="checkpic" onclick="javascript:this.src='yzm2.php?tm='+Math.random();" src='yzm2.php' />
    <button type="submit">提交</button>
</form>
<?php
    if(trim(@$_POST['num'])){
        session_start();
        if(isset($_POST['yzm'])&&trim($_POST['num'])){
            if($_SESSION["str"]==$_POST['yzm']){
              echo "right";
              $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
              $stmt=$pdo->prepare("insert into test(num)values(:num)");
              $stmt->execute(array(':num'=>$_POST['num']));
            }else{
              echo "wrong";
            }
        }else{
          echo "还没有输入";
        }
    }
    
?></code>

Reply content:

When I opened this page for the first time, there was no post data. In order to avoid error prompts, I added a judgment if(trim(@$_POST['num'])) on the outside. In order to avoid empty submission, I did it again. A judgment (isset($_POST['yzm'])&&trim($_POST['num'])) If there is no post data, it will prompt that there is no input, but I only enter the verification code and the text box above will write nothing. After submission, nothing will be written. Why is this not prompted? It should prompt that there is no input, right?

<code><form method="post" >
    <input type="text" name="num"><br />
    <input type="text" style="display: inline-block;width: 50px;" name="yzm"></span>
    <img id="checkpic" onclick="javascript:this.src='yzm2.php?tm='+Math.random();" src='yzm2.php' />
    <button type="submit">提交</button>
</form>
<?php
    if(trim(@$_POST['num'])){
        session_start();
        if(isset($_POST['yzm'])&&trim($_POST['num'])){
            if($_SESSION["str"]==$_POST['yzm']){
              echo "right";
              $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
              $stmt=$pdo->prepare("insert into test(num)values(:num)");
              $stmt->execute(array(':num'=>$_POST['num']));
            }else{
              echo "wrong";
            }
        }else{
          echo "还没有输入";
        }
    }
    
?></code>

if(trim($_POST['num'])) You didn’t submit anything but $_POST['num'] is empty, if(false) so it didn’t go in at all. It's a very simple question.

Think about it for yourself. Your code cannot justify itself. You can use isset($_POST['yzm']) to prevent undefined subscripts, but trim($_POST['num']) if $_POST[' Isn't it also wrong that num'] is not defined? Either verify everything, or don't verify at all. This situation shows that your posture when writing code is wrong.

Writing programs is a fool's errand and is a developer's rest time. Why? Because you have to think about it first before writing, rather than writing a few words and thinking about it, thinking is work, and writing a program is just recording the results of thinking.
The problem with your code is obvious. If you had considered undefined problems before writing, then this would definitely be the case:

<code>if(isset($_POST['num']) && isset($_POST['yzm'])){
    //...处理逻辑
}</code>

But your code is not. When you typed $_POST['yzm'], you suddenly remembered that I wanted to do an undefined verification? This kind of sudden inspiration will actually completely disrupt your thinking, so I don’t recommend it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn