duanchun1852 2016-01-03 20:05
浏览 40
已采纳

SQL Insert执行两次,php

Couldn't find a straight answer to this. My custom wordpress php code seems to be executed twice when I click on the submit once. When clicked, the codes adds two cities with the same name. As you can see, I tried to aviod this by checking for duplicates, but it ignores it... I tried moving the html to a function, but not help there either. Now my code looks weird and still no solution. Any new ideas?

<?php get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
        // Start the loop.
        while ( have_posts() ) : the_post();

            // Include the page content template.
            get_template_part( 'template-parts/content', 'page' );

            if ( comments_open() || get_comments_number() ) {
                comments_template();
            }

            // End of the loop.
        endwhile;
        ?>

    </main><!-- .site-main -->

<?php

function showForm() {

    echo "<form name='submit' method='post'>";
    echo "<select name='countriesSelect'>
            <option value='-1'>Choose...</option>";
    $conn = new mysqli('localhost','a','b','c');
    $query = "SELECT id,country_name FROM countries";
    $result = $conn->query($query);

    while($row = $result->fetch_assoc()) {
        echo "<option value='" . $row['id'] . "'>" . $row['country_name'] . "</option>";
    }
    $conn->close();
    echo "</select><br /><br /> 

        Now choose a new city:&nbsp;&nbsp;<input type='text' name='cityname' maxlength='30' size='30' value='' style='width: 300px;' /><br /><br />

        That is it: <br />
        <input type='submit' value='Add new city' /><br /><br />
    </form>";
    return;
}

if (!empty($_POST['cityname']) && !empty($_POST['countriesSelect'])) {

    $cityname = $_POST['cityname'];
    $countriesSelect = $_POST['countriesSelect'];

    // make sure city doesn't exist - also because for some unknown reason this script is called twice =/
    $db = new mysqli('localhost','a','b','c');
    $query = "SELECT city_name FROM cities WHERE city_name='$cityname'";
    $result = $db->query($query);
    $row = $result->fetch_assoc();
    if (empty($row['city_name'])) {

        // add new city
        $query = "INSERT INTO cities (country_id, city_name, amount_raised_usd) VALUES ($countriesSelect, '$cityname', 0)";
        $result = $db->query($query);

        if ($db->query($query) === TRUE) {
            echo "<div style='color: green;'>New city added successfully. Add another?";
        } else {
            echo "<div style='color: red;'>Error: " . $query . " :: " . $db->error; 
        }

        echo '</div><br /><br />';
    }

    $db->close();
    showForm();

} else {
    showForm();
}
?>

    <?php get_sidebar( 'content-bottom' ); ?>

</div><!-- .content-area -->

<?php get_footer(); ?>
  • 写回答

2条回答 默认 最新

  • dragon19720808 2016-01-03 20:14
    关注

    Do you see this

    $result = $db->query($query);
    

    And the next line:

    if ($db->query($query) === TRUE) {
    

    This means that you run your query twice. Remove one of the $db->query, e.g.:

    $result = $db->query($query);
    if ($result === TRUE) {    /* do stuff */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算