drudfe0446838 2016-11-01 04:53 采纳率: 0%
浏览 11

检查整行是否存在

I would like to know what is wrong with my code.. What I'm trying to do is POST data INTO my database BUT BEFORE INSERT, check if an EQUAL row exists and if exists show an message.. I tried it:

Here's my querys at the top of the page:

if(isset($_POST['submitAddEvents'])){

    // Verify if the data exists
    if(!filter_var($_POST['teamA'] && $_POST['teamB'] && $_POST['eventDate'])){
        $stmt = $db->prepare('SELECT * FROM events WHERE teamA = :teamA AND teamB = :teamB AND eventDate = :eventDate');
        $stmt->execute(array(
            ':teamA' => $_POST['teamA'],
            ':teamB' => $_POST['teamB'],
            ':eventDate' => $_POST['eventDate'],
        ));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
    }

    $statement = $db->prepare("INSERT INTO events(teamA, teamB, eventDate, `time`, live) VALUES (:teamA, :teamB, :eventDate, :timeOfEvent, :live)");
    $statement->execute(array(
        ':teamA' => $_POST['teamA'],
        ':teamB' => $_POST['teamB'],
        ':eventDate' => $_POST['date'],
        ':timeOfEvent' => $_POST['timeOfEvent'],
        ':live' => $_POST['live'],
    ));
    $id = $db->lastInsertId('ID');
}
?>

Here's my script at the bottom of the page:

<script>
    //Saving Data
    $('#saveButton').click(function(){
        var teamA = $('#teamA').val();
        var teamB = $('#teamB').val();
        var date = $('#date').val();
        var timeOfEvent = $('#timeOfEvent').val();
        var live = "0";
        if($("#live").is(':checked'))
            live = $('#live').val();

        $.ajax({
            url  : "<?= $_SERVER['PHP_SELF'] ?>",
            type : "POST",
            async: false,
            data :
            {
                'submitAddEvents' : 1,
                'teamA' : teamA,
                'teamB' : teamB,
                'date' : date,
                'timeOfEvent' : timeOfEvent,
                'live' : live,
            },
            success:function(re)
            {
                    window.location = "addEvents.php";
            }
        });
    });
    </script>

But didn't work, the data saves into database and doesn't pass for any verification on ajax submit... Could someone tell me what's wrong with that? Can someone help me to fix it?

  • 写回答

3条回答 默认 最新

  • duanfaxin7014 2016-11-01 05:01
    关注

    You get the existing row $row = $stmt->fetch(PDO::FETCH_ASSOC); but you didn't do anything with it. Use if ($stmt->rowCount() == 0) checking if row count is zero then execute the rest of the code and insert data.

    评论

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答