Query Results and buttons

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
srmsound
Forum Newbie
Posts: 2
Joined: Mon Dec 20, 2010 12:10 pm

Query Results and buttons

Post by srmsound »

Hi. I am kinda new to PHP and MySQL but learn fast and am willing to take on a challenge. Right now I have one of those challenges. :) At this point I can search the database and have it give me my results in a table. I am working on being able to use

Code: Select all

sql_query SELECT *  FROM my_database WHERE username LIKE $search_criteria
and output looking like this:
name1 view edit
name2 view edit
name3 view edit

The view and edit columns will be buttons so that when clicked will bring up a page to either view/edit the username that is in the same row as the buttons. Here is where the newbie comes out. :) I can't figure out how to associate the username of name1 to the view/edit buttons in row 1 of the results. I'm thinking I will need to declare a variable ($name) and assign it the contestant's name. The variable $name would then be the value of the button. Then, as each row gets outputted it would declare $name=username then output the table and when the button is clicked it passes the value of the username to the script. Would that work or is there a better way to do this??
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Query Results and buttons

Post by califdon »

Usually each button is going to either be a hyperlink--that is, within an HTML anchor element, <a ...> </a>--or it will have an onClick event property that calls a Javascript function to redirect the browser to a new page. In either case, your PHP needs to build the HTML code so that the URL of the redirect contains the key value you will use to determine which page, or what content it should have. I would suggest using something other than a name if you are going to search a database, because if you have duplicate names in your database, a query may return multiple rows and your PHP logic won't work. Normally you would use a unique user ID or something. Your goal is to end up with HTML code that looks either something like this:

Code: Select all

...
<tr><td>Jones</td>
<td><a href='view.php?id=152'><input type='button'  value='View'></a></td>
</td>
<td><a href='edit.php?id=152><input type=button' content='Edit'></a></td></tr>
...
or like this:

Code: Select all

...
<tr><td>Jones</td>
<td><input type='button' value='View' onClick='window.location="view.php?id=152";'></td>
<td><input type='button' value='Edit' onClick='window.location="edit.php?id=152";'></td>
</tr>
...
In either case, your scripts view.php and edit.php would use the $_GET array to recover the id number so that you could query your database for the information you need and create either a view page or an edit page.
srmsound
Forum Newbie
Posts: 2
Joined: Mon Dec 20, 2010 12:10 pm

Re: Query Results and buttons

Post by srmsound »

Thank you! Thank you! Thank you! :) Once I saw the code it made it so simple. I have never actually used the GET method before so now I can see why people would actually use it. You were an excellent help.

Kevin
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Query Results and buttons

Post by califdon »

You're welcome, Kevin, that's the reason we're here. If you need help with the $_GET part, you can read up on it in online tutorials and if you have any questions at that point, come back here and we'll help you. Season's Greetings!
Post Reply