Jump to content

[SOLVED] Table entry doesn't delete.


dezkit

Recommended Posts

I have this code:

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:/admin");
}
?>
<body bgcolor="#EEEEEE">
<h2>Upload</h2>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<table border=0>
<tr>
<td>Select File:
<td><input name="file" type="file" /><input type="submit" name="submit" value="Upload File" />
<tr>
</form>
</table><br><br>
<?php
include("config.php");
$submit = $_POST["submit"];
if($submit == "Upload File"){
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png")) || ($_FILES["file"]["type"] == "image/jpg") && ($_FILES["file"]["size"] < 20000000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    } else {
    echo "<font color=\"red\" size=\"+1\">Image has been uploaded!</font><br><br>";
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
   if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      } else {
     $filename = $_FILES["file"]["name"];
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "./uploads/" . $_FILES["file"]["name"];
      cropImage(83, 60, "./uploads/$filename", 'jpg', "./uploads/th-$filename");
      include("config.php");
      $thumbnailss = "th-".$filename;
      mysql_query("INSERT INTO gallery (thumb_url, full_url) VALUES('$thumbnailss', '$filename' ) ") or die(mysql_error());  
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
if($submit == "delete"){
for($i=0;$i<$count;$i++){
  $del_id = $checkbox[$i];
  $tbl_name="gallery"; // Table name
  $sql = "DELETE FROM $tbl_name WHERE id='$id'";
  $result = mysql_query($sql);
  echo "File has been deleted!";
}
}

$tbl_name="gallery"; // Table name
// select record from mysql
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
	case 'gif':
	$simg = imagecreatefromgif($source);
	break;
	case 'jpg':
	$simg = imagecreatefromjpeg($source);
	break;
	case 'png':
	$simg = imagecreatefrompng($source);
	break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w> $h) {
	$adjusted_width = $w / $hm;
	$half_width = $adjusted_width / 2;
	$int_width = $half_width - $w_height;
	imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif(($w <$h) || ($w == $h)) {
	$adjusted_height = $h / $wm;
	$half_height = $adjusted_height / 2;
	$int_height = $half_height - $h_height;
	imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
	imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center" bgcolor="#FFFFFF"> </td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Thumbnail</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Image URL</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><img src="./uploads/<? echo $rows['thumb_url']; ?>"></td>
<td bgcolor="#FFFFFF"><a href="./uploads/<? echo $rows['full_url']; ?>" target="_blank"><? echo $rows['full_url']; ?></a></td>
</tr>
<?

// close while loop
}

?>
</table></td>
</tr>
</table>
<input name="submit" type="submit" id="delete" value="delete"></form>
<?php
// close connection;
mysql_close();
?>

Whenever I select a couple of files, and click "delete", it doesn't delete anything, help please :)

Link to comment
https://forums.phpfreaks.com/topic/131393-solved-table-entry-doesnt-delete/
Share on other sites

First, instead of this:

for($i=0;$i<$count;$i++){
  $del_id = $checkbox[$i];
  $tbl_name="gallery"; // Table name
  $sql = "DELETE FROM $tbl_name WHERE id='$id'";
  $result = mysql_query($sql);
  echo "File has been deleted!";
}

Use this:

 foreach($checkbox as $del_id){
  $tbl_name="gallery"; // Table name
  $sql = "DELETE FROM $tbl_name WHERE id='$id'";
  $result = mysql_query($sql);
  echo "File has been deleted!";
}

 

Second, have you tried echoing some troubleshooting code?

Weird, it says "File has been deleted!" But the file is still there!

Current code:

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:/admin");
}
?>
<body bgcolor="#EEEEEE">
<h2>Upload</h2>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<table border=0>
<tr>
<td>Select File:
<td><input name="file" type="file" /><input type="submit" name="submit" value="Upload File" />
<tr>
</form>
</table><br><br>
<?php
include("config.php");
$submit = $_POST["submit"];
if($submit == "delete"){
foreach($checkbox as $del_id){
  $tbl_name="gallery"; // Table name
  $sql = "DELETE FROM $tbl_name WHERE id='$id'";
  $result = mysql_query($sql);
  echo "File has been deleted!<br>";
}
}

if($submit == "Upload File"){
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png")) || ($_FILES["file"]["type"] == "image/jpg") && ($_FILES["file"]["size"] < 20000000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    } else {
    echo "<font color=\"red\" size=\"+1\">Image has been uploaded!</font><br><br>";
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
   if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      } else {
     $filename = $_FILES["file"]["name"];
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "./uploads/" . $_FILES["file"]["name"];
      cropImage(83, 60, "./uploads/$filename", 'jpg', "./uploads/th-$filename");
      include("config.php");
      $thumbnailss = "th-".$filename;
      mysql_query("INSERT INTO gallery (thumb_url, full_url) VALUES('$thumbnailss', '$filename' ) ") or die(mysql_error());  
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}

$tbl_name="gallery"; // Table name
// select record from mysql
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
	case 'gif':
	$simg = imagecreatefromgif($source);
	break;
	case 'jpg':
	$simg = imagecreatefromjpeg($source);
	break;
	case 'png':
	$simg = imagecreatefrompng($source);
	break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w> $h) {
	$adjusted_width = $w / $hm;
	$half_width = $adjusted_width / 2;
	$int_width = $half_width - $w_height;
	imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif(($w <$h) || ($w == $h)) {
	$adjusted_height = $h / $wm;
	$half_height = $adjusted_height / 2;
	$int_height = $half_height - $h_height;
	imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
	imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center" bgcolor="#FFFFFF"> </td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Thumbnail</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Image URL</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><img src="./uploads/<? echo $rows['thumb_url']; ?>"></td>
<td bgcolor="#FFFFFF"><a href="./uploads/<? echo $rows['full_url']; ?>" target="_blank"><? echo $rows['full_url']; ?></a></td>
</tr>
<?

// close while loop
}

?>
</table></td>
</tr>
</table>
<input name="submit" type="submit" id="delete" value="delete"></form>
<?php
// close connection;
mysql_close();
?>

I fixed it, THANKS SO MUCH FOR THE HELP.

 

Changed:

 foreach($checkbox as $del_id){
  $tbl_name="gallery"; // Table name
  $sql = "DELETE FROM $tbl_name WHERE id='$id'";
  $result = mysql_query($sql);
  echo "File has been deleted!";
}

 

To

 foreach($checkbox as $del_id){
  $tbl_name="gallery"; // Table name
  $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  $result = mysql_query($sql);
  echo "File has been deleted!";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.