Home > Backend Development > PHP Tutorial > 还是上午那个空提交的问题

还是上午那个空提交的问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:06:36
Original
1039 people have browsed it

之前的问题是 比如输入框里面什么都不输入直接点击提交 应该打印的出来的是“你还没有输入” 可是我的代码打印出来的是“没有” 我听别人说把isset改成trim就好了 我试了一下 空提交时确实可以打印出来“你还没有输入” 但是问题是每次一打开这个页面时都会报错 Notice: Undefined index: num in D:wampwwwtest.php on line 13 为什么会这样?
后来试了一下 改成empty也不对 那么应该怎么办呢?而且我想要的效果是每次一打开这个页面时什么提示都没有 不显示“没有”或“你还没有输入” 谢谢

还是上午那个空提交的问题

<code>


      <meta charset="utf-8">
    <title></title>


<form method="post">
    <input type="text" name="num">
    <button type="submit">提交</button>
</form>
<?php if (isset($_POST['num'])) {
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$stmt=$pdo->prepare("select * from aaa where num=?");
$stmt->execute(array($_POST['num']));
$res=$stmt->fetchall(PDO::FETCH_ASSOC);
if($res){   
         foreach($res as $v){
          echo $v['name'];
         }
       }else{
        echo "没有";
       }
}else{
    echo "你还没有输入";
}
?>


</code>
Copy after login
Copy after login

回复内容:

之前的问题是 比如输入框里面什么都不输入直接点击提交 应该打印的出来的是“你还没有输入” 可是我的代码打印出来的是“没有” 我听别人说把isset改成trim就好了 我试了一下 空提交时确实可以打印出来“你还没有输入” 但是问题是每次一打开这个页面时都会报错 Notice: Undefined index: num in D:wampwwwtest.php on line 13 为什么会这样?
后来试了一下 改成empty也不对 那么应该怎么办呢?而且我想要的效果是每次一打开这个页面时什么提示都没有 不显示“没有”或“你还没有输入” 谢谢

还是上午那个空提交的问题

<code>


      <meta charset="utf-8">
    <title></title>


<form method="post">
    <input type="text" name="num">
    <button type="submit">提交</button>
</form>
<?php if (isset($_POST['num'])) {
$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$stmt=$pdo->prepare("select * from aaa where num=?");
$stmt->execute(array($_POST['num']));
$res=$stmt->fetchall(PDO::FETCH_ASSOC);
if($res){   
         foreach($res as $v){
          echo $v['name'];
         }
       }else{
        echo "没有";
       }
}else{
    echo "你还没有输入";
}
?>


</code>
Copy after login
Copy after login

因为在你没有提交前,POST数据是没有的,所以$_POST[‘num‘]不存在报错。在前面加个@就好了,或者先定义POST为空。

PHP版本不同造成的报错级别不同,可以在顶部入口隐藏掉notice级别的错误:

<code>error_reporting( error_reporting() & ~E_NOTICE )</code>
Copy after login

看不下去了

<code>if(empty($_POST['num'])){
    echo '未输入num';
}</code>
Copy after login
Related labels:
php
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template