2011-11-07 100 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

相关问题