View Full Version : Linking Functions to Data Displayed on Page

11-20-2006, 02:17 PM
Ok, this is probably an easy question, but since I'm new to PHP it is slowly deteriorating the mass of my brain.

I have a text input box, in which you can enter a user ID. You click 'find user' and this populates a table with this users details. So far pretty straight forward.

What I want, is a set of functions relating to this user, such as a simple button: 'Delete User' (the functionality of which is self explanatory).

I'm having a very difficult time relating these functions to the user displayed in the table. It shouldn't be difficult, but being new to PHP I don't know if I should be calling PHP from a seperate page, where to put my variables, wondering if there's a way to do this without refreshing the page, or if I should be using cookies...there seems to be so many options and my head's full of junk. I'm finding it tough to even get on the right path to the question, never mind the answer.

How would you do it? How do I delete this specific user (displayed on screen) from the database without having to enter their user ID again?

In english, please :thumbsup:

Any help welcome.

11-20-2006, 04:32 PM
You didn't mention how you're getting the data (user details) for the user.

Are you reading the data from a database?
... or I should ask, what kind of database?

This might affect the answer to your question.

Also ... how far have you gotten? Do you have the portion
where it displays the user details? If you've gotten that far,
adding the portion to delete them won't be too hard to do.

a link to what you have so far might be nice to see.


11-20-2006, 04:42 PM
Yeah, the data in the table (user data) is coming directly from the database.

When you enter a User ID and click 'Find User', it fires up a mySQL SELECT in a seperate php page and stores the values for that user in variables. In the page that is being displayed, I have an include so I can access the code and use the variables. The variables are then placed within the table.

This can all be changed of course, I'm really not sure what the best approach is...

Edit: I do have the data displayed currently. This is all working, yes.

11-20-2006, 04:48 PM
Few ways you could approach it, depending on what you want to do.

You could add a cell in your table that has a link in it that says "Delete username". This would point to a page called deleteUser.php?id=(for example). The id passed in the querystring would be the userid from your database. You can then run a query to delete that userid from your table.

Another approach maybe you have a column in your table that has a checkbox, then somewhere after the table you have a delete button, which would cycle through all of the checkboxes and delete those users(this would be the case if you wanted to delete multiple users at once).

Roughly, that's an idea.

Good luck;

11-20-2006, 04:58 PM
So I'm envisioning something like this ...

In the script that displays the table, you then have something at
the end of the table that has a link like this:

<a href="myscript.php?id=123123&action=confirm">Delete This ID</a>

It then runs a script that looks for the two variables:

the logic would flow something like this ... the script would have two
functions, one to confirm the deletion, and the other to actually delete it.

if action = confirm, then display a confirmation to delete, yes or no.

This is your data for id: $id ... blah blah blah ..
<a href="myscript.php?id=123123&action=delete">Yes, Delete it.</a>

if action = delete, then actually delete it from the database.

otherwise, just go back.

That delete script could be a PHP script all by itself to handle the
confirmation Y or N, and the actual deletion.

Keep in mind that once you define variables, when you go back to another
script, those variables are still defined (they are global). So in my example,
I used $id and $action. Make sure you're not using those anywhere else.

Sort of a rough scenerio, but it's hard to give actual code without seeing
what you already have. But it might get you started in the right direction.

11-20-2006, 05:28 PM
Thank you very much.

I did write out a long list of questions...but in reality, all I need to know is this:

]You could add a cell in your table that has a link in it that says "Delete username". This would point to a page called deleteUser.php?id=(for example). The id passed in the querystring would be the userid from your database.

How do I get the User ID into the url? Obviously this can't be hardcoded. I mean I may not be creating a the next mySpace, but the whole point of having a member based system is to have more than one member :rolleyes:

11-20-2006, 06:34 PM
Yes, you generate the link with the userid from your table, e.g when you are displaying all the other info in the table it's merely taking the row variable from the database and tacking it onto the querystirng.

while($row = mysql_fetch_assoc($result))
// building a table in your loop? use the userid from here and put it at the end of your querystring

Posting your code where you are building the table will allow us to show you in example.

Good luck;

11-20-2006, 06:39 PM
Thanks again.

I don't have any loops. I'm only displaying the one user on the page, based on a search for the user id.

11-20-2006, 06:40 PM
yea ... if it's displaying the ID in the table, then it must
already be a variable ...

something like this:

<a href="myscript.php?id=<?=$id?>&action=confirm">Delete This ID</a>

11-20-2006, 06:43 PM
Right, loop or no loop the variable is there.
mlseim has it.

11-20-2006, 07:15 PM
Aha..thank you

I did try this, but had no '=' in my <?=$id?> which meant no results.

All working now....time to kick some data's ***.

Much appreciated.

11-20-2006, 07:34 PM
great ...

if you get stuck on the confirmation/delete code, post some of it
and we'll see what we can do. I just think it's important to at least
give a confirmation of YES or NO before you delete anything.

11-21-2006, 04:58 PM
I'm still having a struggle with this...mainly with my if statements...

I'm currently saying something like this:

then find the user and display user on screen

but then, if I do this:

grab the user id and delete that user

...it has a tendency to also run the code in the first if too (request userID). I don't believe I'm using a feasible method here...any help? What should I be using to say that the user has clicked a specific button then run the code on it?

Or should I have the code for each function in a seperate page (Surely that's not necesarry)?

11-21-2006, 06:12 PM
On the page, you might have the link to delete user something like this ...

<a href="myscript.php?userID=<?=$userID?>&action=confirm">Delete This ID</a>

Then, in your script, you grab the variables ($userID and $action) ...


//confirm deletion
//delete user

11-21-2006, 10:36 PM
Thanks, I think I'm beginning to understand it better now. 'Beginning' being the most appropriate word...

I've been out and bought myself a book too, so maybe I'll actually get somewhere.

11-22-2006, 01:39 PM