Rodrigo Pacheco
Rodrigo Pacheco

Reputation: 33

Check if the whole row exists

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?

Upvotes: 0

Views: 67

Answers (3)

StackSlave
StackSlave

Reputation: 10627

PHP on a separate page should look more like:

<?php
if(isset($_POST['submitAddEvents'])){
  // obviously these other things are also already set unless there's a hack
  $tA = trim($_POST['teamA']); $tB = trim($_POST['teamA']);
  $eD = trim($_POST['eventDate']); $tE = trim($_POST['timeOfEvent']);
  $lV = trim($_POST['live']);
  // Verify if the data exists
  if($ta !== '' && $tB !== '' && $eD !== '' && $tE !== '' && $lV !== ''){
    // I like to save steps
    $db->query("INSERT events (teamA, teamB, eventDate, `time`, live) VALUES ('$tA', '$tB', '$eD', '$tE', '$lV')");
  }
  // doesn't look like you need to query the information you just entered - code above assumes STRING like data in MySQL, which may not be the case since we can't see your database
}
?>

Upvotes: 0

Ergec
Ergec

Reputation: 11824

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.

Upvotes: 1

JYoThI
JYoThI

Reputation: 12085

if you need to show ajax response from server side. you just echo anything in server side . you will get that in ajax success alert(re); like this

Ajax:

success:function(re)
        {
              alert(re); //here you get the message from server side 

                window.location = "addEvents.php";
        }

PHP:

In php you have to add that if condition to check if data is duplicate or not like this

<?php

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

        if ($stmt->rowCount()==0)
        {



            $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');

            echo "inserted succesfully ";


        }
        else
        {


            echo "not inserted because data duplicate";
        }

       }

    }
    ?>

Upvotes: 0

Related Questions