2011-11-07 98 views
0

我想從我的數據庫使用jquery和ajax添加和刪除記錄(遊戲)。 我還是一名初級軟件工程師,學習網絡開發的基礎知識,但我有這個任務來完成和確定我缺少的工作。刪除並插入新記錄到數據庫而不刷新

我從我的數據庫中有5列,ID是隱藏的自動增量。

我的問題是什麼是不是讓我添加或刪除遊戲,並反映在數據庫中的代碼中損壞?

這是我的主要文件與調用刪除和添加遊戲。

  <script type = "text/javaScript" src ="http://code.jquery.com/jquery-    latest.js""></script> 
     <script type = "text/javascript"> 
      $(document).ready(function(){ 
      $("add games").Submit(function() 
      $("#add").load("add.php"); { 
     var id = $("#ID").val(); 
     var name = $("#name").val(); 
     var type = $("#type").val(); 
     var rating = $("#score").val(); 
     var release = $("Yreleased").val(); 
     var dataString = 'ID=' + id 'name='+ name + '&username=' + type + '&password=' + rating +       '&gender=' + release; 
     if (id =='' || name == '' || type == '' || rating == '' || release == '') 
     { 
     alert("please enter some valid data for your game entry"); 
     } 
     else 
     $.ajax({ 
     type: "POST", 
     url: "add.php", 
     data: dataString, 
     success: function(){ 
     $('.success').fadeIn(200).show(); 
     $('.error').fadeOut(200).hide(); 
     } 
     }); 
     } 
     return false; 
     }); 
     }); 



     </script> 


     <body> 
     <?php 

     include("dbconfig.php"); 
     $sql = "SELECT * FROM games"; 
     $result = mysql_query($sql); 
     while ($record = mysql_fetch_array($result)){ 

     echo "</br> Game ID: " .$record['ID']. "</br> Game Name: " .$record['Name']. 
     "<br /> Game Type: ".$record['Type']. "<br /> Rating: ".$record['Rating']."<br /> Year Released: ".$record['Release Year']."<br /> <br />" ?> 


    <a href="#" id="<?php echo $record["ID"]; ?>" class="deletebutton"><img src="trash.png" alt="delete"/></a> 






    <?php 
     } 

     ?> 

     <form name="add" id ="add" action="add.php" method="post"> 
     <input class ="gameID" type="hidden" id="ID" value = " ' .$record['ID'] . ' " /> 
     <b>Game Name: </b> <input type="text" id="name" size=70> 
     <b>Game Type:</b> <input type="text" id="type" size=40> 
     <b>Rating: </b> <input type="number" id="score" min="1.0" max="10.0" step ="0.1"/> 
     <b>Year Released: </b> <input type="number" min="1900" max="2011" id="Yreleased" value="1985" size=4> 


     <p><input type="submit" name="Submit" id = "Submit" value="Add Game" class = "add games"></p> 
     </form> 



     <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 

     <script type="text/javascript" 
     $(document).ready(function() { 

     $("a.deletebutton").click(function(){ 

     var del_id = element.attr("id"); 
     var info = 'id=' + del_id; 
     var parent = $(this).parent(); 
     if(confirm("Sure you want to delete this game? !")) 
     { 
     $.ajax({ 
     type: "get", 
     url: "delete.php", 
     data: parent.attr('id').replace('record-',"), 
     success: function(){ 
     } 
     }); 
     $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast") 
     .animate({ opacity: "hide" }, "slow"); 
     } 
      return false; 
      }); 
      }); 
      </script> 




      </body> 

和我有附加非常簡單的PHP文件,刪除和對DBCONFIG

爲delete.php

<?php 

// This is a sample code in case you wish to check the username from a mysql db table 
include("dbconfig.php"); 
if(($_GET['id']) 
{ 
$id=$_GET['id']; 

$sql = "DELETE from games where ID='.(int)$_GET['id']; 
$result= mysql_query($sql); 






} 

?> 

,這是我add.php和我dbconfig.php到底

<?php 
include("dbconfig.php"); 

if(isset($_POST['Submit']) 

$name=$_POST['name']; 
$type=$_POST['type']; 
$rating=$_POST['score']; 
$release=$_POST['Yreleased']; 
$sql = ("INSERT INTO games VALUES ($name, $type, $rating, $release)"); 

echo " your game has been added to the list of games. "; 
?> 

和最後我的DBCONFIG

<?php 
$user_name = "root"; 
$password = ""; 
$database = "gamebook"; 
$server = "localhost"; 


$bd = mysql_connect($server, $user_name, $password) or die ('I cannot connect to the  database because:' . mysql_error()); 
mysql_select_db($database); 
?> 

這是我第一次發佈一個問題並提出問題,所以如果我做了一件不可接受的事情,或者應該做其他事情,我會很感激提示,幫助,鏈接,或者如果有人能幫助我如何解決我的問題。 我可以從數據庫中讀取數據,但不能動態添加或刪除數據。

+0

有什麼問題嗎? –

+0

我無法動態刪除記錄,而無需刷新數據並從刪除數據庫 中添加遊戲。 我不能添加一個遊戲,並看到它反映在數據庫中。 所有的代碼正在做的權利是從數據庫中讀取並顯示記錄。我看到每個記錄下的刪除按鈕,但它不起作用 – deli10

+0

請將您的問題縮小到特定組件,並對其進行正確格式化。 –

回答

0

delete.php

<?php 
    require('dbconfig.php'); //we cannot continue without this file, thats why using require instead of include 
    if(isset($_GET['id'])){ 
    $id=(int)$_GET['id']; 
    $sql = 'DELETE FROM `games` WHERE `ID`="'.$id.'" LIMIT 1'; 
    mysql_query($sql); 
    echo 'deleted'; 
    } 
?> 

add.php

<?php 
    require('dbconfig.php'); //we cannot continue without this file, thats why using require instead of include 

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

    $name=addslashes($_POST['name']); 
    $type=addslashes($_POST['type']); 
    $rating=addslashes($_POST['score']); 
    $release=addslashes($_POST['Yreleased']); 
    $sql = 'INSERT INTO `games` (`Name`,`Type`,`Rating`,`Release Year`) VALUES ("'.$name.'", "'.$type.'", "'.$rating.'", "'.$release.'")'; 
    mysql_query($sql); 
    if(!mysql_errno()) 
     echo " your game has been added to the list of games. "; 
    } 
?> 

的index.php採取從這裏:http://pastebin.com/NVbheAJZ

+0

非常感謝。我重新讀取了你發佈的代碼並修復了我的代碼。儘管它沒有解決問題,但它們更加整潔了 4個字段,你在表格中寫的是正確的。 如果我嘗試添加一個遊戲,我得到這個錯誤:解析錯誤:語法錯誤,意外的'{'在第8行C:\ xampp \ htdocs \ add.php .. 刪除我按下按鈕以刪除下每條記錄,但它不會帶我到任何地方..如果有提示調試我的代碼,以及如何做到這一點......任何提示,將不勝感激。並感謝你的回覆0leg太..我會開始看我的index.php現在看看問題 – deli10

+0

即時通訊建議您爲您的瀏覽器(FF或Chrome)使用http://getfirebug.com/插件然後當您按下按鈕時,您可以看到ajax呼叫。我希望它有幫助。 – 0leg

+0

對不起,我的錯。請更新我的答案中的add.php,它會起作用。 – 0leg

相關問題