Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 117,572 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,978 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



How to delete a row from drop down list box

 
Reply to this topicStart new topic

How to delete a row from drop down list box

msvp4u
post 21 Jul, 2008 - 12:57 PM
Post #1


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 37


My Contributions


I n the following code i have listed the users through a query.Now i want to delete those users by selecting them through a dropdown list box and a delete button. my code is as follows
CODE

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$userpassword=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="adduser"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$userpassword")or die("Cannot connect to the Data Base");
mysql_select_db("$db_name")or die("Cannot select DB");


$sql="select concat(left(firstname,1),lastname) as username from adduser order by firstname;";
$result=mysql_query($sql)or die(mysql_error());

?>
<select size ="10" name="seluser" style="width:150" class=NormalFont>

            <option value="0">-Select User from List-</option>            
            <?php
            while ($row = mysql_fetch_array($result)) {
                //$count=mysql_num_rows($result);
               $username = $row["username"];
            ?>                  

                        <option value><?php echo "$username";?>
            <?php
            }
            ?>
            </option>

                  

                  </select>
<input type="submit" class="NormalButton"  value=" Delete " onClick="doSubmit(deluser)" id=button1 name=button1>

                 <input type="button" class="NormalButton" value=" Cancel " onClick="java script:location.href = './users.php'" id=button2 name=button2>

<script language="javascript">

  

  function doSubmit(frm)

  {

     if (deluser.seluser.selectedIndex < 0 )

     {

        alert("Select a User to delete");

     }

     else

     {

        frm.flag.value = 1;

        frm.action = "./userlist.php";

        //frm.submit();

        callVBconfirm()

     }    

  }

function nextPage()

{

     deluser.flag.value = 1;

     deluser.action = "./userlist.php";

     deluser.submit();

}  

</script>



<script language="VBScript">

function callVBconfirm()

ans = MsgBox("Are you sure want to delete this user account?",vbYesNo)

if (ans=6) then

nextPage()

end if

end function    

</script>


the cancel button works absolutely fine and when i tried to write a query to delete that user i could not delete. kindly help me write a query to delete the user.

Details about the table 'adduser'
it has three fields namely,
1.uid
2.firstname
3.lastname
User is offlineProfile CardPM

Go to the top of the page


MitkOK
post 21 Jul, 2008 - 02:24 PM
Post #2


D.I.C Regular

Group Icon
Joined: 9 Aug, 2007
Posts: 289



Thanked 9 times

Dream Kudos: 250
My Contributions


Your code is a mess. I don't really want look at it, but...

1. Your select box should look like this :
html
<select size ="10" name="deluser" style="width:150" class=NormalFont>
<option value="0">-Select User from List-</option>
<option value ="1">JDoe </option>
<option value ="3">WSmith </option>
<option value ="2">MJohnson </option>
</select>

... where the values of the option is uid. You get get this adding uid to the select statement :
sql
$sql="select uid, concat(left(firstname,1),lastname) as username from adduser order by firstname;";


After this you have to put the uid in the form:
php
<option value =" <?php echo $row['uid']; ?>"><?php echo "$username";?></option>

After these steps you'll get post variable like this $_POST['deluser'] which will point to the user id you want to delete. The delete query is simple:
sql
$query = "DELETE FROM adduser WHERE uid='$_POST[deluser]'";


I strongly recomment you reading some basic HTML ( forms ) and PHP/MySQL tutorials.

This post has been edited by MitkOK: 21 Jul, 2008 - 02:25 PM
User is offlineProfile CardPM

Go to the top of the page

msvp4u
post 22 Jul, 2008 - 11:04 AM
Post #3


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 37


My Contributions


thanks for the help it worked fine. i made the necessary changes you told.

i am still a beginner, i am still learning php, html forms and MySQL and those were the codes which i got from the net.

Any way thanks for your suggestion and hope will be better with the code next time i post a new one.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 08:41PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month