删除并将新记录插入数据库而不刷新

时间:2011-11-07 19:36:13

标签: jquery

我正在尝试使用jquery和ajax从我的数据库中添加和删除记录(游戏)。 我仍然是一名初级软件工程师,并且学习了Web开发的基础知识,但我完成了这项任务,并且不确定我缺少什么工作。

我的数据库中有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);
?>

这是我第一次发帖提问,所以如果我做了一些不可接受的事情,或者本来应该做的事情,我会很感激提示,帮助,链接,或者如果有人可以帮我解决问题的方法我的问题。 我可以从数据库中读取,但不能动态地添加或删除它。

1 个答案:

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