Jools1988 Posted March 9, 2009 Share Posted March 9, 2009 Hi there, I've been having trouble the past few days figuring out what was wrong with my code. What I'm trying to do is delete a product from an online store and from the mysql database connected to it. I have not implemented a cart function yet so there is no code for that function. <?php session_start(); require_once('./connections/connect.php'); require_once("template.html"); if (isset($_POST['submit']) && isset($_POST['number'])) { $Description = $_POST['description']; $Type = $_POST['type']; $Units = $_POST['units']; $Storage = $_POST['storage']; $Price = $_POST['price']; $query = "DELETE FROM product WHERE PID = '$_POST[number]'"; $result = mysql_query($query); if(mysql_affected_rows() == 1) { echo "The product details have been deleted from the system"; } } echo "<p></p>"; $query = "SELECT PID, P_Desc, P_Type, P_Units, P_Storage, P_Unit_Price FROM product WHERE PID = '$_POST[id]'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { $id = $row['PID']; $Description = $row['P_Desc']; $Type = $row['P_Type']; $Units = $row['P_Units']; $Storage = $row['P_Storage']; $Price = $row['P_Unit_Price']; } ?> <form action="delete_product.php" method="post" class ="contact"> <fieldset> <legend>Delete Product Details</legend> <p> <label for="description" class ="fixed_required">Description: </label> <input type="text" name="description" id="description" value ="<?php echo $Description; ?>"/> </p> <p> <label for="type" class ="fixed_required">Type: </label> <input type="text" name="type" id="type" value ="<?php echo $Type; ?>"/> </p> <p> <label for="units" class ="fixed_required">Units: </label> <input type="text" name="units" id="units" value ="<?php echo $Units; ?>"/> </p> <p> <label for="storage" class ="fixed_required">Storage: </label> <input type="text" name="storage" id="storage" value ="<?php echo $Storage; ?>"/> </p> <p> <label for="price" class ="fixed_required">Price: </label> <input type="text" name="price" id="price" value ="<?php echo $Price; ?>"/> </p> <p class="submitbuttonarea" id="submit"> <input type="submit" name ="submit" id="submit" value="Submit" /> <input type="hidden" name ="number" id="number" value ="<?php echo $id; ?>" /> </p> </fieldset> </form> Any input is apreciated. Thanks! Edit: Left out important code oops! <?php session_start(); require_once('./connections/connect.php'); require_once("template.html"); $query = "SELECT * FROM product"; $result = mysql_query($query); echo "<p></p>"; echo "<table class ='events'>"; echo "<th align='center'>ID Number</th><th align='center'>Description</th><th align='center'>Type</th><th align='center'>Units</th><th align='center'>Storage</th><th align='center'>Unit Price</th><th align='center'></th><th align='center'></th>"; while ($row = mysql_fetch_array($result)) { $id = $row['PID']; $Description = $row['P_Desc']; $Type = $row['P_Type']; $Units = $row['P_Units']; $Storage = $row['P_Storage']; $Price = $row['P_Unit_Price']; $_SESSION['id']=$id; echo "<tr><td align='center'>$id</td><td align='center'>$Description</td><td align='center'>$Type</td><td align='center'>$Units</td><td align='center'>$Storage</td><td align='center'>$Price</td>"; echo '<td> <form method="post" action="delete_product.php" name="delete_product"> <input type ="submit" name="submit" id="submit" value="Delete Product" /> <input type="hidden" name="id" value="'.$id.'" /> </form> </td> <td> <form method="post" action="edit_product.php" name="edit_product"> <input type ="submit" name="submit" id="submit" value="Edit Product Details" /> <input type="hidden" name="id" value="'.$id.'" /> </form> </td> </tr>'; } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/ Share on other sites More sharing options...
ngreenwood6 Posted March 9, 2009 Share Posted March 9, 2009 Well I noticed one problem: $query = "DELETE FROM product WHERE PID = '$_POST[number]'"; This is not actually getting the number because you are missing the quotes. I would change this to someting like this: $number = $_POST['number']; $query = "DELETE FROM product WHERE PID = '$number'"; Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780386 Share on other sites More sharing options...
Jools1988 Posted March 9, 2009 Author Share Posted March 9, 2009 Well I noticed one problem: $query = "DELETE FROM product WHERE PID = '$_POST[number]'"; This is not actually getting the number because you are missing the quotes. I would change this to someting like this: $number = $_POST['number']; $query = "DELETE FROM product WHERE PID = '$number'"; Sorry for the late reply, but thank you for posting. I will test it out as soon as i can and let you know. Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780764 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 trim it then mysql_real_escape_string() it. <?php $query = "DELETE FROM product WHERE PID = 'trim(mysql_real_escape_string({$_POST['number']}')))"; ?> Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780808 Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 Thats a good programming practice red but if it isnt working its not very helpful lol. Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780810 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 Your query's have the same name change them. look at all your arrays man come on $redarrow['look_array'] <<< correct not correct $redarrow[look_array] did this code as posted work earlier $number = $_POST['number']; $query = "DELETE FROM product WHERE PID = '$number'"; Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780812 Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 I think you posted to me but I am not the original poster and I am the one that posted the last code you posted lol. Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780818 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 that why i asked as i no the code works (( your driving me crazy lol Most programmers say it bad programming practice to use any functions within a sql query, but as time goes on most php programming books now show it as good programming practice (crazy man). It all about preference i guess. Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780828 Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 lol i think we confused each other. I was just saying that it was good practice to use the trim() and the mysql_real_escape_string() functions not actually how you used them. I think either way you use it is personal preference. Personally I use them outside of my queries for readablity. Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780844 Share on other sites More sharing options...
Jools1988 Posted March 10, 2009 Author Share Posted March 10, 2009 Thanks for the replies, unfourtunatly green your code was giving us te same problem. We decided to completly revamp the delete by using a checkbox instead. After this we got it working. <?php $dbconn = mysql_connect("127.0.0.1","mannixtn","R00022424"); mysql_select_db("britney"); include("template.html"); if($_POST['delete']) // from button name="delete" { $checkbox = $_POST['checkbox']; //from name="checkbox[]" $countCheck = count($_POST['checkbox']); for($i=0;$i<$countCheck;$i++) { $del_id = $checkbox[$i]; $sql = "DELETE from product where PID = $del_id"; $result = mysql_query($sql, $dbconn); } if($result) { echo "Product has being deleted from the system"; } else { echo "Error: ".mysql_error(); } } ?> <?php //mysql connection $dbconn = mysql_connect("127.0.0.1","mannixtn","R00022424"); mysql_select_db("britney"); $sqlquery = "SELECT * FROM product"; // query on table $sqlresult = mysql_query($sqlquery, $dbconn); $count = mysql_num_rows($sqlresult); // count query result include("template.html"); ?> <table > <tr><td> <form method="post" action="delete_multiple2.php"> <table width="700" border="1" cellpadding="5" cellspacing="5" ><tr> <tr><td align="center">#</td> <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Type</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Units</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Storage</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Price</strong></td></tr> <?php while($row = mysql_fetch_array($sqlresult)){ ?> <tr><td align="center"> <input type="checkbox" name="checkbox[]" id="checkbox[]" value="<?php echo $row['PID']?>" /> </td><td><?php echo $row['PID']; ?></td> <td bgcolor="#FFFFFF"><?php echo $row['P_Desc']; ?></td> <td bgcolor="#FFFFFF"><?php echo $row['P_Type']; ?></td> <td bgcolor="#FFFFFF"><?php echo $row['P_Units']; ?></td> <td bgcolor="#FFFFFF"><?php echo $row['P_Storage']; ?></td> <td bgcolor="#FFFFFF"><?php echo $row['P_Unit_Price']; ?></td></tr> <?php } ?> <tr><td colspan="7" align="center"> <input name="delete" type="submit" id="delete" value="Delete"></td></tr> </table> </form></td></tr></table> Thanks for all your replies, and sorry for wasting your time! Link to comment https://forums.phpfreaks.com/topic/148598-delete-product-from-website-and-database/#findComment-780982 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.