查看: 778|回复: 5
|
求助~~php~~ 急
[复制链接]
|
|
我这里有个问题想请教大家。。
我的图片名(image'name)save入database的时候
只save入第一个字母而已。。
exp: image name= product.jpg
在database里只save到 “p” 我的database有足够的空间
为什么会这样?
我的coding有问题吗?
谢谢大家帮忙。。。
<?php
require_once '../../library/config.php';
require_once '../library/functions.php';
checkUser();
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
case 'addProduct' :
addProduct();
break;
case 'modifyProduct' :
modifyProduct();
break;
case 'deleteProduct' :
deleteProduct();
break;
case 'deleteImage' :
deleteImage();
break;
default :
// if action is not defined or unknown
// move to main product page
header('Location: index.php');
}
function addProduct()
{
$catId = $_POST['cboCategory'];
$name = $_POST['txtName'];
$description = $_POST['mtxDescription'];
$price = str_replace(',', '', (double)$_POST['txtPrice']);
$qty = (int)$_POST['txtQty'];
$image = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');
$mainImage = $image['image'];
$thumbnail = $image['thumbnail'];
$sql = "INSERT INTO tbl_product (cat_id, pd_name, pd_description, pd_price, pd_qty, pd_image, pd_thumbnail, pd_date)
VALUES ('$catId', '$name', '$description', $price, $qty, '$mainImage', '$thumbnail', NOW())";
$result = dbQuery($sql) or die('Cannot add product' . mysql_error());
header("Location: index.php?catId=$catId");
}
/*
Upload an image and return the uploaded image name
*/
function uploadProductImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';
// if a file is given
if (trim($image['tmp_name']) != '') {
$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
// generate a random new file name to avoid name conflict
$imagePath = $_POST['txtName'].".$ext";
$size = getimagesize($image['tmp_name']);
// list($width, $height, $type, $attr) = getimagesize($image['tmp_name']);
if ($size[0] > MAX_PRODUCT_IMAGE_WIDTH) {
$imagePath = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
} else {
// move the image to category image directory
// if fail set $imagePath to empty string
if (!move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath)) {
$imagePath = '';
}
}
}
return $imagePath;
}
/*
Modify a product
*/
function modifyProduct()
{
$productId = (int)$_GET['productId'];
$catId = $_POST['cboCategory'];
$name = $_POST['txtName'];
$description = $_POST['mtxDescription'];
$price = str_replace(',', '', $_POST['txtPrice']);
$qty = $_POST['txtQty'];
$images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');
$mainImage = $images['image'];
$thumbnail = $images['thumbnail'];
// if uploading a new image
// remove old image
if ($mainImage != '') {
_deleteImage($productId);
$mainImage = "'$mainImage'";
$thumbnail = "'$thumbnail'";
} else {
// if we're not updating the image
// make sure the old path remain the same
// in the database
$mainImage = 'pd_image';
$thumbnail = 'pd_thumbnail';
}
$sql = "UPDATE tbl_product
SET cat_id = $catId, pd_name = '$name', pd_description = '$description', pd_price = $price,
pd_qty = $qty, pd_image = $mainImage, pd_thumbnail = $thumbnail
WHERE pd_id = $productId";
$result = dbQuery($sql);
header('Location: index.php');
}
/*
Remove a product
*/
function deleteProduct()
{
if (isset($_GET['productId']) && (int)$_GET['productId'] > 0) {
$productId = (int)$_GET['productId'];
} else {
header('Location: index.php');
}
// remove any references to this product from
// tbl_order_item and tbl_cart
$sql = "DELETE FROM tbl_order_item
WHERE pd_id = $productId";
dbQuery($sql);
$sql = "DELETE FROM tbl_cart
WHERE pd_id = $productId";
dbQuery($sql);
// get the image name and thumbnail
$sql = "SELECT pd_image, pd_thumbnail
FROM tbl_product
WHERE pd_id = $productId";
$result = dbQuery($sql);
$row = dbFetchAssoc($result);
// remove the product image and thumbnail
if ($row['pd_image']) {
unlink(SRV_ROOT . 'images/product/' . $row['pd_image']);
unlink(SRV_ROOT . 'images/product/' . $row['pd_thumbnail']);
}
// remove the product from database;
$sql = "DELETE FROM tbl_product
WHERE pd_id = $productId";
dbQuery($sql);
header('Location: index.php?catId=' . $_GET['catId']);
}
/*
Remove a product image
*/
function deleteImage()
{
if (isset($_GET['productId']) && (int)$_GET['productId'] > 0) {
$productId = (int)$_GET['productId'];
} else {
header('Location: index.php');
}
$deleted = _deleteImage($productId);
// update the image and thumbnail name in the database
$sql = "UPDATE tbl_product
SET pd_image = '', pd_thumbnail = ''
WHERE pd_id = $productId";
dbQuery($sql);
header("Location: index.php?view=modify&productId=$productId");
}
function _deleteImage($productId)
{
// we will return the status
// whether the image deleted successfully
$deleted = false;
$sql = "SELECT pd_image, pd_thumbnail
FROM tbl_product
WHERE pd_id = $productId";
$result = dbQuery($sql) or die('Cannot delete product image. ' . mysql_error());
if (dbNumRows($result)) {
$row = dbFetchAssoc($result);
extract($row);
if ($pd_image && $pd_thumbnail) {
// remove the image file
$deleted = @unlink(SRV_ROOT . "images/product/$pd_image");
$deleted = @unlink(SRV_ROOT . "images/product/$pd_thumbnail");
}
}
return $deleted;
}
?> |
|
|
|
|
|
|
|
发表于 11-4-2006 06:46 AM
|
显示全部楼层
$mainImage = $image['image'];
改成
$mainImage = $image;
试试看
你的'image'可能被当成 0 了, 所以才会拿第一个字. |
|
|
|
|
|
|
|

楼主 |
发表于 12-4-2006 03:14 AM
|
显示全部楼层
谢谢belon_cfy 的帮忙。。
问题解决了。 |
|
|
|
|
|
|
|
发表于 13-4-2006 04:34 PM
|
显示全部楼层
$action = isset($_GET['action']) ? $_GET['action'] : '';
对不起,打扰了...我想问一下,请问上面写的是什么意思??我每次看到有些script之间有个问号,然后后面有冒号,请问这是什么意思呢??要在什么情况下才可以用呢?? |
|
|
|
|
|
|
|
发表于 13-4-2006 06:13 PM
|
显示全部楼层
$action = isset($_GET['action']) ? $_GET['action'] : ''
if not mistaken the meaming is >>
if (isset($_GET['action']))
$action=$_GET['action'];
else
$action =''; |
|
|
|
|
|
|
|
发表于 14-4-2006 03:05 PM
|
显示全部楼层
oh...原来...有点像javascript的code=> condition?"true":"false".
谢谢! |
|
|
|
|
|
|
| |
本周最热论坛帖子
|