插入和動態表創建工作正常。通過按下該行上的按鈕,所有事情都會按照從數據庫中刪除記錄的方式進行。嘗試了幾種不同的方式來訪問沒有結果的值。該按鈕只是點擊,沒有任何反應。任何幫助將不勝感激! (代號爲所有四個部分如下。)爲什麼我的動態刪除不起作用?
主頁:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Test</title>
<link href="styleTest.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<header id="header"><strong>PHP Test</strong></header>
<div id="mainContent" align="center">
<form id="myForm" action="insert.php" method="post">
<table>
<tr valign="baseline">
<td>Name:</td>
<td> <input id="name" type="text" name="name"/></td>
</tr>
<tr valign="baseline">
<td>Comment:</td>
<td> <input id="comment" type="text" name="comment"/></td>
</tr>
<tr valign="baseline">
<td><input name="sub" id="sub" type="button" value="Save"></td>
</tr>
</table>
</form>
<span id="result"></span>
</div><!-- mainContent -->
</div><!-- Container -->
<script src="jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="my_script.js" type="text/javascript"></script>
</body>
</html>
插入頁:
<?php
//get the values passed by post
$name = mysql_real_escape_string($_POST['name']);
$comment = mysql_real_escape_string($_POST['comment']);
//create connection to Database
$hostname = "localhost";
$database = "test";
$username = "root";
$password = "";
$con = mysqli_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
mysqli_select_db($con,$database);
//Run the insert query and check to see if it processes
if(mysqli_query($con,"INSERT INTO info VALUES('','$name', '$comment')"))
{
echo "Successfully Inserted";
$sql2="SELECT * FROM info ORDER BY ID ASC";
$result = mysqli_query($con,$sql2);
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Comment</th>
<th>Remove Entry</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['COMMENTS'] . "</td>";
echo "<td> <form id='myForm2' method='post' action='delete.php'><input name='del' id='del'
type='button' value='Delete'><input id='id' type='hidden' name='id' value=".$row['ID']."></form></td>";
echo "</tr>";
}
echo "</table>";
}
else
echo "Save Failed!";
//close connection to database
mysqli_close($con);
?>
刪除頁:
<?php
//create connection to Database
$hostname = "localhost";
$database = "test";
$username = "root";
$password = "";
$con = mysqli_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
mysqli_select_db($con,$database);
//retrieve posted variables
$id= mysql_real_escape_string($_POST['id']);
$sql="DELETE FROM info WHERE ID = $id";
$query = mysql_query($con,$sql);
if(mysql_affected_rows()>0){
$sql2="SELECT * FROM info ORDER BY ID ASC";
$result = mysqli_query($con,$sql2);
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Comment</th>
<th>Remove Entry</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['COMMENTS'] . "</td>";
echo "<td> <form id='myForm2 method='post' action='delete.php'><input name='del' id='del' type='button' value='Delete'><input id='id' type='hidden' name='id' value=".$row['ID']."></form></td>";
echo "</tr>";
}
echo "</table>";
}else{
echo "Error deleting Data";
}
mysqli_close($con);
?>
腳本:
$(document).ready(function()
{
$("#sub").click(function()
{
//Check to see that no fields are left blank if one is all fields are cleared and save is failed from insert.php.
if ($("#name").val()=="")
{
clearInput();
return false;
}
else if($("#comment").val()=="")
{
clearInput();
return false;
}
//save the fields into an array and post to the result span then clear inputs
$.post($("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info){ $("#result").html(info);});
clearInput();
});
$('#del').click(function()
{
$.post("delete.php",$("#myForm2 :input").serialize(),function(info){ $("#result").html(info);});
});
});
$("#myForm").submit(function() {
return false;
});
如果你不得不張貼一段代碼,那麼你不會努力去調試它。 –
*旁註:*您同時使用'mysql_ *'和'mysqli_ *'庫!爲什麼? – Raptor
螢火蟲怎麼說?檢查請求的響應。 – Zarathuztra