2013-07-10 192 views
1

嘿我遇到了我的SQL/PHP/JAVA應用程序中的一個小問題我希望你們可以幫助:) 我有一個Java應用程序,當運行連接到我的網站時,Java應用程序驗證它正在運行它與我的網站交談,我的網站將會話Id分配給Java應用程序和網站本身。MySQL插入然後更新表數據

很酷我們到目前爲止好嗎?

好的我的java應用程序定期發送數據到一個名爲Dashboard.php的頁面我想要做的是將數據保存到我的Mysql表中,然後當我的Java應用程序從Dashboard.php接收到新數據時, sessionID是相同的我想表更新到剛剛收到的新數據

這裏是我到目前爲止雖然它不工作的PHP。

function update($script_name, $version, $runtime, $status, $ranged, $attack, $defense, $strength, $magic, $sessionID, $username) 
{ 
    global $db; 
    $sql = "SELECT * FROM Dashboard WHERE session_id = '$sessionID'"; 
    try { 
     $results = $db->query($sql); 
     if ($results->rowCount() <= 0) { 
      $query = "INSERT INTO Dashboard (script_name, version, runtime, status, ranged, attack, defense, strength, magic, session_id, username) VALUES ('$script_name', '$version', '$runtime', '$status', '$ranged', '$attack', '$defense', '$strength', '$magic', '$sessionID', $username)"; 
      $db->exec($query); 
     } else { 
      foreach ($results as $row) { 
       $timerunnew = $row['runtime'] + $runtime; 
       $v4new = $row['ranged'] + $range; 
       $v5new = $row['attack'] + $attack; 
       $v6new = $row['defense'] + $defense; 
       $v7new = $row['strength'] + $strength; 
       $v8new = $row['magic'] + $magic; 
      } 
      $db->exec("UPDATE Dashboard SET `runtime` = $timerunnew, `ranged` = $v4new, `attack` = $v5new, `defense` = $v6new, `strength` = $v7new, `magic` = $v8new WHERE session_id = '$sessionID'"); 
     } 
    } catch (PDOException $ex) { 
     echo "fail"; 
    } 
} 

香港專業教育學院還試圖用ON DUPLICATE KEY UPDATE value = VALUES(value)但我有沒有運氣沒有任何人有一個解決方案進行試驗?任何幫助,將不勝感激

+1

在你的參數中你有'$ ranged',但你使用'$ range' o n:'$ v4new = $ row ['ranged'] + $ range;' – Timber

+0

哦哇,我沒有看到讓我改變這一點,並測試出來 –

回答

0

如果這是記錄可以被插入到Dashboard表的唯一方法,那麼它是不可能的兩個記錄共享同一session_id(保存爲SELECTINSERT之間出現的競爭冒險命令)。在這種情況下,你應該:

  1. 確保有上session_id定義的UNIQUE鍵:

    ALTER TABLE Dashboard ADD UNIQUE KEY (session_id); 
    
  2. 使用INSERT ... ON DUPLICATE KEY UPDATE,最好有一個適當的參數化準備好的語句:

    $qry = $db->prepare(' 
        INSERT INTO Dashboard (
        script_name, version, runtime, status, ranged, attack, 
        defense, strength, magic, session_id, username 
    ) VALUES (
        :script_name, :version, :runtime, :status, :ranged, :attack, 
        :defense, :strength, :magic, :session_id, :username 
    ) ON DUPLICATE KEY UPDATE 
        runtime = runtime + VALUES(runtime), 
        attack = attack + VALUES(status), 
        defense = defense + VALUES(defense), 
        strength = strength + VALUES(strength), 
        magic = magic + VALUES(magic) 
    '); 
    
    $qry->execute([ 
        ':script_name' => $script_name, 
        ':version'  => $version, 
        ':runtime'  => $runtime, 
        ':status'  => $status, 
        ':ranged'  => $ranged, 
        ':attack'  => $attack, 
        ':$defense' => $defense, 
        ':strength' => $strength, 
        ':magic'  => $magic, 
        ':session_id' => $sessionID, 
        ':username' => $username 
    ]); 
    
+0

非常感謝你,我會給這個鏡頭!並看看它是否有效! –