Jump to content

Deleting Items From A Drop Down Menu, Linked To Mysql


Greysoul
 Share

Recommended Posts

this is more than likely an easy question, and i'm brand new to sql/php. I'm just kind of trying as best i can to piece things together. so far i have a drop down list successfully showing a list of all the names in my column. I want to be able to choose a name, and hit delete..and it delete the row from the DB. i'm able to do it fine from a text form..where you type the name in..but i seem a bit confused on the drop down. this is what i have so far..including a button..however the main function of submitting isn't there :/ if i try to do it like the other form i made..then i end up with two $result. i'm not even sure if result is just a variable or an actual set thing..wouldn't work if i changed one to $results. but to get to the point..i just want it to delete the row identified by the name of the clan, selected in the box when i click the button..thats all :) any help is appreciated!Clans : <select name="Clans"><?php$connection=mysql_connect("localhost","test","test") or die("cannot locate the database server");$db=mysql_select_db("test") or die("can't find database");$query="SELECT ClanName FROM Clans";$result=mysql_query($query);while($nt=mysql_fetch_array($result)) { echo "<option value=$nt[ClanName]>$nt[ClanName]</option>"; }echo "<input type='submit' value='Delete' name='clans'></form>";</select>?>

Edited by Greysoul
Link to comment
Share on other sites

yeah like i said, this one pretty much has no function of delete..its just showing the drop down. the /select may be wrong but its not changing anything yet lol. heres the other one that works with typing a name in the blank and hitting the delete button. <form action ="index.php" name="thisform" method="get">Please Enter a Clan Tag to Delete: <INPUT TYPE="text" NAME="name" SIZE="4"><input type="submit" value="Remove" onclick="submit();"> Refresh Page after hitting Delete!<br><?php$var = $_REQUEST['name'];$delete = "delete from Clans where ClanName = \"$var\"";//Connect to SQL$db = mysql_connect("localhost", "test", "test");//Connect to DBmysql_select_db("test", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");$result =mysql_query($delete);// these three lines don't work// if ($result ==true)// { echo "The row has been deleted successfully"; }// else echo "No rows has been deleted";?></form>

Link to comment
Share on other sites

<?php//posted value with GET as form method... $var = $_GET['Clans']; $delete = "delete from Clans where ClanName = $var";$db = mysql_connect("localhost", "test", "test");mysql_select_db("test", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");$result =mysql_query($delete);if ($result) { echo "Row deleted successfully"; }else{echo "Nothing deleted";}?><form id="myform" name="myform" method="get" action=""><select name="Clans" id="Clans"><?php do {  ?><option value="<?php echo $row['ClanName']?>"><?php echo $row['ClanName']?></option><?php} while ($row_test = mysql_fetch_assoc($test));$rows = mysql_num_rows($test);if($rows > 0) {mysql_data_seek($test, 0);$row_test = mysql_fetch_assoc($test);}?></select><input type="submit" value="Delete" /></form>

Edited by student101
Link to comment
Share on other sites

<?php//posted value with GET as form method... $var = $_GET['Clans']; $delete = "delete from Clans where ClanName = $var";$db = mysql_connect("localhost", "test", "test");mysql_select_db("test", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");$result =mysql_query($delete);if ($result) { echo "Row deleted successfully"; }else{echo "Nothing deleted";}?><form id="myform" name="myform" method="get" action=""><select name="Clans" id="Clans"><?php do {  ?><option value="<?php echo $row['ClanName']?>"><?php echo $row['ClanName']?></option><?php} while ($row_test = mysql_fetch_assoc($test));$rows = mysql_num_rows($test);if($rows > 0) {mysql_data_seek($test, 0);$row_test = mysql_fetch_assoc($test);}?></select><input type="submit" value="Delete" /></form>

hm with that it shows me a drop down box thats empty, and a msg saying nothing deleted above it. check your pm i'll send you a link of the site. nevermind it says you can't receive pms Edited by Greysoul
Link to comment
Share on other sites

You need to add your own database stuff, once you do that it should work.
if you mean the connection to my db/pw and database name, i did that. did i miss something else in the rest :x i mean i don't see where theres a SELECT ClanName FROM Clans to list them in the select box Edited by Greysoul
Link to comment
Share on other sites

Assuming the clan name is a string, you need to quote it in the query.Other than that, I would also strongly advise that you sanitize the clan name to protect against SQL injection attacks, and also realize that you aren't doing any user validation, anyone who knows the URL of that page can delete anything they want from that table.

Link to comment
Share on other sites

Your db select is before the delete part, I am not sure where you learnt to code MySQL SELECT in the actual HTML <select></select> parts?w3schools.com? please enlighten me.
man..i'm just asking for help..not your judgement. all i can say is its not working. if you're not in the mood to help, i understand.
Link to comment
Share on other sites

Assuming the clan name is a string, you need to quote it in the query.Other than that, I would also strongly advise that you sanitize the clan name to protect against SQL injection attacks, and also realize that you aren't doing any user validation, anyone who knows the URL of that page can delete anything they want from that table.
this is for learning and testing purposes, if anyone knew the URL..which they don't..i'd just restore the db and or change the URL. it's nothing set in stone..its just practice for me.
Link to comment
Share on other sites

Please post your non-working version.
please understand, the best way i learn is from example, and i've read the tutorials on w3..the more advanced stuff isn't really on there or i'm looking in the wrong place. i am VERY new to php and sql in all aspects. i think what i've done so far is a lot for a second dayer. the non working version of the one you gave me is as you posted it above. i didn't know if there was anything other than the db connection stuff to change, so i'm a little lost there.<?php//posted value with GET as form method...$var = $_GET['Clans'];$delete = "delete from Clans where ClanName = $var";$db = mysql_connect("localhost", "test", "test");mysql_select_db("test", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");$result =mysql_query($delete);if ($result) {echo "Row deleted successfully";}else{echo "Nothing deleted";}?><form id="myform" name="myform" method="get" action=""><select name="Clans" id="Clans"><?php do { ?><option value="<?php echo $row['ClanName']?>"><?php echo $row['ClanName']?></option><?php} while ($row_test = mysql_fetch_assoc($test));$rows = mysql_num_rows($test);if($rows > 0) {mysql_data_seek($test, 0);$row_test = mysql_fetch_assoc($test);}?></select><input type="submit" value="Delete" /></form>this was mine..where the delete button isn't really set to delete anything.Clans : <select name="Clans"><?php$connection=mysql_connect("localhost","test","test") or die("cannot locate the database server");$db=mysql_select_db("test") or die("can't find database");$query="SELECT ClanName FROM Clans";$result=mysql_query($query);while($nt=mysql_fetch_array($result)) { echo "<option value=$nt[ClanName]>$nt[ClanName]</option>"; }echo "<input type='submit' value='Delete' name='clans'></form>";?></select>in mine, i have all the clans in the drop down but of course delete doesn't work. in yours i have nothing in the drop down..and the delete probably does work. Edited by Greysoul
Link to comment
Share on other sites

select.php

<?php $connection=mysql_connect("localhost","greysoul_gs","test") or die("cannot locate the database server");$db=mysql_select_db("greysoul_leaguedb") or die("can't find database");$query="SELECT ClanName FROM Clans";$result=mysql_query($query);?><form id="myform" name="myform" method="get" action="delete.php"><select name="Clans"><?phpwhile($nt=mysql_fetch_array($result)){echo "<option value=$nt[ClanName]>$nt[ClanName]</option>";}?></select><input type='submit' value='Delete' name='clans'></form>

delete.php

<?php $connection=mysql_connect("localhost","greysoul_gs","test") or die("cannot locate the database server");$db=mysql_select_db("greysoul_leaguedb") or die("can't find database");$query="SELECT ClanName FROM Clans";$result=mysql_query($query);// you need to sanitize the get var...$var = $_GET['Clans'];$delete = "delete from Clans where ClanName = $var";$rsdeleted =mysql_query($delete);if ($rsdeleted) { echo "Row deleted successfully"; }else{echo "Nothing deleted";}?>

Try this, post results.

Edited by student101
Link to comment
Share on other sites

You need a basic HTML tutorial.What was passed in the URL; ie http://www.sitename/pagename.php?select=1
no i just didn't understand you. text really isn't the same as having a chat vocally.clandelete.php?Clans=cb&clans=Deletecb is the clan name. db field names for that is ClanName. db name is greysoul_leaguedb. i have a feeling thats not what you're asking either lol Edited by Greysoul
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...