Trying to delete an item pressing the button deletes the item from the bottom of the table

The code is :

<?php
function confirmadelprod($idp)
{ global $link;
	global $op;
	global $delprod;
	global $idprodus;
 	global $idp;
	
 


	$sql = "SELECT a.idprodus, a.denprod, a.um, b.dencateg
			FROM produse a INNER JOIN categorii b
				ON a.idcateg= b.idcateg
				ORDER BY a.denprod DESC
				";
	 $result = mysqli_query($link,$sql);
	 
	 $eroare = mysqli_error($link);
	 if ($eroare) echo $eroare;
	 
	 while($row = mysqli_fetch_array($result))
	 {
	 	$idprodus = $row["idprodus"];
		$denprod = $row["denprod"];
		$um = $row["um"];
		$dencateg = $row["dencateg"];
		
		//butoane adauga si sterge 
		$linkmodi = "<a href=\"admin.php?idprodus=$idprodus&op=modiprod \"> <img src='edit.png'class='butoneditdel'/ onclick='confirmsterge()'> </a>";
		$linkdel   = "<a href=\"admin.php?idprodus=$idprodus&op=delprod \"> <img src='del.png'class='butoneditdel'   /> </a>";
		//butoane adauga si sterge END

		//echo "$linkmodi | $linkdel : $idprodus | $denprod | $um | $dencateg <br />";


		
	 }	
 echo '<p>Sunteti sigur ca vreti sa stergeti produsul?</p>';
 echo "<a href=\"admin.php?idprodus=$idprodus&op=delprod \"><button>Da</button></a>";
 echo "<a href=\"admin.php?idprodus=$idprodus&op=veziproduse \"><button>Nu</button></a>";
}




function delprod($idp)
{   global $sql;
	global $link;
  $sql="DELETE FROM `produse` WHERE `produse`.`idprodus` = $idp"; 
	mysqli_query($link,$sql);
    $eroare = mysqli_error($link);
	 	if ($eroare) echo $eroare;
			else echo"Am sters cu succes produsul cu ID: $idp !";
} ?>

So basically what I want to do is to pass from the confirmadelprod() function to delprod() function once the “Da” button is pressed .
The problem is that whatever product from the table I try to erase it always deletes the item from the bottom of the table .
This is how the table looks like:
Capture

Sorry if the post is written bad . it’s the first one I make .

I don’t see how this script even executes with obvious errors. if else conditionals need to be corrected and variable concatenation needs to be better understood by the author of the script.

if ($eroare) { echo $eroare; }

if ($eroare) { 
  echo $eroare;
} else {
  echo "Am sters cu succes produsul cu ID: " . $idp . "!";
}

https://secure.php.net/manual/ro/control-structures.if.php

The script misuses globals instead of function parameters and the functions are more like procedures instead of functions. I recommend placing the database procedures in a separate php file. Advanced method is a class.

Speaking of databases, mysqli should be replaced with PDO and prepared statements:
https://phpdelusions.net/pdo

I recommend cleaning the script and applying fundamental php and pdo.

1 Like

You need to take a few steps back. Where do you pass an id of the product you want to delete in?

Sponsor our Newsletter | Privacy Policy | Terms of Service