这是我的功能add_product
function add_product() {
if(isset($_POST['publish'])) {
$product_title = escape_string($_POST['product_title']);
$product_category_id = escape_string($_POST['product_category_id']);
$product_price = escape_string($_POST['product_price']);
$product_quantity = escape_string($_POST['product_quantity']);
$product_description = escape_string($_POST['product_description']);
$short_desc = escape_string($_POST['short_desc']);
$product_image = $_FILES['file']['name'];
$image_temp_location = $_FILES['file']['tmp_name'];
move_uploaded_file($image_temp_location, UPLOAD_DIRECTORY . DS . $product_image );
$query = query("INSERT INTO products(product_title, product_category_id,
product_price, product_quantity, product_description, short_desc, product_image) VALUES('{$product_title}', '{$product_category_id}',
'{$product_price}', '{$product_quantity}', '{$product_description}', '{$short_desc}', '{$product_image}')");
$last_id = last_id();
confirm($query);
set_message("New Product with id {$last_id} was Added");
redirect("index.php?products");
}
}
这是函数get_products_in_admin
function get_products_in_admin(){
$query = query(" SELECT * FROM products");
confirm($query);
while($row = fetch_array($query)) {
$category = show_product_category_title($row['product_category_id']);
$product = <<<DELIMETER
<tr>
<td>{$row['product_id']}</td>
<td>{$row['product_title']}<br>
<a href="index.php?edit_product&id={$row['product_id']}" ><img width='100' src="../../resources/uploads{$row['product_image']}" alt=""></a>
</td>
<td>{$category}</td>
<td>{$row['product_price']}</td>
<td>{$row['product_quantity']}</td>
<td><a class="btn btn-danger"
href="../../resources/templates/back/delete_product.php?id=
{$row['product_id']}"><span class="glyphicon glyphicon-remove"></span></a>
</td>
</tr>
DELIMETER;
echo $product;
}
这是我的html(add_product.php)
<?php add_product(); ?>
<div class="col-md-12">
<div class="row">
<h1 class="page-header">
Add Product
</h1>
</div>
<form action="" method="post" enctype="multipart/form-data">
<div class="col-md-8">
<div class="form-group">
<label for="product-title">Product Title </label>
<input type="text" name="product_title" class="form-control">
</div>
<div class="form-group">
<label for="product-title">Product Description</label>
<textarea name="product_description" id="" cols="30" rows="10" class="form-
control"></textarea>
</div>
<div class="form-group row">
<div class="col-xs-3">
<label for="product-price">Product Price</label>
<input type="number" name="product_price" class="form-control" size="60">
</div>
</div>
<div class="form-group">
<label for="product-title">Product Short Description</label>
<textarea name="short_desc" id="" cols="30" rows="3" class="form-control">
</textarea>
</div>
</div><!--Main Content-->
<!-- SIDEBAR-->
<aside id="admin_sidebar" class="col-md-4">
<div class="form-group">
<input type="submit" name="draft" class="btn btn-warning btn-lg"
value="Draft">
<input type="submit" name="publish" class="btn btn-primary btn-lg"
value="Publish">
</div>
<!-- Product Categories-->
<div class="form-group">
<label for="product-title">Product Category</label>
<select name="product_category_id" id="" class="form-control">
<option value="">Select Category</option>
<?php show_categories_add_product_page(); ?>
</select>
</div>
<!-- Product Brands-->
<div class="form-group">
<label for="product-quantity">Product Quantity</label>
<input type="number" name="product_quantity" class="form-control"
size="60">
</div>
<!-- Product Tags -->
<!-- <div class="form-group">
<label for="product-title">Product Keywords</label>
<hr>
<input type="text" name="product_tags" class="form-control">
-->
<!-- Product Image -->
<div class="form-group">
<label for="product-title">Product Image</label>
<input type="file" name="file">
</div>
</aside><!--SIDEBAR-->
</form>
这是三个文件。函数内部的functions.php
(有我的函数add_product和get_products_in_admin
。最后一个是add_product.php
我有一个问题,因为上传产品后图像未显示。