webznz Posted February 7, 2010 Share Posted February 7, 2010 Hi there im currently writing my first back end to a website where I enable myself to add new items to my database and delete them. I followed a Mysql tutorial to get myself started and have done some php at uni but im still pretty new so am finding it hard to solve the problem i am having. Basicly everything works correctly I.E. connect to the database and all items that are currently in the database are presented but then when i click the delete this product link nothing happens. here is my code thank you for taking you time to help me. <html> <head> <title> MTS Controll panle </title> <link rel="stylesheet" type="text/css" href="http://127.0.0.1/mts/css/backend.css" /> </head> <body> <div id="wrap"> <div id="header"></div> <div id="nav"></div> <div id="sidebar"> <!-- start of controll links --> <a href="../backend.html">Insert new Products</a></br> <a href="Delete.php">Delete Products</a> <!-- controll links end here --> </div> <div id="main"> <!-- controll panel start here --> <?php // If the user wants to add a product if (isset($addproduct)): ?> <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST> <P>Type your Product here:<BR> <TEXTAREA NAME="producttext" ROWS=10 COLS=40 WRAP> </TEXTAREA><BR> <INPUT TYPE=SUBMIT NAME="submitproduct" VALUE="SUBMIT"> </FORM> <?php else: // Connect to the database server $dbcnx = @mysql_connect( "localhost", "root", "secretpass"); if (!$dbcnx) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit(); } // Select the mtsproducts database if (! @mysql_select_db("mtsproducts") ) { echo( "<P>Unable to locate the products " . "database at this time.</P>" ); exit(); } // If a product has been submitted, // add it to the database. if ("SUBMIT" == $submitproduct) { $sql = "INSERT INTO products SET " . "productText='$producttext', " ; if (mysql_query($sql)) { echo("<P>Your product has been added.</P>"); } else { echo("<P>Error adding submitted product: " . mysql_error() . "</P>"); } } // If a product has been deleted, // remove it from the database. if (isset($deleteproduct)) { $sql = "DELETE FROM products " . "WHERE partID=$deleteproduct"; if (mysql_query($sql)) { echo("<P>The product has been deleted.</P>"); } else { echo("<P>Error deleting product: " . mysql_error() . "</P>"); } } echo("<P> Here are all the products " . "in our database: </P>"); // Request the ID and text of all the products $result = mysql_query( "SELECT partID, Name FROM products"); if (!$result) { echo("<P>Error performing query: " . mysql_error() . "</P>"); exit(); } // Display the text of each product in a paragraph // with a "Delete this product" link next to each. while ( $row = mysql_fetch_array($result) ) { $ID = $row["partID"]; $text = $row["Name"]; echo("<P>$text " . "<A HREF='$PHP_SELF?deleteproduct=$ID'>" . "Delete this product</A></P>"); } // When clicked, this link will load this page // with the product submission form displayed. echo("<P><A HREF='$PHP_SELF?addproduct=1'>" . "Add a product!</A></P>"); endif; ?> <!--controllpanel links start here --> </div> <div align="center"> <div id="footer">Controll Panel Developed by Casey Smith</div> </div> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/ Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 Your delete link points to a file delete.php is the delete functionality in that file? Also, the delete query needs to be passed the id of a product to delete. Your posted code does not make it clear how it would get that. Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/#findComment-1008287 Share on other sites More sharing options...
webznz Posted February 7, 2010 Author Share Posted February 7, 2010 Sorry about that initially i was going to have a septate file DELETE.php where the delete functionality would be, but after realizing i could use a simple if else statement and keep all the code on one page i decided to do that, however have left the link there to the old file, sorry about that. by my mind i thought if (isset($deleteproduct)) { $sql = "DELETE FROM products " . "WHERE partID=$deleteproduct"; is where the id is given its value to delete? Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/#findComment-1008290 Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 Ok that is fine, so the link now points to the same page, but now you have the variable $deleteproduct, but you do not appear to be setting it anywhere On your product list, you would need a delete link alongside each product, which would go back your page with a ?delete=the id here that you would set and then that id would be used in to set your $deleteproduct by $deleteproduct = $_GET['delete'] Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/#findComment-1008292 Share on other sites More sharing options...
webznz Posted February 7, 2010 Author Share Posted February 7, 2010 Took a while for me to understand what you mean but I think i get it now. there is already a link next to each database entry heres the code for that. while ( $row = mysql_fetch_array($result) ) { $ID = $row["partID"]; $text = $row["Name"]; echo("<P>$text " . "<A HREF='$PHP_SELF?deleteproduct=$ID'>" . "Delete this product</A></P>"); so deleteproduct gets the $ID value which is set just above it with the row in my databse 'PartID' as you can see the link then reloads the page if pressed. However the page loads when a link is clicked but nothing is deleted i assume because the $ID value never makes it to if (isset($deleteproduct)) { $sql = "DELETE FROM products " . "WHERE partID=$deleteproduct"; shouldnt $deleteproduct already have $ID value by this point? Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/#findComment-1008302 Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 No it wont have it will be $_GET['deleteproduct'] Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/#findComment-1008320 Share on other sites More sharing options...
webznz Posted February 7, 2010 Author Share Posted February 7, 2010 could you show me where to put $_GET['deleteproduct'] in my code in order to get this to work as my command over the php syantax is a little rusty. Thanks for all the help you have given me its greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/#findComment-1008331 Share on other sites More sharing options...
webznz Posted February 7, 2010 Author Share Posted February 7, 2010 AHHHHA!!! got it! lol thanks for your help I just added <?php $deleteproduct=$_GET['deleteproduct'] ?> above the first php statement and it works perfectly! thank you very much i should have been able to solve this myself lol need to get some more practice in b4 uni starts up again this semester! thanks again! regards Casey Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/#findComment-1008332 Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 ok 2 ways either if (isset($_GET['deleteproduct')) { $deleteproduct = $_GET['deleteproduct']; $sql = "delete from products where partID=$deleteproduct"; or if (isset($_GET['deleteproduct')) { $sql = 'delete from products where partID='.$_GET['deleteproduct']; both of those queries are making the assumption that the ID used is a pure number. They will not work if it is a string Link to comment https://forums.phpfreaks.com/topic/191231-delete-and-insert-not-working/#findComment-1008333 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.