Jump to content

Deleting Items From A Drop Down Menu, Linked To Mysql


Greysoul

Recommended Posts

Tested...Select.php

<?php// Make a MySQL Connectionmysql_connect("localhost", "username", "password") or die(mysql_error());mysql_select_db("greysoul_leaguedb") or die(mysql_error());// Get all the data from the table$result = mysql_query("SELECT * FROM ClanName") or die(mysql_error());  echo "<form action='delete.php' method='get' name='myform' id='myform'>";echo "<select name='select' id='select'>";// keeps getting the next row until there are no more to getwhile($row = mysql_fetch_array( $result )) {// Print out the contents of each rowecho "<option value=".$row['ClanName'].">".$row['ClanName']."</option>";} echo "</select>";echo "<input type='submit' value='Delete'>";echo "</form>";?>

Delete.php

<?php$var = $_GET['select'];// Make a MySQL Connectionmysql_connect("localhost", "username", "password") or die(mysql_error());mysql_select_db("greysoul_leaguedb") or die(mysql_error());$delete = mysql_query("DELETE FROM ClanName WHERE ClanName='$var'")or die(mysql_error());$rsdeleted =mysql_query($delete);//error here... (my if then else is always backwards!!!!) FIXED.if (!$rsdeleted) { echo "Row deleted successfully"; }else{echo "Nothing deleted";}?>

Link to comment
Share on other sites

You want to POST instead of GET?Change this line.

echo "<form action='delete.php' method='post' name='myform' id='myform'>";

and this line

$var = $_POST['select'];

If this doesn't work then you broke it!sanitize the $var(PHP 5 and above)$var = mysql_real_escape_string($_GET['select']); OR $var = mysql_real_escape_string($_POST['select']);(PHP 5 and less)$var = mysql_escape_string($_GET['select']); OR $var = mysql_escape_string($_POST['select']);

Link to comment
Share on other sites

i changed back to the original code you had in the first page, so i don't get that error that table doesn't exist. and i used the updated delete.php code. i changed the ("DELETE FROM ClanName WHERE ClanName='$var'") to ("DELETE FROM Clans WHERE ClanName='$var'")...because the table itself is called Clans. It posts "Row deleted successfully" now..but it didn't really delete.

Link to comment
Share on other sites

<?php// Make a MySQL Connectionmysql_connect("localhost", "username", "password") or die(mysql_error());mysql_select_db("greysoul_leaguedb") or die(mysql_error());// Get all the data from the table$result = mysql_query("SELECT * FROM Clans") or die(mysql_error());  echo "<form action='delete.php' method='get' name='myform' id='myform'>";echo "<select name='select' id='select'>";// keeps getting the next row until there are no more to getwhile($row = mysql_fetch_array( $result )) {// Print out the contents of each rowecho "<option value=".$row['ClanName'].">".$row['ClanName']."</option>";} echo "</select>";echo "<input type='submit' value='Delete'>";echo "</form>";?>[b]delete.php[/b]<?php$var = $_GET['select'];// Make a MySQL Connectionmysql_connect("localhost", "username", "password") or die(mysql_error());mysql_select_db("greysoul_leaguedb") or die(mysql_error());$delete = mysql_query("DELETE FROM Clans WHERE ClanName='$var'")or die(mysql_error());$rsdeleted =mysql_query($delete);//error here... (my if then else is always backwards!!!!) FIXED.if ($rsdeleted) { echo "Row deleted successfully"; }else{echo "Nothing deleted";}?>

Link to comment
Share on other sites

yeah really sorry about the mass confusion and what not. thanks for sticking with me. is all of the information you just helped me with in the sql tutorials? i don't want to jump into things i'm not ready for anymore...can you point me in the right direction?

Link to comment
Share on other sites

all i changed basically was where the info was pulling from$result = mysql_query("SELECT * FROM Clans") in the first page and("DELETE FROM Clans WHERE ClanName='$var'") in the second.quick question...the $row variable..is that a constant within php? the rsdeleted..is a user created variable? my main confusion to start with was thinking this could all be done from the same page..i didn't know it required a second page to submit to.
Link to comment
Share on other sites

You can do it with one page, since you only starting - it's simpler to teach using more pages.Yes you can use one page with one connect statement and two queries to process what you need based on the varaible.constants? I have absolutely no clue what that is.I know as much as you do.

Link to comment
Share on other sites

This is not needed like this, but since you posted saying it didn't work - I made it work.

$delete = mysql_query("DELETE FROM Clans WHERE ClanName='$var'")or die(mysql_error());$rsdeleted =mysql_query($delete);if ($rsdeleted) { echo "Row deleted successfully"; }else{echo "Nothing deleted";}

it could just be...

mysql_query("DELETE FROM Clans WHERE ClanName='$var'")or die(mysql_error());

but how you gonna know if it was deleted?you could check for which row was affected instead with mysql_affected_rows

mysql_query("DELETE FROM Clans WHERE ClanName='$var'");printf("Records deleted: %d\n", mysql_affected_rows());

Link to comment
Share on other sites

yes i changed it to:

$delete = mysql_query("DELETE FROM Clans WHERE ClanName='$var'")or die(mysql_error());if ($delete) {echo "Row deleted successfully";}else{echo "Nothing deleted";}?>

i had to remove that exclamation point in front of $delete though, because even though it was deleting it printed that it didn't. why was that there in the !$resdeleted code like below? i'm referring to the exclamation.

if (!$rsdeleted) {echo "Row deleted successfully";}else{echo "Nothing deleted";}?>

Link to comment
Share on other sites

Wow, that took a while to read.With this code:$delete = mysql_query("DELETE FROM Clans WHERE ClanName='$var'")or die(mysql_error());$rsdeleted =mysql_query($delete);The second line is not doing anything, and actually producing an error. The first line runs the query, and saves the return value to $delete. The manual says this about the return value for mysql_query:

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
So, the return value will either be true or false. That means that $delete will be true or false. So the second line which sends $delete to mysql_query is sending a boolean value, not a query to execute. So no, that second line is not needed and is in fact an error.Moreover, checking the return value from a delete query will not tell you if any rows were deleted. Even if no rows were deleted it will still return true, which indicated that the query ran successfully (even though it didn't delete anything, it ran without an error). The manual has this to say about checking for affected rows:
Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
Any identifier starting with $ is a variable. Constants are defined using the define function, and are not prepended with $:define('DB_NAME', 'mydb');mysql_select_db(DB_NAME);By convention constants are usually written in caps.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...