Jump to content

Why won't the row delete?


ianco

Recommended Posts

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

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.";
}

 

 

:facepalm:

 

He meant in the SQL.

 

And yes, I meant the SQL.  But your HTML should have quotes around attribute values as well.

 

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.