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
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!

 

Link to comment
Share on other sites

Why are you checking for the same indexes in both $_POST and $_GET ?

You can use $_REQUEST to check both but you should really just do one or the other, probably $_POST if you're deleting something so someone can't just pop stuff into the URL to delete stuff

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.