Delete record sql unlink
Ik ben bezig met een site, maar nu wil ik een record en de achterliggende image verwijderen.
De record krijg ik verwijderd alleen de image blijft nog in de map staan.
Kan iemand zeggen wat er fout is in mijn code. Bvd.
Hier de code:
Quote:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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['image_id'])) && ($_GET['image_id'] != "")) {
$deleteSQL = sprintf("DELETE FROM images WHERE image_id=%s",
GetSQLValueString($_GET['image_id'], "int"));
mysql_select_db($database_koopjes, $koopjes);
$Result1 = mysql_query($deleteSQL, $koopjes) or die(mysql_error());
}
$deleteGoTo = "bewerk.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
$album_id = $image_result['album_id'];
$image_ext = $image_result['ext'];
unlink('uploads/17/'.$image_id.'.'.$image_ext);
unlink('uploads/thumbs/17/'.$image_id.'.'.$image_ext);
}
header(sprintf("Location: %s", $deleteGoTo));
?>
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['image_id'])) && ($_GET['image_id'] != "")) {
$deleteSQL = sprintf("DELETE FROM images WHERE image_id=%s",
GetSQLValueString($_GET['image_id'], "int"));
mysql_select_db($database_koopjes, $koopjes);
$Result1 = mysql_query($deleteSQL, $koopjes) or die(mysql_error());
}
$deleteGoTo = "bewerk.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
$album_id = $image_result['album_id'];
$image_ext = $image_result['ext'];
unlink('uploads/17/'.$image_id.'.'.$image_ext);
unlink('uploads/thumbs/17/'.$image_id.'.'.$image_ext);
}
header(sprintf("Location: %s", $deleteGoTo));
?>
Gewijzigd op 24/08/2012 12:00:00 door Ruud Clauwers
Zet dit helemaal boven in je script.
Waar komt $image_result['ext'] vandaan?
en meld wat je ziet. Bestaat dat bestand?
Maar krijg geen error.
Daarnaast vind ik dit stuk ook erg vreemd:
Code (php)
1
2
3
4
2
3
4
<?php
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
?>
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
?>
Volgens mij kan dat een stuk simpeler door te werken met de waardes in de $_GET array, daar moet het volgens mij allemaal uit komen.
Tot slot: waar komt $image_result vandaan. Je roept het wel aan, maar in dit deel van je script is het niet duidelijk waar het vandaan komt.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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['image_id'])) && ($_GET['image_id'] != "")) {
$deleteSQL = sprintf("DELETE FROM images WHERE image_id=%s",
GetSQLValueString($_GET['image_id'], "int"));
mysql_select_db($database_koopjes, $koopjes);
$Result1 = mysql_query($deleteSQL, $koopjes) or die(mysql_error());
}
$deleteGoTo = "bewerk.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
$image_id = $image_result['image_id'];
$image_ext = $image_result['ext'];
unlink('uploads/17/'.$image_id.'.'.$image_ext);
unlink('uploads/thumbs/17/'.$image_id.'.'.$image_ext);
}
header(sprintf("Location: %s", $deleteGoTo));
?>
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['image_id'])) && ($_GET['image_id'] != "")) {
$deleteSQL = sprintf("DELETE FROM images WHERE image_id=%s",
GetSQLValueString($_GET['image_id'], "int"));
mysql_select_db($database_koopjes, $koopjes);
$Result1 = mysql_query($deleteSQL, $koopjes) or die(mysql_error());
}
$deleteGoTo = "bewerk.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
$image_id = $image_result['image_id'];
$image_ext = $image_result['ext'];
unlink('uploads/17/'.$image_id.'.'.$image_ext);
unlink('uploads/thumbs/17/'.$image_id.'.'.$image_ext);
}
header(sprintf("Location: %s", $deleteGoTo));
?>
Toevoeging op 24/08/2012 12:15:39:
Sorry had de #image_id er niet bij gezet.
Krijg nu wel errors:
Notice: Undefined variable: image_result in /Applications/MAMP/htdocs/H&H Koopjes/delete_image_gallery.php on line 53
Notice: Undefined variable: image_result in /Applications/MAMP/htdocs/H&H Koopjes/delete_image_gallery.php on line 54
Warning: unlink(uploads/17/.) [function.unlink]: Invalid argument in /Applications/MAMP/htdocs/H&H Koopjes/delete_image_gallery.php on line 56
Warning: unlink(uploads/thumbs/17/.) [function.unlink]: Invalid argument in /Applications/MAMP/htdocs/H&H Koopjes/delete_image_gallery.php on line 57
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/H&H Koopjes/delete_image_gallery.php:53) in /Applications/MAMP/htdocs/H&H Koopjes/delete_image_gallery.php on line 61
Daar ga je dus, $image_result bestaat helemaal niet, dus er kan ook niet gedelete worden.
Hij verwijderd nu wel de record maar verwijderd gelijk alle images uit de map.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
$deleteGoTo = "bewerk.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
$image_id = $row_Recordset1['image_id'];
$image_ext = $row_Recordset1['ext'];
unlink('uploads/17/'.$image_id.'.'.$image_ext);
unlink('uploads/thumbs/17/'.$image_id.'.'.$image_ext);
?>
$deleteGoTo = "bewerk.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
$image_id = $row_Recordset1['image_id'];
$image_ext = $row_Recordset1['ext'];
unlink('uploads/17/'.$image_id.'.'.$image_ext);
unlink('uploads/thumbs/17/'.$image_id.'.'.$image_ext);
?>
Gewijzigd op 24/08/2012 12:41:23 door Ruud Clauwers
En regel 7/8 is natuurlijk onnodig variabelen copieren. Geen enkele noodzaak om dat te doen.
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<?php
mysql_select_db($database_koopjes, $koopjes);
$query_Recordset1 = "SELECT image_id, ext FROM images";
$Recordset1 = mysql_query($query_Recordset1, $koopjes) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
mysql_select_db($database_koopjes, $koopjes);
$query_Recordset1 = "SELECT image_id, ext FROM images";
$Recordset1 = mysql_query($query_Recordset1, $koopjes) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
Toevoeging op 24/08/2012 13:05:28:
Sorry voor het ongemak.
Ben nog php aan het leren.
Heb vorig jaar een avond opleiding gedaan, maar dit is niet ten sprake gekomen.
Probeer zoveel mogelijk zelf uit te vogelen.
Selecteer dus alleen het record van de foto die verwijderd moet worden.
Erwin H op 24/08/2012 13:41:15:
Selecteer dus alleen het record van de foto die verwijderd moet worden.
Uiteraard wel voordat je het record verwijderd.
:-)
Code (php)
1
2
3
2
3
<?php
<td><a href="delete_image_gallery.php?image_id=<?php echo $row_RS_products['image_id']; ?>">Delete</a></td>
?>
<td><a href="delete_image_gallery.php?image_id=<?php echo $row_RS_products['image_id']; ?>">Delete</a></td>
?>
En dan verwijderd die op de andere pagina die record, hoe kan het dan dat die dan niet die betreffende image verwijderd?
Maar een willekeurige?
Wat je moet doen, is alleen het record selecteren uit de database die je wilt verwijderen.
Ik laat het er maar bij zitten, zal zelf de images moeten verwijderen om de zoveel tijd.
Evengoed bedankt voor de hulp.
Haal de bestandsnaam op uit de database.
Delete het bestand.
Delete het record.
Wat kan daar nou moeilijk aan zijn?
Bedankt voor alle snelle reacties.