使用AJAX,MySQL和PHP添加,删除,更新[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用AJAX,MySQL和PHP添加,删除,更新[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我的应用程序中有一些Ajax和mysql的问题。我的应用程序有点像用户管理器。我有带有ID,USERNAME,PASSWORD,FIRST_NAME,LAST_NAME和EMAIL字段的MySQL数据库。我列出了每页用户分页10条记录。现在我正在尝试为添加新用户,编辑现有用户以及逐个删除用户提供功能。我有下一个代码:
config.php文件
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "password";
$mysql_database = "mybase";
$con = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps error! occurred");
mysql_select_db($mysql_database, $con) or die("Opps error! occurred");
?>
这是数据库连接
**
的index.php
**
<?php
include('config.php');
$per_page = 3;
//getting number of rows and calculating no of pages
$sql = "select count(*) from users";
$result = mysql_query($sql);
$count = mysql_fetch_row($result);
$pages = ceil($count[0]/$per_page);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Users Manager</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" src="pagination.js"></script>
<style>
body { margin: 0; padding: 0; font-family:Verdana; font-size:15px }
a
{
text-decoration:none;
color:#B2b2b2;
}
a:hover
{
color:#DF3D82;
text-decoration:underline;
}
#loading {
width: 100%;
position: absolute;
}
#pagination
{
text-align:center;
margin-left:120px;
}
li{
list-style: none;
float: left;
margin-right: 16px;
padding:5px;
border:solid 1px #dddddd;
color:#0063DC;
}
li:hover
{
color:#FF0084;
cursor: pointer;
}
</style>
</head>
<body>
<div align="center">
<div style="margin-top:50px;"><b>Title</b>: users Manager</div>
<div id="content" ></div>
<table width="800px">
<tr><Td>
<ul id="pagination">
<?php
//Show page links
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
</ul>
</Td>
</tr></table>
<div id="loading" ></div>
</div>
</body>
</html>
**
data.php
**
<?php
include('config.php');
$per_page = 10;
if($_GET)
{
$page=$_GET['page'];
}
//getting table contents
$start = ($page-1)*$per_page;
$sql = "select * from users order by id limit $start,$per_page";
$rsd = mysql_query($sql);
if(isset($_POST['buttonsave'])){
$query_sql = "INSERT INTO users (username,firstname,lastname,email) VALUES ('{$_POST[username]}','{$_POST[firstname]}','{$_POST[lastname]}','{$_POST[email]}')";
$result = mysql_query($query_sql);
if($result){
echo "Successful insert";
}
}
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<table id="tbl">
<th>User Name:<input type="text" id="username" name="username" placeholder="User"></th>
<th>First Name:<input type="text" id="firstname" name="firstname" placeholder="First Name"></th>
<th>Last Name:<input type="text" id="lastname" name="lastname" placeholder="Last Name"></th>
<th>E-mail:<input type="email" id="email" name="email" placeholder="Email"></th>
</table>
<input type="button" value="Insert" id="save">
<script type="text/javascript">
$(function(){
$("#save").click(function(){
var uname = $("#username").val();
var fname = $("#firstname").val();
var lname = $("#lastname").val();
var email = $("#email").val();
$.ajax({
url: "data.php",
type: "POST",
async: true,
data: {
buttonsave: 1,
username: uname,
firstname: fname,
lastname: lname,
email: email
},
success: function(result){
alert("OK! Good!");
}
});
});
});
</script>
<table id="tbl">
<th><b>Id</b></th>
<th><b>User Name</b></th>
<th><b>First Name</b></th>
<th><b>Last Name</b></th>
<th><b>E-mail</b></th>
<?php
while($row = mysql_fetch_array($rsd))
{
$id = $row['id'];
$uname = $row['username'];
$fname = $row['first_name'];
$lname = $row['last_name'];
$email = $row['email'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $uname; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $lname; ?></td>
<td><?php echo $email; ?></td>
</tr>
<?php
} //End while
?>
</table>
<style>
#tbl
{
width:800px;
border:1px solid #2E8AE6;
margin-top:50px;
}
#tbl tr:nth-child(odd) {
background: #C2E0FF;
}
#tbl td{
border:1px solid #2E8AE6;
padding: 5px;
}
#tbl th
{
background: #2E8AE6;
border:1px solid #2E8AE6;
padding: 5px;
}
</style>
和我的jQuery文件:**
pagination.js
**
$(document).ready(function(){
//Loading Image Display
function Display_Load()
{
$("#loading").fadeIn(100);
$("#loading").html("<img src='loading.gif' />");
}
//Hide Loading Image
function Hide_Load()
{
$("#loading").fadeOut('slow');
};
//Default Starting Page Results
$("#pagination li:first").css({'color' : '#FF0084','border' : 'none'});
$("#content").load("data.php?page=1", Hide_Load());
//Pagination Click
$("#pagination li").click(function(){
Display_Load();
//CSS Styles
$("#pagination li")
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});
$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});
//Loading Data
var pageNum = this.id;
$("#content").load("data.php?page=" + pageNum, Hide_Load());
});
});
所以问题出在我的data.php中。我试图使用ajax插入数据,但它不起作用!它不会向数据库插入任何内容。但它提醒我
alert("OK! Good!");
我无法理解那是错的。请帮忙。
答案
if(isset($_POST['buttonsave'])){
$query_sql = "INSERT INTO users (id,username,first_name,last_name,email) VALUES ('','$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[email]')";
$result = mysql_query($query_sql);
if($result){
echo "Successful insert";
}
以上是关于使用AJAX,MySQL和PHP添加,删除,更新[重复]的主要内容,如果未能解决你的问题,请参考以下文章
复选框状态更改时如何更新mysql字段?使用 jquery (ajax)、php 和 mysql
AJAX PHP MYSQL 更新/编辑记录 (jQuery)