subhomoy Posted January 6, 2013 Share Posted January 6, 2013 hello everyone actually i'm trying to delete data from the database that is been queried in the drop down menu.... The code is shown below... <?php require_once 'myconn.php'; $sql = "SELECT course_id, course_name FROM course"; $result = mysql_query($sql); echo "<form method='post' action=''>"; echo "<label>Delete Course</label>"; echo "<select>"; while($row = mysql_fetch_array($result)){ echo "<option value='{$row['course_id']}'>{$row['course_name']}</option>"; } echo "</select>"; echo "<input type='submit' name='submitbtn' value='Delete'>"; if(isset($_REQUEST['submitbtn'])){ $sql1= "DELETE course_name FROM course WHERE course_id='{$row['course_id']}'"; $reslut1= mysql_query($sql1); if(mysql_affected_rows()>0){ header("Location:".$_SERVER['php_SELF']); } } ?> When i press delete button the page get refresh but nothing gets deleted from the list... Can any one help me with it... Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/ Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2013 Share Posted January 6, 2013 Your <select> field needs a name= attribute, which you would then use to get the value of the <option> from the $_POST array, validate it, and use that value in the delete query. Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403563 Share on other sites More sharing options...
haku Posted January 6, 2013 Share Posted January 6, 2013 To elaborate on that, your problem lies in this code: if(isset($_REQUEST['submitbtn'])){ $sql1= "DELETE course_name FROM course WHERE course_id='{$row['course_id']}'"; You are trying to delete $row['course_id'], but that is not the value that has been submitted. The value that has been submitted will be part of the $_POST array. Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403564 Share on other sites More sharing options...
subhomoy Posted January 6, 2013 Author Share Posted January 6, 2013 To elaborate on that, your problem lies in this code: if(isset($_REQUEST['submitbtn'])){ $sql1= "DELETE course_name FROM course WHERE course_id='{$row['course_id']}'"; You are trying to delete $row['course_id'], but that is not the value that has been submitted. The value that has been submitted will be part of the $_POST array. no actually i'm trying to delete the 'course_name' not the 'course_id'... If possible can you plz show me... I'm not getting it... Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403572 Share on other sites More sharing options...
haku Posted January 6, 2013 Share Posted January 6, 2013 Sorry, I should have said 'your code is trying to $row['course_id']. Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403574 Share on other sites More sharing options...
Jessica Posted January 6, 2013 Share Posted January 6, 2013 You cannot DELETE column_name. You DELETE a whole row. It's DELETE FROM table ... Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403614 Share on other sites More sharing options...
subhomoy Posted January 6, 2013 Author Share Posted January 6, 2013 You cannot DELETE column_name. You DELETE a whole row. It's DELETE FROM table ... I did what you told but still its not working.... echo "<input type='submit' name='submitbtn' value='Delete'>"; if(isset($_REQUEST['submitbtn'])){ $sql1= "DELETE FROM course WHERE course_id='{$row['course_id']}'"; $reslut1= mysql_query($sql1); if(mysql_affected_rows()>0){ header("Location:".$_SERVER['php_SELF']); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403625 Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2013 Share Posted January 6, 2013 Have you taken care of this? It will never work until you do. Your <select> field needs a name= attribute, which you would then use to get the value of the <option> from the $_POST array, validate it, and use that value in the delete query. Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403627 Share on other sites More sharing options...
subhomoy Posted January 6, 2013 Author Share Posted January 6, 2013 Have you taken care of this? It will never work until you do. Can you plz show me the code... Actually i'm not getting it.... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403631 Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2013 Share Posted January 6, 2013 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403635 Share on other sites More sharing options...
subhomoy Posted January 7, 2013 Author Share Posted January 7, 2013 <?php require_once 'myconn.php'; $sql = "SELECT course_id, course_name FROM course"; $result = mysql_query($sql); echo "<form method='post' action=''>"; echo "<label>Delete Course</label>"; echo "<select>"; while($row = mysql_fetch_array($result)){ echo "<option value='{$row['course_id']}'>{$row['course_name']}</option>"; } echo "</select>"; echo "<input type='submit' name='submitbtn' value='Delete'>"; if(isset($_REQUEST['submitbtn'])){ $sql1= "DELETE FROM course WHERE course_id='{$row['course_id']}'"; $reslut1= mysql_query($sql1); if(mysql_affected_rows()>0){ header("Location:".$_SERVER['php_SELF']); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403879 Share on other sites More sharing options...
Christian F. Posted January 7, 2013 Share Posted January 7, 2013 You have not given a name attribute to the select element, so you will not have a named index for it in the POST array. Though, not that it matters since you're not using it anyway. What you need to do is to realize that PHP and HTML are two completely separate processes: PHP may generate the HTML, but it has no knowledge of what that HTML code means. Same with HTML, it does not know that PHP exists and as such cannot alter anything in the PHP code does. What you can do, however, is to use regular HTTP requests to send data to the server. Which the PHP code picks up, and then use said data to determine what it should do. The best way to accomplish this mental model is by "physically" separating the HTML and PHP code, so that all of your PHP code is located at the very top of the file. Then use variables to hold the generated HTML content, and then echo it out wherever you need it. This will make your script look something like this: <?php // Retrieve list of courses. if (is_submitted ()) { // Retrieve and validate data if (!$valid) { // Generate error message. // Repopulate the form with the selected value // Show the form w/error message and abort the deletion process. } // Delete the selected row from the DB // Redirect user to confirmation screen. die (); } // Not submitted, generate clean form. ?> <doctype html> <html> <head> <title>Your page</title> </head> <body> <!-- Your HTML code here. --> <?php echo $form; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1403894 Share on other sites More sharing options...
subhomoy Posted January 8, 2013 Author Share Posted January 8, 2013 Thaks guysss for the problem.... Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1404178 Share on other sites More sharing options...
Christian F. Posted January 8, 2013 Share Posted January 8, 2013 You're welcome, and I'm glad to see that you got it sorted out. Quote Link to comment https://forums.phpfreaks.com/topic/272751-delete-from-database/#findComment-1404185 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.