Help dynamically deleting MySql data

Hello -

I’ve been working on creating a customer email list for a company. I’ve got a form set up so that when the user submits their info, it is saved to a database. Next I’ve created a page where authorized users can view the list of customers and their emails. Now I want to add the option for them to delete individual fields from the database… that’s where I’m all hung up. Any help would be greatly appreciated.

Below is the code:
[php]//access Address Database and Table
$DBName = “database”;
$DBConnect = @mysql_connect(“localhost”,“database”,“password”);

if ($DBConnect === FALSE)//connection error
echo "

Connection Error: " . mysql_error() . “

\n”;
else {
	
		if (@mysql_select_db($DBName, $DbConnect) === FALSE) { //error - no db selected
			echo "<p>Could not select the \"$DBName\" database: " . mysql_error($DBConnect) . "</p>\n";
			mysql_close($DBConnect);
			$DBConnect = FALSE;
			}
	
	
	}
          
          if ($DBConnect !== FALSE) { //all connections are good
              @mysql_select_db($DBName);//selects database
              $SQLstring = "SELECT * FROM Addresses ORDER BY lName"; //run query to select Client info and order by their last name
              $QueryResult = @mysql_query($SQLstring, $DBConnect); //check query result
     
              if($QueryResult === FALSE)
                  echo"<p>There are no addresses in the database database. Thank you!</p>" 
                  . "<p>Error Code " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>";
                  
              else{ //query result ran - now display result in a table
         			  echo "<table width='620px' border='none'>\n";
				  echo "<tr><th>Last Name</th><th>First Name</th><th>Address</th><th>Home Phone</th><th>Email</th></tr>\n";
				  while (($Row = mysql_fetch_row($QueryResult)) !== FALSE) {
				   	
					  echo "<tr><td>{$Row[2]}</td>";
					  echo "<td>{$Row[1]}</td>";
					  echo "<td>{$Row[8]} {$Row[5]}, {$Row[6]} {$Row[7]} </td>";
					  echo"<td>{$Row[3]}</td>";
					  echo "<td>{$Row[4]}</td>\n";
					  echo"<td>" . "<a href='displayAddress.php?" . "action=Delete%20Entry&" . "name=$Row[4]'>" . "Delete</a></td></tr>\n";  //possible way to delete records???
					  if(isset($_GET['action'])) { //run action to run query??
						$SQLDelete = "DELETE FROM 'Addresses' WHERE email = 'name'";
						$QueryDelete = @mysql_query($SQLDelete, $DBConnect);						
	
							}
		
					  }
					  echo "</table>\n";
			  }
              mysql_close($DBConnect);
              }
			  [/php]

[php]“DELETE FROM ‘Addresses’ WHERE email = ‘name’”;[/php]

From what I see, you don’t need quotes in [size=14pt]’[/size]addresses[size=14pt]’[/size] since addresses is a table in the database. You only need the quotes when your are pulling data from a particular table column, and and in your case that doesn’t happen until you actually delete the email row: [php]WHERE email = ‘name’[/php]

Hi - I tried removing the ‘’ as you suggested, but it still didn’t delete any records. I’m wondering if there is something wrong with the action - if(isset($_GET[‘action’])). Is this the way to do this? Or is there another way? Something I’m missing?

[php] echo"

" . “” . “Delete \n”; //possible way to delete records???

if(isset($_GET[‘action’])) { //run action to run query??

}[/php]

I definitively know the quotes don’t go there.

Oh, I think I spotted another problem, and this should probably do it:

Problem: Take a look at email =‘name’
[php]$SQLDelete = “DELETE FROM ‘Addresses’ WHERE email = ‘name’”;[/php]

Remember that Email is a post variable and should be as follow: [size=12pt]$[/size]email. Thus:
[php]$SQLDelete = “DELETE FROM Addresses WHERE $email = ‘name’”;[/php]
and remember to get rid of the quotes on [size=12pt][/size]Addresses[size=12pt]’[/size]

Anyway, I am not an expert by any mean, and I am learning as well. However, here’s an implementation I used before, maybe you can adapt it to your own:

Delete Function:
[php]function deletePosts($id){
$id = (int) $id;// TYPECASTING to avoind SLQ injections
mysql_query("DELETE FROM content WHERE id= ‘$id’ ") or die (mysql_error() );
{
echo “1 Record Deleted”;}
echo’’;
//header(“location: posts.php”);
}[/php]

You can also use the code directly, i.e, not as a function:
[php]

$id = (int) $id;// TYPECASTING to avoid SLQ injections
mysql_query("DELETE FROM content WHERE id= '$id' ") or die (mysql_error() );
{
	echo "<span class='current_admin'>1 Record Deleted</span>";}
	echo'<META HTTP-EQUIV="Refresh" CONTENT="1; URL=posts.php">';
	//header("location: posts.php");	
}

One thing I forgot. If you use the function, you call it from your delete.php page as follow:

delete.php
[php]<?
PHP include(‘includes/functions.php’); //include functions file, where function delete(Posts) resides (in a folder called includes)

deletePosts($_GET[‘id’]); //function call
?>[/php]

Again, here’s that function file:

functions.php
[php]//Select current item id so it can be deleted
function deletePosts($id){
$id = (int) $id;// TYPECASTING to avoind SLQ injections
mysql_query("DELETE FROM content WHERE id= ‘$id’ ") or die (mysql_error() );
{
echo “1 Record Deleted”;}
echo’’;
//header(“location: posts.php”);
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service