Jump to content

Select box populated by db, need to delete selected option from DB


phpnewbie25

Recommended Posts

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>

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>

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 ' &nbsp'; ?><input name="submit" value="Delete User" type="submit" />
</form>


</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.