<?php
/*
提供数据库操作相关的函数
*/
//数据库连结
function con_db()
{
$con = mysql_connect("localhost", "root", "root");
if(!$con)
{
return -1;
}
mysql_select_db("bms", $con);
return($con);
}
//管理员登录帐号检查
function CheckAdmin($username, $password)
{
$rcode = "";
$con = con_db();
if($con == -1)
{
$rcode = "数据库连接失败";
return $rcode;
}
$sql = "select * from admin where username='" . $username
. "' and password='" . $password . "'";
$result = mysql_query($sql, $con);
$row = mysql_fetch_array($result);
if(empty($row[0]))
{
$rcode = "用户名或密码不正确,请重新输入";
$_SESSION['login_info'] = $rcode;
}
mysql_close($con);
return $rcode;
}
//修改管理员信息
function UpdateAdmin($username, $password)
{
$rcode = "";
$con = con_db();
if($con == -1)
{
$rcode = "数据库连接失败";
return $rcode;
}
$sql = "update admin set password='" . $password
. "' where username='" . $username . "'";
mysql_query($sql, $con);
mysql_close($con);
return $rcode;
}
///////////////////////////////////////////////////////////////////////////////
/*
用户管理相关
用户的增、删、查、改
*/
///////////////////////////////////////////////////////////////////////////////
//添加用户信息
function AddUser($user)
{
$rcode = "";
$dbcon = con_db();
if($dbcon == -1)
{
$rcode = "数据库连接失败";
return $rcode;
}
$sql = "select * from user where uid='" . $user['user_ID'] . "'";
if($row=mysql_fetch_array(mysql_query($sql, $dbcon)))
{
$rcode = "该用户已存在";
}
else
{
$sql = "insert into user(uid, name, tel, address)
values('" . $user['user_ID'] . "','" . $user['user_name']
. "','" . $user['user_tel'] . "','" . $user['user_address'] . "')";
if(!mysql_query($sql, $dbcon))
$rcode = "插入数据失败";
else
$rcode = "";
}
mysql_close($dbcon);
return $rcode;
}
//删除用户信息
function DeleteUser($uid)
{
$rcode = "";
$con = con_db();
if($con == -1)
{
$rcode = "数据库连接失败";
return $rcode;
}
$sql = "delete from user where uid='" . $uid . "'";
if(!mysql_query($sql, $con))
{
$rcode = "数据删除失败";
}
mysql_close($con);
return $rcode;
}
//根据不同的条件组合查寻用户信息,返回查寻集合
function FindUser($user, $fx, $fn)
{
$con = con_db();
if($con == -1)
{
die('连接数据库失败' . mysql_error());
}
//根据ID、姓名、电话查寻
$sql = "select * from user ";
if($user['user_ID'] !="" || $user['user_name']!="" || $user['user_tel']!="")
{
$sql = $sql . "where ";
$fir = 0; //标记是否是第一个条件,0是/1不是
if($user['user_ID']!="")
{
$sql = $sql . "uid like '%" . $user['user_ID'] . "%' ";
$fir = 1;
}
if($user['user_name']!="")
{
if($fir == 1)
$sql = $sql . "and ";
$sql = $sql . "name like '%" . $user['user_name'] . "%' ";
$fir = 1;
}
if($user['user_tel']!="")
{
if($fir == 1)
$sql = $sql . "and ";
$sql = $sql . "tel like '%" . $user['user_tel'] . "%' ";
$fir = 1;
}
}
//限定分页
$sql = $sql . " limit " . $fx . "," . $fn;
$result = mysql_query($sql, $con);
mysql_close($con);
return $result;
}
//修改用户信息
function UpdateUser($user)
{
$rcode = "";
$con = con_db();
if($con == -1)
{
$rcode = "数据库连接失败";
return $rcode;
}
$sql = "update user set uid='" .$user['user_ID'] .
"', name='" . $user['user_name'] .
"', tel='" . $user['user_tel'] .
"', address='" . $user['user_address'] .
"' where uid='" . $user['user_ID'] . "'";
if(!mysql_query($sql, $con))
{
$rcode = "更新数据失败";
}
mysql_close($con);
return $rcode;
}
//统计用户总量
function TotalUser($user)
{
$con = con_db();
if($con == -1)
{
die('连接数据库失败' . mysql_error());
}
//根据ID、姓名、电话查寻
$sql = "select count(*) from user ";
if($user['user_ID'] !="" || $user['user_name']!="" || $user['user_tel']!="")
{
$sql = $sql . "where ";
$fir = 0; //标记是否是第一个条件,0是/1不是
if($user['user_ID']!="")
{
$sql = $sql . "uid like '%" . $user['user_ID'] . "%' ";
$fir = 1;
}
if($user['user_name']!="")
{
if($fir == 1)
$sql = $sql . "and ";
$sql = $sql . "name like '%" . $user['user_name'] . "%' ";
$fir = 1;
}
if($user['user_tel']!="")
{
if($fir == 1)
$sql = $sql . "and ";
$sql = $sql . "tel like '%" . $user['user_tel'] . "%' ";
$fir = 1;
}
}
$result = mysql_query($sql, $con);
mysql_close($con);
return $result;
}
///////////////////////////////////////////////////////////////////////////////
/*
图书管理相关
图书的增、删、查、改
*/
///////////////////////////////////////////////////////////////////////////////
//添加图书信息
function AddBook($book)
{
$rcode = "";
$con = con_db();
if($con == -1)
{
$rcode = "数据库连接失败";
return $rcode;
}
$sql = "select * from book where isdn='" . $book['book_ISDN'] . "'";
if(mysql_fetch_array(mysql_query($sql, $con)))
{
$rcode = "该图书已存在";
}
else
{
$sql = "insert into book(isdn, name, press, writer, locate, year, num, cnum, detail)
values('" . $book['book_ISDN'] . "','" . $book['book_name']
. "','" . $book['book_press'] . "','" . $book['book_writer']
. "','" . $book['book_locate'] . "'," . $book['book_year']
. "," . $book['book_num'] . "," . $book['book_num'] . ",'" . $book['book_detail'] . "')";
if(!mysql_query($sql, $con))
$rcode = "插入数据失败" . $sql;
else
$rcode = "";
}
mysql_close($con);
return $rcode;
}
//删除图书信息
function DeleteBook($isdn)
{
$rcode = "";
$con = con_db();
if($con == -1)
{
$rcode = "数据库连接失败";
return $rcode;
}
$sql = "delete from book where isdn='" . $isdn . "'";
if(!mysql_query($sql, $con))
{
$rcode = "数据删除失败";
}
mysql_close($con);
return $rcode;
}
//根据不同的条件组合查寻图书信息,
//书名、作者、出版社均可模糊查寻
//返回查寻集合
function FindBook($book, $fx, $fn)
{
$con = con_db();
if($con == -1)
{
die('连接数据库失败' . mysql_error());
}
//根据书名、作者、出版社、出版年限结合查寻
$sql = "select * from book ";
if($book['book_name'] !="" || $book['book_writer']!="" ||
$book['book_press']!="" || $book['book_year']!="")
{
$sql = $sql . "where ";
$fir = 0; //标记是否是第一个条件,0是/1不是
if($book['book_name']!="")
{
$sql = $sql . "name like '%" . $book['book_name'] . "%' ";
$fir = 1;
}
if($book['book_writer']!="")
{
if($fir == 1)
$sql = $sql . "and ";
$sql = $sql . "writer like '%" . $book['book_writer'] . "%' ";
$fir = 1;
}
if($book['book
- 1
- 2
- 3
- 4
- 5
- 6
前往页