Jump to content

Delete.php not working


Amanda-Lee

Recommended Posts

Hi

I am struggling with my last page for a project it is a delete page that retrieves data from a list, asks the user if he wants to delete that record if 'yes' delete it otherwise redirect to another page. My page looks like it should, but the function is not working. my code is:


<?php

  //check to see if user is logged on
  session_start();	
  if (!(isset($_SESSION['login']) && $_SESSION['login'] != "")) {	
  header ("Location:login.php");	
  }
  
  include('connect.php'); //connection details to database in a connect.php page 

  //if form was submitted
  if (isset($_REQUEST['yes'])){
  
  $product_id = $_GET['product_id'];	
  $productname = $_POST['pname'];	
  $range = $_POST['prange'];	
  $price1 = $_POST['pprice'];	
  $price = (int)$price1;
  				
  $query = "DELETE FROM products WHERE product_id = '$product_id'";		
  $result = mysql_query($query);	
 print "<br> $productname from range $range with a price of R$price has been deleted!</br>";
  } 
  }
  
  if (isset($_GET['product_id'])){			  
    $product_id = $_GET['product_id'];
    $query = "SELECT * FROM products WHERE product_id = '$product_id'";
    $result = mysql_query($query);
  
  echo "<form method = \"post\" action = \"delete.php\">";
    
  while($row = mysql_fetch_array($result)){
  echo "<table>";	
  echo "<tr>";	
  echo "<td><input name = 'product_id' type = 'hidden' value ='{$row['product_id']}'></td>";	
  echo "</tr>";	
  echo "<tr>";	
  echo "<td>Do you really want to delete {$row['product_name']} from range {$row['product_range']} with a price of R{$row['product_price']} from the database?</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td<input type = 'submit' name = 'yes' value = 'Yes'><input type = 'submit' name = 'no' value = 'NO'></td>";	
  echo "</tr>";
  echo "</table>";
  }
  echo "</form>";
  }   
  ?>

 

Any help please?

Link to comment
https://forums.phpfreaks.com/topic/262299-deletephp-not-working/
Share on other sites

I worked on it  my new code is

 

<?php

  //check to see if user is logged on
  session_start();	
  if (!(isset($_SESSION['login']) && $_SESSION['login'] != "")) {	
  header ("Location:login.php");	
  }
  
  include('connect.php'); //connection details to database in a connect.php page

  //if form was submitted
  if (isset($_POST['yes'])){
    if (isset($_GET['product_id'])||isset($_POST['product_id'])||isset($_GET['pname'])|| isset($_POST['pname']) ||isset($_GET['prange'])||isset($_POST['prange'])||isset($_GET['pprice'])||isset($_POST['pprice'])){
      if (isset($_GET['product_id'])|| isset($_GET['pname'])||isset($_GET['prange'])||isset($_GET['pprice'])){
      $product_id = $_GET['product_id'];
      $productname = $_GET['pname'];
      $range = $_GET['prange'];	
  		$price = $_GET['pprice'];
       }
      if (isset($_POST['product_id'])|| isset($_POST['pname'])||isset($_POST['prange'])||isset($_POST['pprice'])){
      $product_id = $_POST['product_id'];
      $productname = $_POST['pname'];
      $range = $_POST['prange'];	
  		$price = $_POST['pprice'];
       }
      }
      
  $query = "DELETE FROM products WHERE product_id = '$product_id'";		
  $result = mysql_query($query);	
   print "<br> $productname from range $range with a price of R$price has been deleted!</br>";		
  } 
  
  if (isset($_POST['no'])){
      header ("Location:list.php");
      }
      
  if (isset($_GET['product_id'])){			  
    $product_id = $_GET['product_id'];
    $query = "SELECT * FROM products WHERE product_id = '$product_id'";
    $result = mysql_query($query);
  
  echo "<form method = \"post\" action = \"delete.php\">";
    
  while($row = mysql_fetch_array($result)){
  echo "<table>";	
  echo "<tr>";	
  echo "<td><input name = 'product_id' type = 'hidden' value ='{$row['product_id']}'></td>";	
  echo "</tr>";	
  echo "<tr>";	
  echo "<td>Do you really want to delete {$row['product_name']} from range {$row['product_range']} with a price of R{$row['product_price']} from the database?</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td><input type = 'submit' name = 'yes' value = 'Yes'><input type = 'submit' name = 'no' value = 'NO'></td>";	
  echo "</tr>";
  echo "</table>";
  }
  echo "</form>";
  } 
   ini_set('error_reporting', E_ALL); 
  ?> 

 

And the result I am getting is that the No option works great, the yes option deletes right but shows this on the page:

 

Notice: Undefined index: pname in C:\Program Files (x86)\EasyPHP-5.3.3\www\delete.php on line 39

 

Notice: Undefined index: prange in C:\Program Files (x86)\EasyPHP-5.3.3\www\delete.php on line 40

 

Notice: Undefined index: pprice in C:\Program Files (x86)\EasyPHP-5.3.3\www\delete.php on line 41

 

from range with a price of R has been deleted!

 

Thanks guys I solved it.

 

echo "<td><input name = 'product_id' type = 'hidden' value ='{$row['product_id']}'></td>";

 

changed to:

echo "<td><input name = 'product_id' type = 'hidden' value ='{$row['product_id']}'><input name = 'pname' type = 'hidden' value ='{$row['product_name']}'><input name = 'prange' type = 'hidden' value ='{$row['product_range']}'><input name = 'pprice' type = 'hidden' value ='{$row['product_price']}'></td>";

:D

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.