student101 Posted February 24, 2010 Share Posted February 24, 2010 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 More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 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 More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 i think theres a typo in the first one, but i'm not real sure where it is. on the first page i get back "Table 'greysoul_leaguedb.ClanName' doesn't exist" Link to comment Share on other sites More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 db field name is ClanName. db name is greysoul_leaguedb.That's what you wrote.The only reason(s) you'ld get that error is when it doesn't exist or your PHP, MySQL versions are different. Link to comment Share on other sites More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 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 More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 i thought by field name you meant the column name, not the table, oops Link to comment Share on other sites More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 <?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 More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 i got it working now...wow Link to comment Share on other sites More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 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 More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 i got it working now...wowCool, wat did you change? Link to comment Share on other sites More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 Start herehttp://w3schools.com/php/default.aspThen herehttp://w3schools.com/php/php_mysql_intro.aspEnjoy! Link to comment Share on other sites More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 Start herehttp://w3schools.com/php/default.aspThen herehttp://w3schools.com/php/php_mysql_intro.aspEnjoy!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 More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 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 More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 I guess my definition of a constant would be a variable that cannot be changed. Like if you changed it to rows, would it still work? $row is a variable right? Or am I way off base. Link to comment Share on other sites More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 Like if you changed it to rows, would it still work? $row is a variable right?Dude, I don't know?Try it, if it works cool otherwise it is what you thought it was. Link to comment Share on other sites More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 Final question. What's the need for rsdeleted. It's by definition the same as delete right? To me I see it as you're saying, apples=redred=prettywouldn't you just say apples=pretty and skip the second line? Just wondering the purpose Link to comment Share on other sites More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 Dude, I don't know?Try it, if it works cool otherwise it is what you thought it was.hm ok well thanks again for the help. it was greatly appreciated. Link to comment Share on other sites More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 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 More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 $delete = mysql_query("DELETE FROM cities WHERE cityid='$var'")or die(mysql_error());if ($delete) {echo "Row deleted successfully";}else{echo "Nothing deleted";} i guess i was wondering why it couldn't be that Link to comment Share on other sites More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 does it work? Link to comment Share on other sites More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 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 More sharing options...
justsomeguy Posted February 24, 2010 Share Posted February 24, 2010 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 More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 why was that there in the !$resdeleted code like below? i'm referring to the exclamation. PHP operators ! means NOT equal tojustsomeguy knows how and why that works like that, I don't (used it on many occasions and always get confused 3 years and still ) Link to comment Share on other sites More sharing options...
student101 Posted February 24, 2010 Share Posted February 24, 2010 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.Now I learnt something, thanks! Link to comment Share on other sites More sharing options...
Greysoul Posted February 24, 2010 Author Share Posted February 24, 2010 lol hopefully one day i can be helpful to others as you guys have been to me Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.