mariadb 请求php文件插入

wlzqhblo  于 2023-10-20  发布在  PHP
关注(0)|答案(1)|浏览(153)

你能帮助我理解为什么我的代码会导致双重插入吗?我有一个JavaScript函数,它向save.php文件发出一个请求,以将数据插入数据库,但每次我提交它都会做两次,但只返回一个id,不知道第二次插入是从哪里进行的。
JavaScript/javascript函数

function opnActPag () {
    var actDiv = document.getElementById('act_div');
    var actLstCont = document.getElementById('act_lst_cont');
    var actTag = document.getElementById('act_tag');
    var actTxt = document.getElementById('act_txt');
    var actClrTxt = document.getElementById('act_clr_txt');
    var actSubmTxt = document.getElementById('act_subm_txt');
    var actTxtArea = document.getElementById('act_txt_area');
    var actHidVal = document.getElementById('act_hid_val').value;
    var actHidUsr = document.getElementById('act_hid_usr').value;
    actDiv.style.display = 'block';
    opnActPag['closeActPag'] = function () {
        actDiv.style.display = 'none';
        actTxtArea.value = '';
    }
    opnActPag['addAct'] = function () {
        $.ajax({
            type: 'POST',
            url:  'save.php',
            data: { act_txt: actTxtArea.value), id: actHidVal, usr: actHidUsr },
            success : function (data) {
                console.log(data);
            }
        });
    }
    
}

数据来自

<div class="act-back-drop" id="act_div">
    <div class="act-cont">
        <div class="act-page">
            <div class="act-cls-btn" onclick="opnActPag.closeActPag()">&times;</div>
            <div class="act-list-cont" id="act_lst_cont">
                <!--<div class="act-list-buble"><div class="act-buble-tag" id="act_tag">DATE NAME</div><div class="act-buble-text" id="act_txt">TEXT HOLDER</div></div>-->
            </div>
            <div class="act-text-cont">
                <textarea rows="6" class="act-text-inp" id="act_txt_area"></textarea>
                <input type="hidden" value="" id="act_hid_val">
                <input type="hidden" value="Some user" id="act_hid_usr">
                <div class="act-text-btn-cont">
                    <div class="act-text-btn-no" id="act_clr_txt_" onclick="document.getElementById('act_txt_area_').value = '';">&#215;</div>
                    <div class="act-text-btn-ok" id="act_subm_txt" onclick="opnActPag.addAct()">&#10004;</div>
                    <!--<div class="act-text-btn"></div>-->
                </div>
            </div>
        </div>
    </div>
</div>

save.php

<?php
if( isset($_POST['act_txt']) && !empty($_POST['act_txt']) ){
    $sql = "select count(*) as count from actions where user_name = '".$_POST['usr']."' and info = '".preg_replace( "/[\r\n]+/", " ",htmlentities(trim($_POST['act_txt']), ENT_QUOTES))."' and level_3_id =".$_POST['id'].";";
    $ret = poke_db($sql,0,1);
    if( $ret[0]['count'] == 0 ){
        $sql = "insert into actions (user_name,info,level_3_id) values('".$_POST['usr']."','".preg_replace( "/[\r\n]+/", " ",htmlentities(trim($_POST['act_txt']), ENT_QUOTES))."','".$_POST['id']."');";
        $ret = poke_db($sql,0,0);
    }
    echo $ret;
}
?>

php db连接函数

$srv = "localhost";
$usr = "root";
$pas = "pass";
$db_name = "dbname";
function poke_db($sql,$multi,$array){
    global $db_name;
    global $srv;
    global $usr;
    global $pas;
    $servername = $srv;
    $username = $usr;
    $password = $pas;
    $dbname = $db_name;
    $conn = mysqli_init();
    $conn->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
    $conn->real_connect($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die('[ERR] '.$conn->connect_error);
    }
    if( !$conn -> query($sql) ){
        die('[ERR] ' . $conn -> error);
    }
    if( $multi == 1 ){
        $result = mysqli_multi_query($conn, $sql);
    }else{
        $result = mysqli_query($conn, $sql);
        $id = mysqli_insert_id($conn);
    }
    if( $array == 1 ){
        if( mysqli_num_rows($result) != 0 ){
            $i=0;
            while($row = mysqli_fetch_assoc($result)){
                $out_array[$i] = $row;
                ++$i;
            }
        }else{
            $out_array = null;
        }
        return $out_array;
    }else{
        if( isset($id) ){
            return $id;
        }
    }
}

谢谢你

wqlqzqxt

wqlqzqxt1#

我发现了为什么我的代码做双插入。在我的php db连接中,

if( !$conn -> query($sql) ){
    die('[ERR] ' . $conn -> error);
}

通过测试查询是否无意中已插入记录。把它从代码中删除,问题就解决了。是什么让我意识到这不是一个问题是php中的另一个代码块,它也插入了两条记录,所以它必须是唯一的一件事。

相关问题