2013-06-18 209 views
-2

我正在開發一個項目,用戶可以點擊一個項目。如果用戶之前點擊過它,那麼當他嘗試再次點擊它時,它不應該在數據庫上工作或插入值。當我點擊第一個項目(我直接從數據庫顯示項目),它插入到數據庫,然後當我再次點擊它時,它的工作原理(給我錯誤代碼)不會插入數據庫。所有其他項目,當我點擊它們,即使我點擊第二,第三,第四次,它會插入到數據庫中。請幫助傢伙。謝謝PHP驗證提交

<?php 

session_start(); 
$date = date("Y-m-d H:i:s"); 

include("php/connect.php"); 

$query = "SELECT * FROM test ORDER BY `id` ASC LIMIT 3"; 
$result = mysql_query($query); 

if (isset($_SESSION['username'])) { 

    $username = $_SESSION['username']; 

    $submit = mysql_real_escape_string($_POST["submit"]); 

    $tests = $_POST["test"]; 

    // If the user submitted the form. 
    // Do the updating on the database. 
    if (!empty($submit)) { 
     if (count($tests) > 0) { 
      foreach ($tests as $test_id => $test_value) { 
       $match = "SELECT user_id, match_id FROM match_select"; 
       $row1 = mysql_query($match)or die(mysql_error()); 
       while ($row2 = mysql_fetch_assoc($row1)) { 
        $user_match = $row2["user_id"]; 
        $match = $row2['match_id']; 
       } 
       if ($match == $test_id) { 
        echo "You have already bet."; 
       } else { 
        switch ($test_value) { 
         case 1: 
          mysql_query("UPDATE test SET win = win + 1 WHERE id = '$test_id'"); 
          mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')"); 
         break; 
         case 'X': 
          mysql_query("UPDATE test SET draw = draw + 1 WHERE id = '$test_id'"); 
          mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')"); 
         break; 

         case 2: 
          mysql_query("UPDATE test SET lose = lose + 1 WHERE id = '$test_id'"); 
          mysql_query("INSERT INTO match_select (user_id, match_id) VALUES ('1','$test_id')"); 
         break; 
         default: 

        } 
       } 
      } 
     } 
    } 

    echo "<h2>Seria A</h2><hr/> 
     <br/>Welcome,".$username."! <a href='php/logout.php'><b>LogOut</b></a><br/>"; 

    while ($row = mysql_fetch_array($result)) { 

     $id = $row['id']; 
     $home = $row['home']; 
     $away = $row['away']; 
     $win = $row['win']; 
     $draw = $row['draw']; 
     $lose = $row['lose']; 

     echo "<br/>",$id,") " ,$home, " - ", $away; 

     echo " 
      <form action='seria.php' method='post'> 
       <select name='test[$id]'>   
        <option value=\"\">Parashiko</option> 
        <option value='1'>1</option> 
        <option value='X'>X</option> 
        <option value='2'>2</option> 
       </select> 
       <input type='submit' name='submit' value='Submit'/> 
       <br/> 
      </form> 
      <br/>";   

     echo "Totali ", $sum = $win+$lose+$draw, "<br/><hr/>"; 
    } 
} else { 
    $error = "<div id='hello'>Duhet te besh Log In qe te vendosesh parashikime ndeshjesh<br/><a href='php/login.php'>Kycu Ketu</a></div>"; 
} 

?> 
+0

請問你的錯誤代碼是什麼樣子?什麼不工作?你用'error_reporting(E_ALL);'等調試了你的代碼嗎? – ferdynator

+0

注意:未定義索引:提交在線17上的/home/content/16/11285316/html/seria.php 注意:未定義索引:在/home/content/16/11285316/html/seria.php上在線測試19 – Henri

+0

我不認爲這些線路與我的問題有什麼關係? – Henri

回答

1

你的問題是在這裏:

$match = "SELECT user_id, match_id FROM match_select"; 
$row1 = mysql_query($match)or die(mysql_error()); 
while ($row2 = mysql_fetch_assoc($row1)) { 
    $user_match = $row2["user_id"]; 
    $match = $row2['match_id']; 
} 

您沒有正確檢查它。您必須檢查match_select中的條目是否存在user_idmatch_id。否則,$match將永遠是你的數據庫等於最後插入的行的match_id領域:

$match = "SELECT * 
    FROM `match_select` 
    WHERE `user_id` = '<your_id>' 
    AND `match_id` = '$test_id'"; 
$matchResult = mysql_query($match)or die(mysql_error()); 
if(mysql_num_rows($matchResult)) { 
    echo "You have already bet."; 
} 

順便說一句,可以考慮使用PDOmysqli操縱數據庫。 mysql_功能已被棄用:

http://www.php.net/manual/fr/function.mysql-query.php

+0

感謝兄弟它的作品..但現在我有另一個問題..我包括圖片..普通頁---> http://postimg.org/image/dw7gjy63l/ ...新的錯誤頁面----> http://postimg.org/image/8yxdbkjbr/ ...(它顯示了所有db輸入條目的次數 – Henri

+0

@Henri好像你正在重新插入條目到'測試'表的地方,但我不能在你發佈的代碼中看到它。 – Brewal

+0

它在switch語句中的整個事情..我沒有其他代碼插入東西到測試 – Henri

0

如果數據已經存在,通過查找表來驗證插入記錄。

例如

簡單的方法是

$query = "SELECT * FROM match_select WHERE user_id = '$user_id'"; 
$result = mysql_query($query); 

if(mysql_num_rows($result) > 0) 
{ 
    // do not insert 
} 
else 
{ 
    // do something here.. 
} 
+1

使用全文搜索LIKE'statement查詢「ID」?爲什麼不直接使用'WHERE user_id = $ user_id' – ferdynator

+0

那麼簡單。= = – mmr

0

在您的形式你有<select name='test[$id]'>(每個項目),那麼當你提交你得到$tests = $_POST["test"];形式不需要指定索引表格並且可以簡單地做<select name='test[]'>,您最終可以添加一個隱藏字段,其ID爲<input type="hidden" value="$id"/>。第二部分是目前不太好的驗證;你可以簡單地檢查itemalready在數據庫中存在與查詢

+0

' $ id'應該是'' – ferdynator

+0

修復編輯謝謝 – Jaay