duanchi4544 2014-08-06 04:34
浏览 179
已采纳

防止空数据插入数据库

I have a form that provides the user with an option to post within 1 of 3 columns. If the user decides they want to post in the first column, then they click the column 1 button and my jquery script removes the other 2 input fields.

The issue is that each one of these columns are linked to its own table within the database, so every time a user enters information into 1 of the columns, it's sent to the landing page where it updates all 3 tables.

If the user where to select Column 1, then their information will be inserted in to the Column 1 table, but blank rows will also be inserted into table 2 and 3.

<?php
     $con=mysqli_connect("URL", "DB", "password","DB_Name");
      // Check connection
      if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

     // escape variables for security
     $Main= mysqli_real_escape_string($con, $_POST['Column_1']);
     $storyn1 = mysqli_real_escape_string($con, $_POST['storyn1']);

     $sql="INSERT INTO Column_1 (Link1, storyn1) 
     VALUES ('$Main', '$storyn1')";

    if (!mysqli_query($con,$sql)) {
       die('Error: ' . mysqli_error($con));
    }
    mysqli_close($con);

   $con=mysqli_connect("URL", "DB", "password","DB_Name");
   // Check connection
   if (mysqli_connect_errno()) {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

     // escape variables for security
    $MLB= mysqli_real_escape_string($con, $_POST['MLB']);
    $storyn2 = mysqli_real_escape_string($con, $_POST['storyn2']);

    $sql="INSERT INTO Column_2 (Link, storyn2) 
    VALUES ('$MLB', '$storyn2')";

    if (!mysqli_query($con,$sql)) {
       die('Error: ' . mysqli_error($con));
    }
    mysqli_close($con);

    $con=mysqli_connect("URL", "DB", "password","DB_Name");;
    // Check connection
    if (mysqli_connect_errno()) {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

     // escape variables for security
     $Column_3= mysqli_real_escape_string($con, $_POST['Column_3']);
     $storyn3 = mysqli_real_escape_string($con, $_POST['storyn3']);

     $sql="INSERT INTO Column_3 (Link3, storyn3) 
     VALUES ('$Column_3', '$storyn3')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
     }

mysqli_close($con);
?>

I feel like the reason I'm getting blank rows inserted into my table is because this page contains insert code for all 3 tables. Whats the best practice for this? Do I need to have 3 separate landing pages to house these insertion codes?

Here is the updated Code from the suggestions below:

                 // escape variables for security
                $Main= mysqli_real_escape_string($con, $_POST['Column_1']);
                $storyn1 = mysqli_real_escape_string($con, $_POST['storyn1']);

                $MLB= mysqli_real_escape_string($con, $_POST['MLB']);
                $storyn2 = mysqli_real_escape_string($con, $_POST['storyn2']);

                $Column_3= mysqli_real_escape_string($con, $_POST['Column_3']);
                $storyn3 = mysqli_real_escape_string($con, $_POST['storyn3']);

                if(!empty($_POST['Column_1']) && !empty($_POST['storyn1'])) {
                          $sql="INSERT INTO Column_1 (Link1, storyn1) 
                     VALUES ('$Main', '$storyn1')";
                    }

                    if(!empty($_POST['MLB']) && !empty($_POST['storyn2'])) {
                       $sql="INSERT INTO Column_2 (Link, storyn2) 
                VALUES ('$MLB', '$storyn2')";
                    }

                  if(!empty($_POST['Link3']) && !empty($_POST['storyn3'])) {
                        $sql="INSERT INTO Column_3 (Link3, storyn3) 
                VALUES ('$Column_3', '$storyn3')";
                    }


                ?>
  • 写回答

2条回答 默认 最新

  • dsdtumf776629385 2014-08-06 04:56
    关注

    A simple approach is to use the PHP empty() method to check if the user-submitted values are empty prior to performing the insert operation. So your codes may look like:

    <?php
        if(!empty($_POST['Column_1']) && !empty($_POST['storyn1'])) {
            // insert into table Column_1
        }
    
        if(!empty($_POST['MLB']) && !empty($_POST['storyn2'])) {
            // insert into table Column_2
        }
    
        if(!empty($_POST['Link3']) && !empty($_POST['storyn3'])) {
            // insert into table Column_3
        }
    ?>
    

    As for whether you need to have 3 separate landing pages (or forms, views etc.), it's not required; it really depends on whether your 3 columns form a cohesive, logical grouping. My suggestion is definitely to refactor your PHP codes to use classes and objects.

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

报告相同问题?

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问