473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Listing table data with an option to delete each individual item.

Lag
Was wondering if anyone could help me with a PHP, MySQL problem. I am
completely new to PHP and MySQL. I have been trying to find a way to
list data from a table on a web page and after each individual item
have a hyperlink to allow users to delete the information listed. Can
anyone help?

i.e.
Name Age Sex
------------------------------------------
Adam 21 M delete
Betty 22 F delete
Chris 23 M delete
Daisy 24 F delete

------------------------------------------------------------------------------------------------------------------------------
(delete after 'Sex' would be the hyperlink to delete that entire line
(row) from the table).

Feb 5 '06 #1
17 3441
i find it best to do thing with id numbers. the table you have shown
will obviously have a primary key for each record. so in php you do the
sql statement something like

$result = mysql_query("select id, name, age, sex from people",$db);
if ($myrow = mysql_fetch_array($result))
{
make the table header information here. ie: titles, name ages sex...
and a row for action
do
{
echo "<tr>
$id =$myrow['id']
<td>$myrow['name']</td>
<td>$myrow['age']</td>
<td>$myrow['sex']</td>
<td><a href = 'delete.php?id=$id>delete</a></td>";

}
while ($myrow = mysql_fetch_array($result))
}

then you make another page that essentially has two or three lines or
modify delete.php in the link fo $_SERVER[PHP_SELF] which come back to
the page you are looking at.

$id = $_GET['id'];
$result = mysql_query("delete from people where id=$id",$db);
header("Refresh: 0; http://full url/whateverfirstpagewascalled.php");

hope this helps, php took me 3 days of looking at examples to learn. do
a few online tutorials and www.php.net is the bible. good luck

Sean Barton

Feb 5 '06 #2
Lag
Thanks Sean.............it helped a lot.

Feb 5 '06 #3
Sean Barton wrote:
i find it best to do thing with id numbers. the table you have shown
will obviously have a primary key for each record. so in php you do the
sql statement something like

$result = mysql_query("select id, name, age, sex from people",$db);
if ($myrow = mysql_fetch_array($result))
{
make the table header information here. ie: titles, name ages sex...
and a row for action
do
{
echo "<tr>
$id =$myrow['id']
<td>$myrow['name']</td>
<td>$myrow['age']</td>
<td>$myrow['sex']</td>
<td><a href = 'delete.php?id=$id>delete</a></td>";

}
while ($myrow = mysql_fetch_array($result))
}

then you make another page that essentially has two or three lines or
modify delete.php in the link fo $_SERVER[PHP_SELF] which come back to
the page you are looking at.

$id = $_GET['id'];
$result = mysql_query("delete from people where id=$id",$db);
header("Refresh: 0; http://full url/whateverfirstpagewascalled.php");

hope this helps, php took me 3 days of looking at examples to learn. do
a few online tutorials and www.php.net is the bible. good luck

Sean Barton


You need to be VERY careful on this one. You aren't validating the data.

For instance - what happens if I type in the browser:

http://www.example.com/delete.php?id=5+OR+1%3d1

Your query ends up as "delete from people where id=5 or 1=1";

Google for 'sql injection".

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 5 '06 #4
Message-ID: <F-********************@comcast.com> from Jerry Stuckle
contained the following:
You need to be VERY careful on this one. You aren't validating the data.

For instance - what happens if I type in the browser:

http://www.example.com/delete.php?id=5+OR+1%3d1

Your query ends up as "delete from people where id=5 or 1=1";

Google for 'sql injection".


Also, I would avoid having links that delete files altogether. If a
search engine ever makes it to that page all your data will be deleted.

I usually make the table a form and use checkboxes
echo "<input type ='checkbox' name='del[]'value=$id>";

Name all the boxes 'del[]' When posted the items to be deleted will be
in an array and you can loop through it and delete them.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Feb 6 '06 #5
Geoff Berrow wrote:
Message-ID: <F-********************@comcast.com> from Jerry Stuckle
contained the following:

You need to be VERY careful on this one. You aren't validating the data.

For instance - what happens if I type in the browser:

http://www.example.com/delete.php?id=5+OR+1%3d1

Your query ends up as "delete from people where id=5 or 1=1";

Google for 'sql injection".

Also, I would avoid having links that delete files altogether. If a
search engine ever makes it to that page all your data will be deleted.

I usually make the table a form and use checkboxes
echo "<input type ='checkbox' name='del[]'value=$id>";

Name all the boxes 'del[]' When posted the items to be deleted will be
in an array and you can loop through it and delete them.


Geoff,

And what happens if I come along and post a form back to your page with:

<input type ='checkbox' name='del[]' value="1 OR 42=42">

ALWAYS validate incoming data - even if it's from a checkbox!
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 6 '06 #6
Lag
Thanks for all the feedback guys...........now I'm just scared! LOL.
I have bought a large book on PHP and studying when I can. I will look
at validating my stuff, I'm new so this will take a little while. But
my pages, directories, are password protected and reject(direct)
spiders not to crawl the site.

Again, thanks a lot guys!

-Lag.

Feb 6 '06 #7
Lag
May I ask........what did you mean when you say.........
"make the table header information here. ie: titles, name ages sex...
and a row for action"

Thanks.

Feb 6 '06 #8
to make the information appear in a standard form you need to put
everything in a table. header is simply a way of saying do your titles
here. ie:

Table
Name Sex Age Action
---------------------------------------------------------
bob M 20 Delete
the rows above the data is the header.

also i have one concern. your not storing age in a database are you??
be wary that age is a number and needs to be updated every year. what
you need to be storing is date of birth and working out the age from
there if you want to. it saves a lot of database errors in the future.

as my peers suggested validate everything. there is a variable in
$_SERVER called 'HTTP_REFERER' i think it may help. validate who is
calling the page and nobody but the page you want can access the delete
function.

dont delete anything from the database. modify the table while it is
still small. add in a checkbox field called active and set default to
yes. then modify your query for the page you want so. select * from
people where active=yes;

Good Luck

Sean Barton

Feb 6 '06 #9
Lag
Thank you very much Sean..........I promise I will not ask a question
of you again until I understand PHP a little more. LOL.

Have a good day.

-Lag.

Feb 6 '06 #10
its no problem, ive only been doing it a couple of months and im no
expert. work through a couple of php/ mysql examples online. they
really helped me. and dont forget that www.php.net wont bite although
www.mysql.com might!

regards
Sean Barton

Feb 6 '06 #11
Lag
LOL

Feb 6 '06 #12
On 2006-02-06, Sean Barton <ba*********@gmail.com> wrote:
to make the information appear in a standard form you need to put
everything in a table. header is simply a way of saying do your titles
here. ie:

Table
Name Sex Age Action
---------------------------------------------------------
bob M 20 Delete
the rows above the data is the header.

also i have one concern. your not storing age in a database are you??
be wary that age is a number and needs to be updated every year. what
you need to be storing is date of birth and working out the age from
there if you want to. it saves a lot of database errors in the future.

as my peers suggested validate everything. there is a variable in
$_SERVER called 'HTTP_REFERER' i think it may help.
don't trust it. it's easy to fake.
validate who is calling the page and nobody
but the page you want can access the delete
function.
HTTP_AUTH_USER
HTTP_AUTH_PASSWORD
dont delete anything from the database. modify the table while it is
still small. add in a checkbox field called active and set default to
yes. then modify your query for the page you want so. select * from
people where active=yes;

--

Bye.
Jasen
Feb 7 '06 #13
Message-ID: <db******************************@comcast.com> from Jerry
Stuckle contained the following:
Name all the boxes 'del[]' When posted the items to be deleted will be
in an array and you can loop through it and delete them.

Geoff,


And what happens if I come along and post a form back to your page with:

<input type ='checkbox' name='del[]' value="1 OR 42=42">

ALWAYS validate incoming data - even if it's from a checkbox!


Jerry...you're not thinking this through. The person already has
permission to delete the data.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Feb 9 '06 #14
Geoff Berrow wrote:
Message-ID: <db******************************@comcast.com> from Jerry
Stuckle contained the following:

Name all the boxes 'del[]' When posted the items to be deleted will be
in an array and you can loop through it and delete them.


Geoff,


And what happens if I come along and post a form back to your page with:

<input type ='checkbox' name='del[]' value="1 OR 42=42">

ALWAYS validate incoming data - even if it's from a checkbox!

Jerry...you're not thinking this through. The person already has
permission to delete the data.


Geoff,

Oh, I'm thinking this through all right.

The case I cited would delete everything in the table. Does the person
have THAT right?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 9 '06 #15
Message-ID: <uP******************************@comcast.com> from Jerry
Stuckle contained the following:
Jerry...you're not thinking this through. The person already has
permission to delete the data.


Geoff,

Oh, I'm thinking this through all right.

The case I cited would delete everything in the table. Does the person
have THAT right?


Ah, you got me. <g> In fact, precisely that thought occurred to me right
after I pressed send.

Yes indeed, validation would be essential if we were dealing with a
subset of the data.

That neatly demonstrates how easy it is to overlook a weakness,
especially if a simple system is being extended. Good job we all back
up our data regularly eh? ;-)
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Feb 9 '06 #16
Lag
Thanks for all the info on my question!

Feb 10 '06 #17
Lag
Got it working......thanks again!

Feb 24 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jay | last post by:
Hi I have a huge table with over 100million records and on regular basis ineed to delete nearly a million records and insert a million records. Currently I delete indexes before going through the...
3
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the button is clicked. However, that table to which a...
16
by: StenKoll | last post by:
Help needed in order to create a register of stocks in a company. In accordance with local laws I need to give each individual share a number. I have accomplished this by establishing three tables...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
4
by: Gregory Gadow | last post by:
If there is a more appropriate forum, please let me know and I will post there. Our field reps can go on to our website and select from several sets of data to create the address we then provide...
4
by: Bob | last post by:
Hi all, I'm trying to import data, modify the data then insert it into a new table. The code below works fine for it but it takes a really long time for 15,000 odd records. Is there a way I...
2
by: cogitoergosum | last post by:
Hi, I have a table with four columns. By default, except for the second column (id="s01") all column values are protected. When a value is selected from second column, the third column is...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.