phpnewbie25 Posted February 20, 2009 Share Posted February 20, 2009 Hi, I have a select box that has its values populated from a db. I'm trying to develop a page so that beside this select box there is a delete button that will delete whichever row is selected from the select box from the db. Very new to php so excuse the state of my current code! Here's what I have so far: Any help greatly appreciated. <?php session_start(); include "dbConfig.php"; $getUser_sql = 'SELECT * FROM users'; $getUser = mysql_query($getUser_sql); ?> <select name="select" style="width:250" style="color:blue;text-align:left" size='21'> <option value="0" style="text-align:center">-Select User from List-</option> <?php while ( $row = mysql_fetch_array($getUser) ) {?> <option value="<?php echo $row['first_name']; ?>"> <?php echo $row['id']; ?> <?php echo $row['first_name']; ?> <?php echo $row['last_name']; ?> </option> <?php } ?> <html> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <?php echo $selectMenu; ?> <input type="submit" /> </form> </html> Link to comment https://forums.phpfreaks.com/topic/146102-select-box-populated-by-db-need-to-delete-selected-option-from-db/ Share on other sites More sharing options...
farkewie Posted February 20, 2009 Share Posted February 20, 2009 Hi, I had a play havent tested it so may need some tweaking. <?php session_start(); include "dbConfig.php"; //make sure the form was submitted if (isset ($_POST['submit'])){ //delete from the table where the id matched the submitted id $sql = "DELETE FROM users WHERE id='".$_GET['name']."' LIMIT 1"; $result = mysql_query($sql) or die(mysql_error()); //make sure we deleetd 1 row if (mysql_affected_rows()> 0){ //User Deleted } else { //Oops im wrong. } } $getUser_sql = 'SELECT * FROM users'; $getUser = mysql_query($getUser_sql); ?> <html> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <select name="name" style=" color:blue;text-align:left" size='21'> <option value="" style="text-align:center">-Select User from List-</option> <?php while ($row = mysql_fetch_array($getUser)) { ?> <option value="<?php echo $row['id']; ?>"> <?php echo $row['first_name']; ?> <?php echo $row['last_name']; ?></option> <?php } mysql_free_result($getUser)?> <input name="submit" value="Submit" type="submit" /> </form> </html> Link to comment https://forums.phpfreaks.com/topic/146102-select-box-populated-by-db-need-to-delete-selected-option-from-db/#findComment-767010 Share on other sites More sharing options...
phpnewbie25 Posted February 20, 2009 Author Share Posted February 20, 2009 many thanks for the reply, in work at the moment so will test it and tweak it when I get home. cheers Link to comment https://forums.phpfreaks.com/topic/146102-select-box-populated-by-db-need-to-delete-selected-option-from-db/#findComment-767020 Share on other sites More sharing options...
phpnewbie25 Posted February 20, 2009 Author Share Posted February 20, 2009 Hi, Just testing this out now, no values are being passed to GET/POST functions, have been toying around with it but getting nowhere, any ideas? Thanks <?php include "dbConfig.php"; if (isset ($_POST['submit'])) { echo 'first loop reached'; $test = $_POST['first_name']; echo 'Post test: '.$test; $firstname = $_GET['first_name']; echo 'Get test: '.$firstname; $sql = "DELETE * FROM users WHERE id='".$_GET['id']."'"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_affected_rows()> 0) { echo 'User Deleted'; } else { echo 'Not Deleted'; } } $getUser_sql = 'SELECT * FROM users'; $getUser = mysql_query($getUser_sql); ?> <html> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <select name='name' style=" color:blue;text-align:left" size='21'> <option value="" style="text-align:center">-Select User from List-</option> <?php while ($row = mysql_fetch_array($getUser)) { ?> <option value =" <?php echo $row['id']; ?>"> <?php echo $row['first_name']; ?> <?php echo $row['last_name']; ?></option> <?php } mysql_free_result($getUser)?> <? echo '  '; ?><input name="submit" value="Delete User" type="submit" /> </form> </html> Link to comment https://forums.phpfreaks.com/topic/146102-select-box-populated-by-db-need-to-delete-selected-option-from-db/#findComment-767420 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.