dongzhun1857 2018-01-29 20:01
浏览 147
已采纳

为什么INSERT在mysql表中的每个查询下面添加额外的空行?

My code:


    $conn = mysqli_connect($servername, $username, $password, $database);

    $n = mysqli_real_escape_string($conn, $_REQUEST['n']);
    $m = mysqli_real_escape_string($conn, $_REQUEST['m']);

    $sql = "INSERT INTO wishform (n,m) VALUES ('$n','$m');";

    if(mysqli_query($conn, $sql)) {
         echo "Your ID is created successfully!";
    } else {
         echo "Error!";
    }

    mysqli_close($conn);
    

In Table:


    308 Name1   Messege1
    309     
    310 Name2   Messege2
    311     
    312 Name3   Messege3
    313

  • 写回答

1条回答 默认 最新

  • dongzai3917 2018-01-29 20:20
    关注

    I think the problem is, you are not checking whether the data is submitted or not. Say for example, you have written this code in your PHP file as it is, without checking whether the user has submitted any data and that $_REQUEST['n'] and $_REQUEST['m'] are not empty!

    So if you are not checking whether the data is submitted or not, probably you are running that piece of code each time when the page is executed, and inserts empty data to the databse!

    Here's a simple idea to get you started:

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
        //-- get the data from $_POST array (since we are expecting the data submitted via POST method, otherwise use $_GET array)
        //-- we are trimming any extra spaces
        $n = trim( $_POST['n'] );
        $m = trim( $_POST['m'] );
    
        if( ! empty( $n ) AND ! empty( $m ) )
        {
            $conn = mysqli_connect($servername, $username, $password, $database);
    
            $n = mysqli_real_escape_string($conn, $n);
            $m = mysqli_real_escape_string($conn, $m);
    
            $sql = "INSERT INTO wishform (n,m) VALUES ('$n','$m');";
    
            if(mysqli_query($conn, $sql)) {
                 echo "Your ID is created successfully!";
            } else {
                 echo "Error!";
            }
    
            mysqli_close($conn);
        }
        else
            echo 'No data submitted!';
    
    }
    

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛