trassalg Posted July 28, 2007 Share Posted July 28, 2007 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 https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/ Share on other sites More sharing options...
AdRock Posted July 28, 2007 Share Posted July 28, 2007 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 https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309741 Share on other sites More sharing options...
Barand Posted July 28, 2007 Share Posted July 28, 2007 Do you charge your clients based on the number of lines of code? Or is it your method of code obfuscation? And if you are posting code, use code tags. I'll put them in this time. Link to comment https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309744 Share on other sites More sharing options...
trassalg Posted July 28, 2007 Author Share Posted July 28, 2007 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 https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309758 Share on other sites More sharing options...
calabiyau Posted July 28, 2007 Share Posted July 28, 2007 are you having errors? what is happening? Link to comment https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309766 Share on other sites More sharing options...
trassalg Posted July 28, 2007 Author Share Posted July 28, 2007 When I hit one of the Delete buttons it reloads the page and nothing appears... no errors that I can see. Link to comment https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309772 Share on other sites More sharing options...
calabiyau Posted July 28, 2007 Share Posted July 28, 2007 could you post connection.php obviously with your own credentials xxx'ed out? Link to comment https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309773 Share on other sites More sharing options...
trassalg Posted July 28, 2007 Author Share Posted July 28, 2007 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 https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309814 Share on other sites More sharing options...
trassalg Posted July 28, 2007 Author Share Posted July 28, 2007 AdRock, Your posting fixed it for me. Thanks a million. I'd be interested in seeing the edit.php page you have as well if you'd be willing to share it. Link to comment https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309824 Share on other sites More sharing options...
Goose87 Posted July 28, 2007 Share Posted July 28, 2007 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 https://forums.phpfreaks.com/topic/62232-solved-delete-button-in-php-to-remove-mysql-row/#findComment-309825 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.