我正在使用此代码:https://github.com/husainsaify/the-cart-script-v1
我可以编辑数据库中存在的类别(演示),但我无法创建/插入新的类别/记录。
创建类别:
<?php
require_once("inc/header.php");
require_once("inc/navbar.php");
require_once("../classes/Upload.php");
// if admin is not login send him to login.php
not_login($_SESSION['admin_id'], "login.php");
?>
<div class="container padding-10">
<div id="search-container">
<h1 class="text-center text-bs-primary text-upper">Add Category</h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<?php
if (isset($_POST['name']) && isset($_FILES['image'])) {
$name = escape($_POST['name']);
// check name & image is provided
if (!empty($_POST['name']) && !empty($_FILES['image'])) {
$allowed = array("jpg","jpeg","png");
// make a new directory
$dir = md5(random_password().rand().$_FILES['image']['tmp_name']);
mkdir("../images/{$dir}/");
$upload = new Upload($_FILES['image'],"../images/{$dir}/", 2000000, $allowed);
$results = $upload->GetResult();
if ($results['type'] == "success") {
$file_name = $_FILES['image']['name'];
$insert = $db->Insert("category", "'','$name','images/{$dir}/$file_name'");
if ($insert) {
echo "<div class='alert alert-success'>Category has been Added</div>";
}else{
echo "<div class='alert alert-danger'>Error in adding category try again</div>";
}
}else{
echo "<div class='alert alert-danger'>{$results['message']}</div>";
}
}else{
echo "<div class='alert alert-danger'>Fill In all the Fields</div>";
}
}
?>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter Category Name">
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" class="form-control" id="image" name="image">
</div>
<input type="submit" class="btn btn-primary" name="add" value="Add Category">
</form>
</div>
<?php require_once("../inc/footer-nav.php"); ?>
</div>
<?php require_once("inc/footer.php"); ?>
更新类别:
<?php
require_once("inc/header.php");
require_once("inc/navbar.php");
not_login($_SESSION['admin_id'], "login.php");
// check $_GET['id']
if (isset($_GET['id'])) {
$id = escape($_GET['id']);
// check $id is a valid category id
$category = $db->Fetch("*","category","id='$id'");
if(empty($category)){
echo "<h1 class='text-center text-red'>Invalid category id</h1>";
exit();
}
}else{
echo "<h1 class='text-center text-red'>invalid url</h1>";
exit();
}
?>
<div class="container padding-10">
<div id="search-container">
<h1 class="text-center text-upper text-bs-primary">Edit `<span class='text-black'><?php echo $category['name']; ?></span>` category</h1>
<?php
if(isset($_POST['update'])){
$name = escape($_POST['name']);
if (!empty($name)) {
$update = $db->Update("category", "name='$name'", "id='$id'");
if ($update) {
echo "<div class='alert alert-success'>Updated Successfull <a href='category.php'>Go Back</a><</div>";
exit();
}else{
echo "<div class='alert alert-danger'>OOPS failed in updating name</div>";
}
}else{
echo "<div class='alert alert-danger'>Name cannot be empty</div>";
}
}
?>
<form action="edit_category.php?id=<?php echo $id; ?>" method="post">
<div class="form-group">
<label for="cname">Name</label>
<input type="text" class="form-control" name="name" value='<?php echo $category['name']; ?>'>
</div>
<div class="form-group">
<input type="submit" value="update" name="update" class="btn btn-primary">
<a href="edit_category_image.php?id=<?php echo $category['id']; ?>" class='btn btn-success'>Change Category Image</a>
</div>
</form>
</div>
<?php require_once("../inc/footer-nav.php"); ?>
</div>
<?php
require_once("inc/footer.php");
?>