多个Mysqli插入一个表单提交

时间:2015-04-12 18:18:41

标签: php database mysqli

我试图以某种形式保存用户填写的数据。

问题是,当我按下提交按钮时,所有数据都按照我想要的方式保存,但它会多次保存。 (多行中的相同数据)

    <?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $share = $_POST['share'];
        $date = $_POST['date'];
        $dealtype = $_POST['dealtype'];
        $enter = $_POST['enter'];
        $exitdeal = $_POST['exitdeal'];
        $no1 = $_POST['no1'];
        $no2 = $_POST['no2'];
        $profit = $_POST['profit'];

        if ($share != "")
        {
            if ($date != "")
            {
                if ($dealtype != "" )
                {
                    if ($enter != "" )
                    {
                        if ($exitdeal != "" )
                        {
                            $mysql = "INSERT INTO markettable (share, date, dealtype, enter, exitdeal, no1, no2, profit) 
                            VALUES ('$share','$date', '$dealtype', '$enter', '$exitdeal', '$no1', '$no2', '$profit')";
                        }
                    }
                }
            }
        }
        $mysqli->query($mysql);
        if ($mysqli->query($mysql) === TRUE) {
            echo "New record created successfully";
        } else {
        echo "Error: " . $mysql . "<br>" . $mysqli->error;
        }
        $mysqli->close();
    }

?>

2 个答案:

答案 0 :(得分:1)

看这里,

  $mysqli->query($mysql);

  if ($mysqli->query($mysql) === TRUE) {
       echo "New record created successfully";
   } 

$mysqli->query($mysql);首先插入数据,再次插入if ($mysqli->query($mysql) === TRUE)

您将代码更改为

     $result =  $mysqli->query($mysql); // save the result in $result variable
     if ($result === TRUE) { // checks if its successfully inserted
           echo "New record created successfully";
      } 

答案 1 :(得分:0)

您调用查询执行两次,第一次执行if语句,第二次调用if语句条件。