SQL插入执行两次,PHP [英] SQL Insert executes twice, php

查看:62
本文介绍了SQL插入执行两次,PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找不到直接的答案.当我单击一次提交时,我的自定义wordpress php代码似乎执行了两次.单击时,代码将添加两个具有相同名称的城市.如您所见,我试图通过检查重复项来避免这种情况,但是它忽略了它...我试图将html移至某个函数,但也没有帮助.现在我的代码看起来很奇怪,仍然没有解决方案.有什么新主意吗?

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(); ?>

推荐答案

您看到这个了吗

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

以及下一行:

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

这意味着您两次运行查询.删除$db->query之一,例如:

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 */

这篇关于SQL插入执行两次,PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆