product_preview.php

// EDIT / DELETE MODE
$kategori = isset($_POST['kategori']) ? $_POST['kategori'] : '';	  

if(!empty($_POST['id']) && !empty($_POST['mode'])){
if(!$_POST['mode'] == 'edit'){
	$output = mysql_query("SELECT * FROM kategori WHERE id=".$_POST['id']) or die(mysql_error());
	$data = mysql_fetch_array($output);
	$kategori = $data['nama_kategori'];		
	echo $kategori;
	}
	elseif(!$_POST['mode'] == 'delete'){
	$output = mysql_query("DELETE * FROM produk WHERE id=".$_POST['id']) or die(mysql_error());
	$confirmation = $output ? "Data has been deleted" : "Fail to delete data"; 	
	}
}
?>        
        
        
<h2><center>Product Preview</center></h2>
<p> <form method = "post" action = "product_edit.php" >
<p> <input name="tambah_produk" type="submit" value="Tambah Produk" /> </p>
</form>
<table border="1" cellpadding="2">
<tr>
<td>No </td>
 <td>Nama Produk</td>
 <td>Harga</td>
 <td>Tgl Masuk</td>
 <td>Aksi </td>
</tr>
	<?php 
	include ("includes/koneksi.php");
	
	$i = 1;
	$output = mysql_query("SELECT * FROM produk") or die(mysql_error());
	while($data = mysql_fetch_array($output)){
	?>

	<tr>
        	<td><?php echo $i."</br>";?>
	<td><?php echo $data['nama_produk']."</br>";?></td>
           <td><?php echo $data['harga']."</br>";?></td>
           <td><?php echo date('j-M-Y',strtotime($data['tgl_masuk']))."</br>";?></td>
		<td>
             <a href="./product_edit.php?id=<?php echo $data['id_produk'];?>&mode=edit">Edit</a>  
            <a href="./product_preview.php?id=<?php echo $data['id_produk'];?>&mode=delete">Delete</a>
            </td>            	
       </tr>    

	
<?php
$i++;	
}

?>

When I press delete button, why nothing happens?

elseif(!$_POST['mode'] == 'delete'){
	$output = mysql_query("DELETE * FROM produk WHERE id=".$_POST['id']) or die(mysql_error());
	$confirmation = $output ? "Data has been deleted" : "Fail to delete data";

should I use $_POST or $_GET ?

In your code the mode is set by the link, so it's $_GET but you should not use it to change data, you should use $_POST. Bye.

You are saying if the $_POST doesn't equal delete, then delete and you have also attached your delete query to a variable which I am not sure works in itself but definitely won't do anything the way you have it as the query isn't being exectued. Change it to:

elseif($_POST['mode'] == 'delete'){
	$output = "DELETE * FROM produk WHERE id=".$_POST['id'];
        mysql_query($output) or die (mysql_error());
	//$confirmation = $output ? "Data has been deleted" : "Fail to delete data"; 	
	}

Also var_dump($_POST) to make sure your mode has a value and that it is correct. I see you have used !$_POST == 'edit' which will also need changing.

@davy_yg
with your code you have to write:

$_GET['mode'] == 'delete'

bye.

// I think this is from var_dump($_POST)
array(0) { }

// I do not know what it's mean. The delete mode has not function yet.

ok, I change it to:

<?php


// EDIT / DELETE MODE
$kategori = isset($_GET['kategori']) ? $_GET['kategori'] : '';	  


if(!empty($_GET['id']) && !empty($_GET['mode'])){
	if($_GET['mode'] == 'edit'){
		$output = mysql_query("SELECT * FROM kategori WHERE id=".$_GET['id']) or die(mysql_error());
		$data = mysql_fetch_array($output);
		$kategori = $data['nama_kategori'];		
		echo $kategori;
		}
	elseif($_GET['mode'] == 'delete'){
		$output = mysql_query("DELETE * FROM produk WHERE id=".$_GET['id']) or die(mysql_error());
		$confirmation = $output ? "Data telah terdelete" : "Gagal menghapus data"; 	
		}
}
?>

I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM produk WHERE id=7' at line 1

You can't use * in a delete query and you haven't closed your speech marks after $_GET or closed the speech marks for the query, change it to

$output = mysql_query("DELETE FROM produk WHERE id='".$_GET['id']."'") or die(mysql_error());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.