记录在mysql数据库中插入两次

时间:2018-04-06 07:24:47

标签: php mysql

我正在尝试在我的数据库中插入一个普通的博客帖子,由于某种原因,它会插入两次相同的记录。我在下面给出了我的html和php代码。

<?php


if(isset($_POST["post"]))
{
    $conn = new mysqli("localhost","root","","yomadsdata");
    $post = $_POST["post"];
    $heading = $_POST["heading"];

    if ($conn->connect_error) 
    {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "INSERT INTO blogposts(blog_heading,blog_post) value 
    ('$heading','$post')";
    $result = $conn->query($sql);

    if ($conn->query($sql) === TRUE)
    {
        echo "New post created successfully";
    }
    else
    {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
    }

    else
   {
        echo "Come in through proper ways.";
   }

   ?>

1 个答案:

答案 0 :(得分:1)

您执行两次查询$conn->query($sql)。您可以在$result

中执行查询后删除if
$sql = "INSERT INTO blogposts(blog_heading,blog_post) value ('$heading','$post')";

if ($conn->query($sql) === TRUE) {
    echo "New post created successfully";
}

或者你可以试试这个

if ($result) {
        echo "New post created successfully";
    }