|
|
|
Upload รูปภาพไม่ขึ้น ไม่เข้าใน mysql (Function move_uploaded file) |
|
|
|
|
|
|
|
อัพโหลดรูปภาพไม่ขึ้นค่ะ เหมือนกับว่ารูปภาพไม่เข้าใน file ที่เก็บ ไม่ทราบว่าเกิดจากสาเหตุอะไรค่ะ ฟ้องข้อความตามไฟล์ที่แนบมา
Code (PHP)
<?php
include("../inc/connect.php");
$sqlc = "select * from category order by cat_name asc";
$resultc = mysql_query($sqlc)or die("Error : $sqlc");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="addsave.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="600" border="1">
<tr>
<td colspan="2" align="center">Add Product</td>
</tr>
<tr>
<td width="122">Name</td>
<td width="462"><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>Detail</td>
<td><textarea name="detail" id="detail" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price" id="price" /></td>
</tr>
<tr>
<td>Category</td>
<td><select name="category" id="category">
<option value="" selected="selected">Please select</option>
<?php
while($rowc = mysql_fetch_array($resultc)){
?>
<option value="<?php echo $rowc['cat_id'] ?>"><?php echo $rowc['cat_name'] ?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td>Status</td>
<td><input name="status" type="radio" id="radio" value="1" checked="checked" />
Yes
<input type="radio" name="status" id="radio2" value="0" />
No</td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="pics" id="pics" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
**************************
Code (PHP)
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
if($_FILES['pics']['size']>0)
{
if($_FILES['pics']['size']>500*1024)
{
exit("<script>alert('รูปภาพเกิน 500 กิโลไบต์');history.back();</script>");
}
if(!strstr($_FILES['pics']['type'],'image'))
{
exit("<script>alert('รูปภาพใช้ไม่ได้');history.back();</script>");
}
}
include("../inc/connect.php");
$name = $_POST['name'];
$detail = $_POST['detail'];
$price = $_POST['price'];
$category = $_POST['category'];
$status = $_POST['status'];
echo "$name<br/>
$detail<br/>
$price<br/>
$category<br/>
$status";
if(empty($name)||empty($detail)||empty($price))
{
exit("<script>alert('ข้อความไม่ครบ');history.back();</script>");
}
if(!is_numeric($price))
{
exit("<script>alert('ราคาต้องเป็นตัวเลขเท่านั้น');history.back();</script>");
}
$sql = "insert into product set
pro_name = '$name',
pro_detail = '$detail',
pro_price = '$price',
cat_id = '$category',
status = '$status'";
mysql_query($sql)or die("Error : $sql");
if($_FILES['pics']['size']>0)
{
$time = time();
if(strstr($_FILES['pics']['type'],'jpg'))
$filename = $time.".jpg";
else if(strstr($_FILES['pics']['type'],'gif'))
$filename = $time.".gif";
else if(strstr($_FILES['pics']['type'],'x-png'))
$filename = $time.".png";
$filepath = "picfile/".$filename;
move_uploaded_file($_FILES['pics']['tmp_name'],$filepath);
$pro_id = mysql_insert_id();
$sql = "update product set pic='$filename' where pro_id='$pro_id'";
mysql_query($sql)or die("Error : $sql");
}
exit("<script>alert('Save Completely');window.location='index.php';</script>");
?>
*******************************************
</head>
<body>
<?php
include("../inc/connect.php");
$sql = "select * from product order by pro_id asc";
$result = mysql_query($sql)or die("Error : $sql");
$num = mysql_num_rows($result);
?>
<table width="100%" border="1">
<tr>
<td colspan="7" bgcolor="#99FFCC">Total : <?php echo $num ?> Records <a href="addform.php">Add Product</a></td>
</tr>
<tr>
<td width="7%" bgcolor="#99FFCC">ID</td>
<td width="13%" bgcolor="#99FFCC">Image</td>
<td width="28%" bgcolor="#99FFCC">Name</td>
<td width="12%" bgcolor="#99FFCC">Price</td>
<td width="15%" bgcolor="#99FFCC">Category</td>
<td width="8%" bgcolor="#99FFCC">Status</td>
<td width="17%" bgcolor="#99FFCC">Action</td>
</tr>
<?php
for($i=1;$i<=$num;$i++)
{
$row = mysql_fetch_array($result);
?>
<tr>
<td bgcolor="#99FFCC"><?php echo $row['pro_id'] ?></td>
<td bgcolor="#99FFCC">
<?php
$filepath = "picfile/".$row['pic'];
if(is_file($filepath))
{
echo "<img src='"
. "$filepath'"
. " width='50' height='50' />";
}
?>
</td>
<td bgcolor="#99FFCC"><?php echo $row['pro_name'] ?></td>
<td bgcolor="#99FFCC"><?php echo $row['pro_price'] ?></td>
<td bgcolor="#99FFCC"><?php echo $row['cat_id'] ?></td>
<td bgcolor="#99FFCC"><?php echo $row['status'] ?></td>
<td bgcolor="#99FFCC">
<a href="editform.php?pro_id=<?php $row['pro_id']?>">Edit</a>
<a href="delete.php?pro_id<?php $row['pro_id'] ?>" onclick="return confirm('Are you sure ?');">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
Code
CREATE TABLE `product` (
`pro_id` int(11) NOT NULL auto_increment,
`pro_name` varchar(255) NOT NULL,
`pro_detail` text NOT NULL,
`pro_price` decimal(10,2) NOT NULL,
`cat_id` int(11) NOT NULL,
`status` int(2) NOT NULL,
`pic` varchar(255) NOT NULL,
PRIMARY KEY (`pro_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
--
-- Dumping data for table `product`
--
INSERT INTO `product` VALUES (1, 'aaaaa', 'ddddd', 60.00, 5, 1, '');
INSERT INTO `product` VALUES (2, 'vvvv', 'ddddd', 78.00, 0, 1, '');
INSERT INTO `product` VALUES (3, 'xxxx', 'xxxxx', 522.00, 3, 1, '');
INSERT INTO `product` VALUES (4, 'mmm', '5555555', 555.00, 5, 1, '');
INSERT INTO `product` VALUES (5, 'ddd', 'dddd', 5211.00, 0, 1, '');
INSERT INTO `product` VALUES (6, 'ccc', 'ccccc', 55.45, 4, 1, '');
INSERT INTO `product` VALUES (7, 'ddddf', 'sssxxxxxx', 325.00, 5, 1, '');
INSERT INTO `product` VALUES (8, 'aaaa', 'dddddd', 35.45, 4, 1, '');
Tag : PHP, MySQL, HTML/CSS
|
|
|
|
|
|
Date :
2011-08-10 19:31:37 |
By :
kee |
View :
1611 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2011-08-10 19:42:49 |
By :
kee |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันหา Path ไม่เจอครับ
$filepath = "picfile/".$filename;
** ตรวจให้ดีว่าโฟลเดอร์ picfile อยู่ส่วนไหนของ web root และไฟล์ upload อยู่ส่วนไหน
ถ้าอ้าง Path ถูกต้อง ก็ไม่ Error แล้วครับ
|
|
|
|
|
Date :
2011-08-11 12:31:26 |
By :
mangkunzo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|