Jump to content

Approve And Deny Buttons


derekshull

Recommended Posts

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 by derekshull
Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by berridgeab
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Glad to here you have progressed :happy-04:

 

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';
?>

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.