derekshull Posted October 5, 2012 Share Posted October 5, 2012 (edited) I have the following code below. I have added buttons to each entry that pops up but I need the buttons to do certain things and I'm stuck. When the Approve button is pressed I want the entire entry to go to another table called "incomplete" (no quotations) and then I want the record to be deleted from the table "needs" (no quotations), which it currently resides in. When the Deny button is pushed I want the entry to just be deleted from the table "needs" (no quotations). Tried to figure it out on my own but I have nothing. I'm new to all this and the fact that I've gotten this far makes me excited :-) $query = mysql_query("SELECT * FROM needs"); while ($rows = mysql_fetch_array($query)): $firstname = $rows['firstname']; $lastname = $rows['lastname']; $address = $rows['address']; $city = $rows['city']; $state = $rows['state']; $phone = $rows['phone']; $email = $rows['email']; $typeofneed = $rows['typeofneed']; echo "$firstname $lastname<br>$address<br>$city $state<br>$phone<br>$email<br>$typeofneed<br>"; echo "<button name='approve' type='button'>Approve</button><button name='deny' type='button'>Deny</button><br>---------------------------<br>"; endwhile; Edited October 5, 2012 by derekshull Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/ Share on other sites More sharing options...
Jagand Posted October 5, 2012 Share Posted October 5, 2012 To the extent I understood your point, if you intend that the code should perform certain action using PHP alone, you must use a form with input type='button' option. You can google/bing 'HTML FORM' for more details. Sample syntax for a form is <form action='a_page_that_performs_either_approve_action.php' method='POST'> <?php print $firstname $lastname<br>$address<br>$city $state<br>$phone<br>$email<br>$typeofneed<br> ?> <input type='submit' name='approve'>Approve</button> <input type='button' name='deny'>Deny</button> <br>---------------------------<br> </form> Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/#findComment-1382789 Share on other sites More sharing options...
derekshull Posted October 5, 2012 Author Share Posted October 5, 2012 That code is already in code I provided. I've got that far. I need to know the code to tell the script that when the approve button is pressed it should do what I said above and then when I hit deny it should do what I said above. Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/#findComment-1382791 Share on other sites More sharing options...
derekshull Posted October 5, 2012 Author Share Posted October 5, 2012 I thought about making a delete.php file and using a link to delete the entry. Here is the current code for the main script that shows the data now: $query = mysql_query("SELECT * FROM needs"); while ($rows = mysql_fetch_array($query)): $id = $rows['ID']; $firstname = $rows['firstname']; $lastname = $rows['lastname']; $address = $rows['address']; $city = $rows['city']; $state = $rows['state']; $phone = $rows['phone']; $email = $rows['email']; $typeofneed = $rows['typeofneed']; echo "$id<br>$firstname $lastname<br>$address<br>$city $state<br>$phone<br>$email<br>$typeofneed<br>"; echo "<a href=''>Approve</a> <a href='delete.php?id=$id'>Deny</a><br>---------------------------<br>"; endwhile; But what does the delete script need to look like? Would it be like this? $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die ('Cant use ' . DB_NAME . ': ' . mysql_error()); } mysql_query("DELETE FROM needs WHERE ID=$id"); Thanks for the help. Just trying to figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/#findComment-1382795 Share on other sites More sharing options...
berridgeab Posted October 5, 2012 Share Posted October 5, 2012 (edited) Yes the delete statement looks ok and should work. Though I don't know the nature of what your database / web app is used for or its table structure, but would it not be better just to have an extra field in the needs table called status, then just update the status to be 'Incomplete', rather than copying the entire row to another table? I only mention this because you say your new to this. Edited October 5, 2012 by berridgeab Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/#findComment-1382827 Share on other sites More sharing options...
derekshull Posted October 5, 2012 Author Share Posted October 5, 2012 Yeah that would work and would be a lot easier it seems :-) Thanks for mentioning it! The delete script isn't working...I click the delete link but nothing happens. Any idea why this may be? Also how would I go about updating the status field? It will initially be NULL but then when I click approve I want it to make the status "incomplete" (without quotes). Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/#findComment-1382872 Share on other sites More sharing options...
derekshull Posted October 5, 2012 Author Share Posted October 5, 2012 I tried this but didn't have any luck Script to show each record and show an approve and delete button $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die ('Cant use ' . DB_NAME . ': ' . mysql_error()); } $query = mysql_query("SELECT * FROM needs"); while ($rows = mysql_fetch_array($query)): $id = $rows['ID']; $firstname = $rows['firstname']; $lastname = $rows['lastname']; $address = $rows['address']; $city = $rows['city']; $state = $rows['state']; $phone = $rows['phone']; $email = $rows['email']; $typeofneed = $rows['typeofneed']; echo "<input type='text' disabled='true' name='idtodelete' value=$id><br>$firstname $lastname<br>$address<br>$city $state<br>$phone<br>$email<br>$typeofneed<br>"; echo "<form action='delete.php?idtodelete=$id' method='post'><input type='submit' value='Delete'></form><br>---------------------------<br>"; endwhile; Then the script to delete the record (which is delete.php): $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die ('Cant use ' . DB_NAME . ': ' . mysql_error()); } $id=$_POST['idtodelete']; mysql_query("DELETE FROM needs WHERE ID=$id"); echo "Deleted Record. <br><br> <a href='adminneeds.php'>Click here to go back.</a>"; ?> I don't get any errors but it's not deleting the record. Really confused. Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/#findComment-1382896 Share on other sites More sharing options...
derekshull Posted October 5, 2012 Author Share Posted October 5, 2012 Ok update time! :-) I got the delete button to work! Now I just need help on how to make the approve button work. I need the approve button to to update the status column of the entry to say "incomplete". Thanks for getting me going guys! Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/#findComment-1382933 Share on other sites More sharing options...
berridgeab Posted October 5, 2012 Share Posted October 5, 2012 Glad to here you have progressed If your worried about the initial database column being NULL then i suggest you add whats called a default value to the field in question. Basically when the record is created (inserted) if you don't assign a value to the field it will assign a pretermined value automatically i.e.'Uncomplete'. If you wish to simply update an existing record to 'incomplete' then simply issue this statement <?php $query = "UPDATE tableName SET field1 = '$value', field2 = '$value2', WHERE id = '$id'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/269098-approve-and-deny-buttons/#findComment-1383105 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.