Amanda-Lee Posted May 9, 2012 Share Posted May 9, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/262299-deletephp-not-working/ Share on other sites More sharing options...
Amanda-Lee Posted May 9, 2012 Author Share Posted May 9, 2012 when you click Yes/No it display the delete page empty. Quote Link to comment https://forums.phpfreaks.com/topic/262299-deletephp-not-working/#findComment-1344197 Share on other sites More sharing options...
Amanda-Lee Posted May 9, 2012 Author Share Posted May 9, 2012 *blush* the else part for the No option i left out because i needed to get the Yes part first! Quote Link to comment https://forums.phpfreaks.com/topic/262299-deletephp-not-working/#findComment-1344198 Share on other sites More sharing options...
darkfreaks Posted May 9, 2012 Share Posted May 9, 2012 put this in your code and tell us exactly what errors you get. ini_set('error_reporting', E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/262299-deletephp-not-working/#findComment-1344261 Share on other sites More sharing options...
Amanda-Lee Posted May 9, 2012 Author Share Posted May 9, 2012 Darkfreaks Can I put it in anywhere between the <?php?> tag? And thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/262299-deletephp-not-working/#findComment-1344265 Share on other sites More sharing options...
Amanda-Lee Posted May 9, 2012 Author Share Posted May 9, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/262299-deletephp-not-working/#findComment-1344266 Share on other sites More sharing options...
leitning Posted May 9, 2012 Share Posted May 9, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262299-deletephp-not-working/#findComment-1344277 Share on other sites More sharing options...
Amanda-Lee Posted May 9, 2012 Author Share Posted May 9, 2012 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/262299-deletephp-not-working/#findComment-1344289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.