|
|
|
ขอความช่วยเหลือ! "วิธี ลบ ไฟล์รูป ออกไปพร้อมกับ Data ใน MySQL"?? |
|
|
|
|
|
|
|
คือว่าผมทำหน้า Delete.php โดยรับค่า pid มาจากหน้า show_product.php (และอื่นๆ)
แล้วในตาราง product ของผมนั่นได้เก็บชื่อรูปเอาไว้ด้วย ในช่อง p_photo ( $row_DeleteProduct['p_photo'] ) ส่วน path ของไฟล์รูป คือ 127.0.0.1/admin/p_phoro/
ตอนนี้สามารถลบข้อมูลในฐานข้อมูลได้ปกติ แต่ ติดปัญหาอยู่ตรงที่ จะทำอย่างไร ให้ลบไฟล์รูปออกไปด้วย?
ผมลองหลายวิธีแล้วแต่ไม่เป็นผล อ่ะครับ ช่วยดูให้ผมหน่อยนะครับ
Delete.php
<?php require_once('../Connections/shop_m.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
if ((isset($_GET['uid'])) && ($_GET['uid'] != "")) {
$deleteSQL = sprintf("DELETE FROM member WHERE `uid`=%s",
GetSQLValueString($_GET['uid'], "int"));
mysql_select_db($database_shop_m, $shop_m);
$Result1 = mysql_query($deleteSQL, $shop_m) or die(mysql_error());
$deleteGoTo = "show_member.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
if ((isset($_GET['cid'])) && ($_GET['cid'] != "")) {
$deleteSQL = sprintf("DELETE FROM category WHERE cid=%s",
GetSQLValueString($_GET['cid'], "int"));
mysql_select_db($database_shop_m, $shop_m);
$Result1 = mysql_query($deleteSQL, $shop_m) or die(mysql_error());
$deleteGoTo = "category.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
if ((isset($_GET['pid'])) && ($_GET['pid'] != "")) {
$deleteSQL = sprintf("DELETE FROM product WHERE pid=%s",
GetSQLValueString($_GET['pid'], "int") );
mysql_select_db($database_shop_m, $shop_m);
$Result1 = mysql_query($deleteSQL, $shop_m) or die(mysql_error());
$deleteGoTo = "show_product.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
$colname_DeleteMember = "-1";
if (isset($_GET['uid'])) {
$colname_DeleteMember = $_GET['uid'];
}
mysql_select_db($database_shop_m, $shop_m);
$query_DeleteMember = sprintf("SELECT * FROM member WHERE `uid` = %s", GetSQLValueString($colname_DeleteMember, "int"));
$DeleteMember = mysql_query($query_DeleteMember, $shop_m) or die(mysql_error());
$row_DeleteMember = mysql_fetch_assoc($DeleteMember);
$totalRows_DeleteMember = mysql_num_rows($DeleteMember);
$colname_DeleteCategory = "-1";
if (isset($_GET['cid'])) {
$colname_DeleteCategory = $_GET['cid'];
}
mysql_select_db($database_shop_m, $shop_m);
$query_DeleteCategory = sprintf("SELECT * FROM category WHERE cid = %s", GetSQLValueString($colname_DeleteCategory, "int"));
$DeleteCategory = mysql_query($query_DeleteCategory, $shop_m) or die(mysql_error());
$row_DeleteCategory = mysql_fetch_assoc($DeleteCategory);
$totalRows_DeleteCategory = mysql_num_rows($DeleteCategory);
$colname_DeleteProduct = "-1";
if (isset($_GET['pid'])) {
$colname_DeleteProduct = $_GET['pid'];
}
mysql_select_db($database_shop_m, $shop_m);
$query_DeleteProduct = sprintf("SELECT pid, p_name, p_cid, p_detail, p_price, p_photo FROM product WHERE pid = %s", GetSQLValueString($colname_DeleteProduct, "int"));
$DeleteProduct = mysql_query($query_DeleteProduct, $shop_m) or die(mysql_error());
$row_DeleteProduct = mysql_fetch_assoc($DeleteProduct);
$totalRows_DeleteProduct = mysql_num_rows($DeleteProduct);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Delete</title>
</head>
<body>
</body>
</html>
<?php
mysql_free_result($DeleteMember);
mysql_free_result($DeleteCategory);
mysql_free_result($DeleteProduct);
?>
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2012-05-18 13:10:24
|
|
|
|
|
Date :
2012-05-18 13:06:03 |
By :
garo |
View :
1520 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คุณต้องส่งชื่อรูปไปพร้อมกับค่าที่ใช้อ้างอิงในการลบข้อมูลด้วย หรืออาจใช้วิธีใดก็ได้ทำให้ได้ชื่อรูป ก็แล้วแต่นะ
Code (PHP)
<?
unlink(" ที่อยู่โฟล์เดอร์/ชื่อรูป ");
?>
|
|
|
|
|
Date :
2012-05-18 14:29:35 |
By :
บังเอิญผ่านมาเห็น |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่างนี้มีการ delete ไฟล์ด้วยครับ
Code (PHP)
<?
//*** Update Record ***//
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "UPDATE files ";
$strSQL .=" SET NAME = '".$_POST["txtName"]."' WHERE FilesID = '".$_GET["FilesID"]."' ";
$objQuery = mysql_query($strSQL);
if($_FILES["filUpload"]["name"] != "")
{
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"myfile/".$_FILES["filUpload"]["name"]))
{
//*** Delete Old File ***//
@unlink("myfile/".$_POST["hdnOldFile"]);
//*** Update New File ***//
$strSQL = "UPDATE files ";
$strSQL .=" SET FilesName = '".$_FILES["filUpload"]["name"]."' WHERE FilesID = '".$_GET["FilesID"]."' ";
$objQuery = mysql_query($strSQL);
echo "Copy/Upload Complete<br>";
}
}
?>
Go to : PHP สร้างฟอร์มสำหรับ Upload รูปภาพลงในฐานข้อมูล MySQL พร้อมกับแบบ Form สำหรับการแก้ไขรูปภาพ
|
|
|
|
|
Date :
2012-05-18 16:26:35 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ ผมทำได้แล้วครับ
/* ผมตัด Code ส่วนที่ไม่ประโยชน์ออก แล้วก็เพิ่ม
Code (PHP)
if ((isset($_GET['p_photo'])) && ($_GET['p_photo'] != "")) {
$del_photo = $_GET['p_photo'];
@unlink("p_photo/". $del_photo);
}
แล้วก็กลับไปแก้ไข Link ในหน้า show_product เป็น
Code (PHP)
<a href="delete.php?pid=<?php echo $row_ShowProduct['pid']; ?>&&p_photo=<?php echo $row_ShowProduct['p_photo']; ?>">ลบ</a>
โดยให่ส่งค่า p_photo มาแยกต่างหาก ...เลยได้ผลครับ ...ส่วนวิธีอื่นๆ พอเจอ Code Delete เข้าไป Code ลบรูปก็จะไม่ทำงาน
Delete.php
<?php require_once('../Connections/shop_m.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
if ((isset($_GET['p_photo'])) && ($_GET['p_photo'] != "")) {
$del_photo = $_GET['p_photo'];
@unlink("p_photo/". $del_photo);
}
if ((isset($_GET['uid'])) && ($_GET['uid'] != "")) {
$deleteSQL = sprintf("DELETE FROM member WHERE `uid`=%s",
GetSQLValueString($_GET['uid'], "int"));
mysql_select_db($database_shop_m, $shop_m);
$Result1 = mysql_query($deleteSQL, $shop_m) or die(mysql_error());
$deleteGoTo = "show_member.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
if ((isset($_GET['cid'])) && ($_GET['cid'] != "")) {
$deleteSQL = sprintf("DELETE FROM category WHERE cid=%s",
GetSQLValueString($_GET['cid'], "int"));
mysql_select_db($database_shop_m, $shop_m);
$Result1 = mysql_query($deleteSQL, $shop_m) or die(mysql_error());
$deleteGoTo = "category.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
if ((isset($_GET['pid'])) && ($_GET['pid'] != "")) {
$deleteSQL = sprintf("DELETE FROM product WHERE pid=%s",
GetSQLValueString($_GET['pid'], "int"));
mysql_select_db($database_shop_m, $shop_m);
$Result1 = mysql_query($deleteSQL, $shop_m) or die(mysql_error());
$deleteGoTo = "show_product.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Delete</title>
</head>
<body>
</body>
</html>
|
|
|
|
|
Date :
2012-05-18 21:41:52 |
By :
garo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เก่งครับ
|
|
|
|
|
Date :
2012-05-18 23:59:02 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|