ianco Posted April 21, 2010 Share Posted April 21, 2010 Hey all. I'm trying to delete a row from a table using a drop down menu. The code in the mysql must be wrong but i can't see where. Here's the code. Any Ideas? <?php $con = mysql_connect("localhost","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("databasename", $con); $sql="SELECT Name, Paid FROM Stag"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $Name=$row["Name"]; $thing=$row["Paid"]; $options.="<OPTION VALUE=\"$Name\">".$Name."</option>"; } ?> <br> <form action="http://xxxxxxx/delstag.php" method="post"> <SELECT NAME=thing> <OPTION VALUE=0>Choose <?=$options?> </SELECT> <input type="submit" /> </form> $result = mysql_query("DELETE FROM Stag WHERE Name = $Name"); Link to comment https://forums.phpfreaks.com/topic/199284-why-wont-the-row-delete/ Share on other sites More sharing options...
Maq Posted April 21, 2010 Share Posted April 21, 2010 First all you need single quotes around $name because, presumably, it is a string. See if that fixes the problem. Link to comment https://forums.phpfreaks.com/topic/199284-why-wont-the-row-delete/#findComment-1045985 Share on other sites More sharing options...
ianco Posted April 21, 2010 Author Share Posted April 21, 2010 THe name in the mysql code? no that hasnt changed anything. Link to comment https://forums.phpfreaks.com/topic/199284-why-wont-the-row-delete/#findComment-1046011 Share on other sites More sharing options...
ianco Posted April 21, 2010 Author Share Posted April 21, 2010 and <OPTION VALUE=\'$Name\'> doesnt work Link to comment https://forums.phpfreaks.com/topic/199284-why-wont-the-row-delete/#findComment-1046023 Share on other sites More sharing options...
Ken2k7 Posted April 21, 2010 Share Posted April 21, 2010 He meant in the SQL. Link to comment https://forums.phpfreaks.com/topic/199284-why-wont-the-row-delete/#findComment-1046077 Share on other sites More sharing options...
ianco Posted April 21, 2010 Author Share Posted April 21, 2010 Yeah that's not doing anything i'm afraid $result = mysql_query("DELETE FROM Stag WHERE Name = '$Name'"); Do I need $Name = $_POST['Name']; or is this irrelevant? Link to comment https://forums.phpfreaks.com/topic/199284-why-wont-the-row-delete/#findComment-1046083 Share on other sites More sharing options...
Maq Posted April 21, 2010 Share Posted April 21, 2010 If you're passing the value from the form the next page via POST, the 'delstag.php' your code should look something like: $Name = mysql_real_escape_string($_POST['thing']); if (!$result = mysql_query("DELETE FROM Stag WHERE Name = '$Name'")) { throw new Exception('Error: ' . mysql_error()); } else { echo $Name . " has been deleted."; } He meant in the SQL. And yes, I meant the SQL. But your HTML should have quotes around attribute values as well. Link to comment https://forums.phpfreaks.com/topic/199284-why-wont-the-row-delete/#findComment-1046084 Share on other sites More sharing options...
ianco Posted April 21, 2010 Author Share Posted April 21, 2010 Haha, Maq, that code did the job. Thank you very much Link to comment https://forums.phpfreaks.com/topic/199284-why-wont-the-row-delete/#findComment-1046090 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.