通过表单插入DB时重复数据

时间:2017-11-01 13:39:45

标签: php mysql

问题是当我的脚本插入它重复数据时;比如在这张照片中它在数据库中添加了2次:

MySQL表:

mysql table

页:

page

提交时,重定向到menu_submit,插入完成后重定向到create_menu.php

这是menu_submit.php

<?php

include ("../include/layout/header.php");
include ("../include/check_connection.php");
include ("../include/check_page.php");

if(isset($_POST["submit"])) {
    $menu_name = $_POST["menu"];
    $optradio_choise = (int)$_POST["optradio"];
    $rank_name = (int)$_POST["rank"];
    $menu_name2 = mysqli_real_escape_String($conn,$menu_name);
    $query = "
        INSERT INTO `website_navbar` (
            `item_name`,
            `rank`,
            `visible`
        ) VALUES (
            '{$menu_name}',
            '{$rank_name}',
            {$optradio_choise}
        )
    ";

    $result = mysqli_query($conn, $query);
    if(mysqli_query($conn, $query) && mysqli_affected_rows($conn)) {
        echo "Menu add sucessfully";
        redirect1("mange_content.php");
    }
}

mysqli_close($conn);

?>

2 个答案:

答案 0 :(得分:4)

mysqli_query

您是否发现了{{1}} 两次的电话?

答案 1 :(得分:0)

你写mysqli_query two times这就是你的查询运行两次的原因insert two record

$result=mysqli_query($conn,$query);
if($result && mysqli_affected_rows($conn)){
     echo "Menu add sucessfully";
     redirect1("mange_content.php");
}

所以在$result

中写mysqli_query($conn,$query);而不是if condition