douzhao7634 2015-09-07 20:03
浏览 40
已采纳

我的条件不起作用(准备好查询和POST方法+ isset函数)

I'd like to verify if an user entered something in an input tag :

here is the index.php page :

<form action="email_validation.php" method="post">
    <p>
     Enter email :  <input type="text" name="email" id="email" /><br />
    <input type="submit" value="Envoyer" />
</p>
</form>

and the email_validation.php :

 if(isset($_POST['email']))
 {
    $req = $bdd->prepare('INSERT INTO newsletter(email) VALUES(:email)');
    $req->execute(array(
    'email' => $entree = $_POST['email']
));

echo 'Email added';
}
else
{
    echo 'Enter something';
}

It seems that my condition with isset() function is not working... when I'm testing this, "Email added" is displayed even if I'm not writing anything and a blank value is added to the database . When someone write nothing, I want to display "Enter something" and don't execute the query. I searched for hours and can't resolve this... Thanks is advance !

  • 写回答

2条回答 默认 最新

  • dsf11t5u1651 2015-09-07 20:12
    关注

    You are not validating the input. You are just testing if the value exists. And it is, the _POST will have the email field, with an empty value. In order to validate it, the code should be:

    if(!empty($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $req....
    } else {
        echo "No valid email provided";
    }
    

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题