Jump to content

[SOLVED] Delete button in PHP to remove MySQL row


trassalg

Recommended Posts

What I want to do is have a button on each line so the user can click that delete button and it removes the respective row in the database table.  I think there's 2 problems I have here:

 

1) My DELETE FROM query may be incorrect (not sure about the " or ' ad their placement)

2) I have each table row (in the HTML output) labeled with a number based on their respective id (table column & primary key).  What I'm trying to do is have the "Delete" button select that primary key and delete it's row from the table.

 

<?php
echo "<hr>\n\n";

$query = "SELECT id, url, companyName FROM logoTable";
$result = mysql_query($query);
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

$row['id'] = $rowid[''];
echo $rowid;

$query_delete = "DELETE FROM 'logoTable' WHERE 'id' = '$rowid'";
$result_delete = mysql_query($query_delete);

echo "<table width=800 align=center bgcolor=#FFCCFF border=1>\n";
echo "<form action =\"adManager.php\" method=\"POST\"><td>\n";
echo "  <tr>\n    <td width=50%><strong><u>Logo</u></strong></td>\n";
echo "    <td width=50%><strong><u>Applied to:</u></strong></td>\n</tr>\n";
while ($row = mysql_fetch_assoc($result)) {
	echo "  <tr>\n";
	echo "    <td>";
	echo "<img src=";
	echo $row['url'];
	echo ">";
	echo "</td>\n";
	echo "    <td>";
    echo $row['companyName'];
	echo "</td>\n";
	echo "    <td value=\"";
	echo $row['id'];
	echo "\"";
	echo " name=\"";
	echo $row['id'];
	echo "\">";
    echo $row['id'];
	echo "</td>\n";
	echo "    <td><input type=\"";
	echo "hidden\" name=";
	echo "\"query_delete\" value=\"";
	echo $row['id'];
	echo "\">";
	echo "<input type=\"submit\" value=";
	echo "\"Borrar Logo\">";
	echo "</td>\n</tr>\n";
}
echo "</form>\n";
echo "</table>\n";

?>

Link to comment
Share on other sites

I don't know if this can help you but here is the part that is on my index page of the admin area which lists all records in the database with a link to delete and edit the record

 

	    <table>			
	<?php 
	    include '../includes/connection.php';

	    $result = mysql_query("SELECT * FROM content ORDER BY title ASC"); // Scan Database using SQL query string

	    while ($row = mysql_fetch_array($result)) {
		?>
		<tr>
		    <td width="200"><? echo $row['title'];?></td>
		    <td width="75"><a href="delete.php?id=<? echo $row['id']; ?>"><img border="0" src="../images/delete1.gif" alt="Delte" /></a>
		    <td width="75"><a href="edit.php?id=<? echo $row['id']; ?>"><img border="0" src="../images/edit1.gif" alt="Edit" /></a></td>
		</tr>
		<?
	    } // Closes While Loop
	?>
    </table>

 

and here is the page that deletes the record

 

<?php 
	    include '../includes/connection.php';

	    $id=$_GET['id'];
	    $title=$_POST['title'];
	    $comments=$_POST['comments'];

	    $query = "DELETE FROM content WHERE id='$id'";
	    mysql_query($query);

	    mysql_close();
	?>
	Congratulations - deleted record <?php echo( "$title" ); ?><br /><br />

Link to comment
Share on other sites

It's not really a client... moreso a favor for my girlfriend's father.  So not charging anyone anything.

 

The sloppy code is cause this is my first database I've ever done (and first time using php as well) so learning as I go.  My extensive training consists of the book "PHP and MySQL for Dummies".  I'm trying to work out a better understanding of the nested quotes, single vs. double quotes, etc.  Which is why I split the echo statements into a million lines, when I'm sure it could be written much more simply.

 

Sorry bout the code tags... will do in the future.

Link to comment
Share on other sites

It's all on a local server so I've posted connection.inc and misc.inc as is:

 

connection.inc

 

<?
$connection = mysql_connect($host,$user,$password);
if (!$connection) {die('Could not connect : ' . mysql_error());}
else {echo ("");}

$db = mysql_select_db($database,$connection);
if (!$db) {die ('Could not connect to database : ' . mysql_error());}
else {echo ("");}
?>

 

misc.inc

 

<?php
  $host="localhost:8889";
  $user="root";
  $password="root";
  $database="rodolfo";
?>

Link to comment
Share on other sites

I had a very similar problem the other day.  I spoke to a friend of mine that is a coder, and he changed it to the $_GET['id'] instead of $_POST and the code was almost exactly what i'd typed with the $_POST, yet it worked.  I have no idea why!

 

As far as i'm aware, there is no harm in using $_GET as long as you do $id=mysql_real_escape_string($_GET['id'])

 

Try with the $_GET function to see if you can get it working.

 

Hope that helps :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.