themightydude Posted January 23, 2008 Share Posted January 23, 2008 Heres the code I have right now. Some of it I've borrowed from other scripts...and some of it I wrote. What this does, is select all data from a database..and display it in a table. It puts "id" into an array with check boxes and allows you to check records you want to delete. What I'm working on now is the ability to have an "edit" button or hyperlink or whatever next to the rows as they are populated allowing me edit that specific record. Heres the code so far: <?php // Show Records - show_record.php function show_records() { if (isset($_POST['remove']) && isset($_POST['id']) && is_array($_POST[id])) { foreach ($_POST['id'] as $id) { if (!mysql_query("DELETE FROM value WHERE id = '$id'")) { echo error_message(sql_error()); } } } else { echo "Error: The ID is not an array!<br />\n"; } /*$query = "SELECT * FROM value ORDER BY aircraft_fleet";*/ $result = mysql_query("SELECT * FROM value ORDER BY aircraft_fleet"); /*$result = mysql_query("SELECT * from value ORDER BY fleet_type");*/ echo "<form action='{$_SERVER['PHP_SELF']}' method ='POST'>\n". "<table border='1' cellspacing='4' cellpadding='1' bgcolor='#DDDDDD'>\n". " <tr valign='top'>\n". " <td align='left'> <font size='2'> <b>Year</b></td></font>\n". " <td align='left'> <font size='2'> <b>Aircraft Type</b></td></font>\n". " <td align='left'> <font size='2'> <b>Tail Number</b></td></font>\n". " <td align='left'> <font size='2'> <b>Egines</b></td></font>\n". " <td align='left'> <font size='2'> <b>APU</b></td></font>\n". " <td align='left'> <font size='2'> <b>Lease Value</td></font>\n". " <td align='left'> <font size='2'> <b>Engine Value</td></font>\n". " <td align='left'> <font size='2'> <b>Engine Lease Value</td></font>\n". " <td align='left'> <font size='2'> <b>Aircraft Value</td></font>\n". " <td align='left'> <font size='2'> <b>Serial Number</td></font>\n". " <td align='left'> <font size='2'> <b>Vintage</td></font>\n". " <td align='left'> <font size='2'> <b>APU Lease Value</td></font>\n". " <td align='left'> <font size='2'> <b>Data Type</td></font>\n". " <td align='left'> <font size='2'> <b>Notes</td></font>\n". " <td align='left'> <font size='2'> <b>Engine Manafacturer</td></font>\n". " <td align='left'> <font size='2'> <b>Month</td></font>\n". " <td align='left'> <b>  </td>\n". " <td align='left'> <font size='2'> <b>Delete</td></font>\n". " </tr>\n"; while ($row = mysql_fetch_array($result)) { echo " <tr>\n". " <td><font size='2'> ".$row['year']. "</font></td>\n". " <td><font size='2'> ".$row['aircraft_fleet']. "</font></td>\n". " <td><font size='2'> ".$row['tail_number']. "</font></td>\n". " <td><font size='2'> ".$row['engines']. "</font></td>\n". " <td><font size='2'> ".$row['apu']. "</font></td>\n". " <td><font size='2'> ".$row['lease_value']. "</font></td>\n". " <td><font size='2'> ".$row['engine_value']. "</font></td>\n". " <td><font size='2'> ".$row['engine_lease_value']. "</font></td>\n". " <td><font size='2'> ".$row['aircraft_value']. "</font></td>\n". " <td><font size='2'> ".$row['serial_number']. "</font></td>\n". " <td><font size='2'> ".$row['vintage']. "</font></td>\n". " <td><font size='2'> ".$row['apu_lease_value']. "</font></td>\n". " <td><font size='2'> ".$row['data_type']. "</font></td>\n". " <td><font size='2'> ".$row['notes']. "</font></td>\n". " <td><font size='2'> ".$row['engine_mfg']. "</font></td>\n". " <td><font size='2'> ".$row['month']. "</font></td>\n". " <td><font size='2'>   </font></td>\n". " <td><input type='checkbox' name='id[]' value='{$row['id']}'>{$row['id']}</input</td>\n". " <td><a href='edit.php');\"></a></td>\n". " </tr>\n"; } echo " <tr>\n". " <td colspan='5' align='right'><input type='submit' name='remove' value='Delete' onclick=\"return confirm('Are you sure you want to delete the selected records?');\" /></td>\n". " </tr>\n". " </table>\n". "</form>"; mysql_free_result($result); mysql_close(); } ?> I was thinking I could I could use $_POST on my "edit.php" page to pull the data over for the selected row that is being edited. This would in turn populate the text boxes and what not on the next page allowing them to be edited, then updated. However part of the problem I have right now, is my form action is currently using php_self. Is it possible to do another <form> pull the id over to the edit.php page? If I can just get id pulled over to edit.php in a variable then I can do a SQL lookup against that ID to pull the data associated with that ID into text boxes so it can be edited. You'll have to forgive me as I'm relatively new at PHP, and still learning. So if anyone has any points or good reads on how I can accomplish this, I would be appreciative. Thanks! Matt Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/ Share on other sites More sharing options...
revraz Posted January 23, 2008 Share Posted January 23, 2008 Use $_GET instead of POST and check for something like page.php?edit=1 Which means edit record 1 Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/#findComment-447153 Share on other sites More sharing options...
themightydude Posted January 23, 2008 Author Share Posted January 23, 2008 Use get on the edit.php page? Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/#findComment-447166 Share on other sites More sharing options...
revraz Posted January 23, 2008 Share Posted January 23, 2008 Well if you have a edit.php page, then you just need to send like edit.php?id=1 and then on edit.php use $_GET['id'] What I do is have a useradmin.php page, and then I combine my View, Edit and Delete all on the same page, and just use $_GET to grab what action I want and what ID to do it on. Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/#findComment-447167 Share on other sites More sharing options...
themightydude Posted January 23, 2008 Author Share Posted January 23, 2008 Ahh I see. Thank you...I'll give that a shot, and let you know how it goes. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/#findComment-447176 Share on other sites More sharing options...
themightydude Posted January 24, 2008 Author Share Posted January 24, 2008 You'll have to forgive me for being a noob on this. I'll be working on this tommorrow at work...I have some php books, and google as well as this site has been truly helpful. Looking over my code and reading up on $_GET. I'll need to change everything in my code from $_POST to $_GET? For example...right now $_POST sends 'id' to php_self right now which then processes that id to delete..still not totally clear on that, as thats a part I borrowed. Though I am familiar with $_POST and how it sends data to whatever action is specified, I'm not completely clear on the while and foreach loop at the beginning of this code. Bear with me here so I can see if I understand this properly: This code checks if the remove button has been pressed, and checks if 'id' has a value, and if there are multiple id's selected. It then cycles thorugh all of the id's until it has all id's in an array. Then the the mysql query is run...if there is an error it processes the error message....right? if (isset($_POST['remove']) && isset($_POST['id']) && is_array($_POST[id])) { foreach ($_POST['id'] as $id) { if (!mysql_query("DELETE FROM value WHERE id = '$id'")) { echo error_message(sql_error()); } } } This is the part I'm kinda confused on: Obviously, here we have our check box that gets populated through all the records and assigns the 'id' value to that check box from the mysql_row..then to the right of that we have a edit link that points to edit.php. Farther down below we have our submit button for deleting checked records. Now, if I click the "edit" link, I'll be passing the 'id' variable to edit.php via $_GET...then in edit.php I'll have a mysql query similiar to "UPDATE table SET (fields....) = (data) where id=id...basically anyway. To pass the id to edit.php as you said I can just use get so that my edit.php link is edit.php?id=x. How do I get to that point? Change the $_POST values to $_GET? When i change the $_POST to $_GET do I change the action to edit.php?$_GET? " <td><input type='checkbox' name='id[]' value='{$row['id']}'>{$row['id']}</input</td>\n". " <td><a href='edit.php');\"></a></td>\n". " </tr>\n"; } echo " <tr>\n". " <td colspan='5' align='right'><input type='submit' name='remove' value='Delete' onclick=\"return confirm('Are you sure you want to delete the selected records?');\" /></td>\n". " </tr>\n". " </table>\n". "</form>"; Thank you for any help / insight you can provide...it is most definitely appreciated...I'm also open to any sites / tutorials out there that go over something similiar to what I am doing. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/#findComment-447560 Share on other sites More sharing options...
themightydude Posted January 24, 2008 Author Share Posted January 24, 2008 Just 1 bump...1 and only 1 Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/#findComment-447835 Share on other sites More sharing options...
revraz Posted January 24, 2008 Share Posted January 24, 2008 Going to start out by saying I didnt read the code you posted, and with that said I'll explain how I do it (not saying mine is the right way, just saying what I do). I have a page called useradmin.php. This page connects to my DB and displays all the user records. In the HTML display I have sort links in the header like: ID | Name | RegDate | Role | Action So I can click on ID and sorty by ID, I can click on Name, sort by name..etc. Under the Action column I create a link that says DELETE. I also have a link under the ID that links the user ID. Now when I build my display, for every row I create, I add a URL to the ID field that says useradmin.php?editid=1 (1 being the record number). In the Action column, I create a URL that links: useradmin.php?deleteid=1 You get the picture. Now at the top of my useradmin.php page I check my $_GET to see if it has a editid value set or a deleteid value set, if it does, I perform that action on that record. If the GET is empty, then I just display the table layout or change the sort order if one of those links were pressed. So no, you don't have to change your $_POSTs to $_GETs, you just need to create links that will send that if that is the action you want. Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/#findComment-447868 Share on other sites More sharing options...
themightydude Posted January 24, 2008 Author Share Posted January 24, 2008 Alright...got it working like that. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/87429-solved-insight-into-a-edit-button/#findComment-448109 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.