dr9379 2013-12-31 12:50
浏览 20

if语句将2个字段插入db

My question is too long winded to find an answer. Please excuse me. Maybe I just don't know how to abbreviate my question. Well here is the problem and and the question. See the code below:

<?php 
    if(isset($_POST['submit'])) {
        $name = $_POST['name'];
        $email = $_POST['email'];

        $server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
        //$dbc = mysqli_select_db($server, 'newsletter') or die("There is a problem accessing the databse!");
        $query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");

        $name = strip_tags($name);
        $email = strip_tags($email);

        if(empty($name) || empty($email)) {
            echo "All Fields Must Be Filled In!";
        } else {
            echo "You have been successfully subscribed";
        }
    }
    mysqli_close($server);
?>

This code works. It gets the information to the database when the user fills out the form. What I noticed is that when the user only fills in one field, i.e. name and leaves the email field empty the if statement returns the line "All Fields Must Be Filled In". Then you return to the page and fill in the email field and everything is fine. But when I check the database I notice that there is an insertion of an id and name for the attempt where the email was not filled out on the form. Is this normal? Or what am I doing wrong. Thanks

  • 写回答

5条回答 默认 最新

  • drk7700 2013-12-31 12:52
    关注

    change this

    if(empty($name) || empty($email))
    

    into this

    if(empty($name) && empty($email))
    

    || pipe sign mean OR in your condition you are telling that either name is empty or email is empty but in && and sign we are telling that either name is empty and email is empty return error.

    $name = strip_tags($_POST['name']);
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图