gpt4 book ai didi

php - 使用 AJAX、MySQL 和 PHP 添加、删除、更新

转载 作者:行者123 更新时间:2023-11-29 23:19:46 24 4
gpt4 key购买 nike

我的应用程序中的 Ajax 和 MySQL 遇到了一些问题。我的应用程序是类似于用户管理器的东西。我有 MySQL 数据库,其中包含 ID、用户名、密码、名字、姓氏和电子邮件字段。我将用户列表分页,每页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>

**

数据.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 文件:**

分页.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";

}

关于php - 使用 AJAX、MySQL 和 PHP 添加、删除、更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27428676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com