2016-11-01 41 views
0

我想知道我的代碼有什麼問題..我正在嘗試將POST數據寫入我的數據庫但是在插入之前,檢查EQUAL行是否存在以及是否存在顯示的消息。我試了一下:檢查整行是否存在

這裏是我的querys在頁面的頂部:

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'); 
} 
?> 

這裏是我的腳本在頁面的底部:

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

但沒有工作,數據保存到數據庫中,並沒有通過任何驗證ajax提交...有人可以告訴我什麼是錯的嗎?有人可以幫我解決它嗎?

+0

你會想從JavaScript發送數據到一個單獨的PHP頁面,然後'回聲json_encode($ assocArray)'從PHP頁面,那麼你會得到響應,在你的對象'成功'方法的're'參數。 – PHPglue

+0

如果你需要顯示ajax響應。你只是在服務器端迴應任何東西。你會在ajax成功提醒(重新);像這 – JYoThI

回答

1

你得到現有的行$row = $stmt->fetch(PDO::FETCH_ASSOC);,但你沒有做任何事情。使用if ($stmt->rowCount() == 0)檢查行計數是否爲零,然後執行代碼的其餘部分並插入數據。

+0

但我不想知道行數是零.. –

+0

@RodrigoPacheco你的意思是「你不想」?它被禁止嗎? – Ergec

0

如果您需要顯示服務器端的ajax響應。你只是在服務器端迴應任何東西。你會得到在阿賈克斯成功alert(re);這樣

阿賈克斯:

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

       window.location = "addEvents.php"; 
     } 

PHP:

在PHP中,你必須補充一點,如果條件檢查數據是否是重複或不像這樣

<?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"; 
     } 

     } 

    } 
    ?> 
+0

如果數據完全相同,它仍會添加我的數據而不顯示任何錯誤消息 –

0

PHP在一個單獨的頁面應該看起來更li柯:

<?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 
} 
?>