1.封装数据库添加修改删除函数
//数据添加,修改,删除操作封装
function sql_excute($sql)
{
$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if(!$conn)
{
$msg= '数据库连接失败,出错原因是'.mysqli_connect_error();
exit($msg);
}
mysqli_query($conn,"set names utf8");
$query = mysqli_query($conn,$sql);
if(!$query)
{
return false;
}
//数据库操作影响行数
$affected_rows = mysqli_affected_rows($conn);
mysqli_close($conn);
return $affected_rows;
}
2.表单提交
function add_category()
{
if(empty($_POST['catename'])||empty($_POST['slug']))
{
$GLOBALS['message']="必填数据不能为空";
return;
}
$catename = $_POST['catename'];
$slug = $_POST['slug'];
$affected_rows = sql_excute("insert into ft_category VALUES (NULL ,'{$catename}','{$slug}');");
if ($affected_rows >0)
{
$GLOBALS['message'] = '栏目名添加成功';
}else
{
$GLOBALS['message'] = '栏目名添加失败';
return;
}
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
add_category();
}