我想從我的數據庫使用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);
?>
這是我第一次發佈一個問題並提出問題,所以如果我做了一件不可接受的事情,或者應該做其他事情,我會很感激提示,幫助,鏈接,或者如果有人能幫助我如何解決我的問題。 我可以從數據庫中讀取數據,但不能動態添加或刪除數據。
有什麼問題嗎? –
我無法動態刪除記錄,而無需刷新數據並從刪除數據庫 中添加遊戲。 我不能添加一個遊戲,並看到它反映在數據庫中。 所有的代碼正在做的權利是從數據庫中讀取並顯示記錄。我看到每個記錄下的刪除按鈕,但它不起作用 – deli10
請將您的問題縮小到特定組件,並對其進行正確格式化。 –