By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,273 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,273 IT Pros & Developers. It's quick & easy.

Mysql table rows delete HELP

P: n/a
Hy,

Sorry for my terreble english

I have this simple code for deleting rows in mysql table...
Everything works fine with it.
So, what do i wanna do...:
my sql table looks something like this:
id Name Surname pictr0 picrt1 pictr2
pictr3
1 Ivan Johnes 1.jpg 120.jpg 111.jpg
pic11.jpg
2 John Doe 2.jpg pic123.jpg img13.jpg
pic12.jpg

images (pictr0, pictr1...) are located in images/ folder..., and when i
delete one row i wanna pictures deleted too.
I have used this script, buth i realised i'm only deleting data in sql
tabel, and images are filling my web hosting space.
Any ideas?

Tnx in advance
Dejan
----------------------------------------

<?php
$host="localhost"; // Host name
$username="xyxxx"; // Mysql username
$password="xxxxx"; // Mysql password
$db_name="delete"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1"
bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in
mysql</strong> </td>
</tr>
<tr>
..
..(simple html code)
..
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]"
type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete"
type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>
Apr 8 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Sat, 8 Apr 2006 12:34:20 +0200, "Dejan"
<dd*************@veleri.hr> wrote:
Hy,

Sorry for my terreble english

I have this simple code for deleting rows in mysql table...
Everything works fine with it.
So, what do i wanna do...:
my sql table looks something like this:
id Name Surname pictr0 picrt1 pictr2
pictr3
1 Ivan Johnes 1.jpg 120.jpg 111.jpg
pic11.jpg
2 John Doe 2.jpg pic123.jpg img13.jpg
pic12.jpg

images (pictr0, pictr1...) are located in images/ folder..., and when i
delete one row i wanna pictures deleted too.
I have used this script, buth i realised i'm only deleting data in sql
tabel, and images are filling my web hosting space.
Any ideas?

Tnx in advance
Dejan
----------------------------------------

<?php
$host="localhost"; // Host name
$username="xyxxx"; // Mysql username
$password="xxxxx"; // Mysql password
$db_name="delete"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1"
bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in
mysql</strong> </td>
</tr>
<tr>
.
.(simple html code)
.
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]"
type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete"
type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>


Have a look at this -
http://uk.php.net/file_exists
and then
http://uk.php.net/unlink
Remove _your_teeth_ to reply.
Apr 8 '06 #2

P: n/a
Also, is that really the structure of your table!?!

I'd consider a structure like this instead...

users

user_id f_name l_name
1 John Lennon
2 Paul McCartney
3 George Harrison
4 Ringo Starr

images

image_id image_name
1 yoko.jpg
2 julia.jpg
3 linda.jpg
4 heather.jpg
5 patti.jpg
6 olivia.jpg
7 barbara.jpg

user_images

user_id image_id
1 1
1 2
2 3
2 4
3 5
3 6
4 7

Apr 8 '06 #3

P: n/a
"strawberry" <za*******@gmail.com>
I'd consider a structure like this instead...
And I think this will be better (if two different users cannot share one
image):
users

user_id f_name l_name
1 John Lennon
2 Paul McCartney
3 George Harrison
4 Ringo Starr

images

image_id image_name user_id
1 yoko.jpg 1
2 julia.jpg 2
3 linda.jpg 1
4 heather.jpg 2
5 patti.jpg 1
6 olivia.jpg 3
7 barbara.jpg 3

Regards,
Talthen

Apr 9 '06 #4

P: n/a
hey pal :) you are not deletin the files
because in php code you wrote del " $sql = "DELETE FROM $tbl_name WHERE
id='$del_id'";
$result = mysql_query($sql);" but wont you try do write php code to delete files too like"ifdel name then del *.jpg;" :) then put it there its gonna work

by the way i am not php programmer but i undestand it because i have
knowledge of other programs and thats the mistake yuo have put there
you delete only name by code (only one action) but not jpgs

Apr 10 '06 #5

P: n/a
I know about this two functions.
I'm looking for a way to implement something like this:
When i'm deleting row, script select's this 4 coloumns form that row first,
put's it into an array.
Then i have file names in array, and i cann delete (unlink etc.) them, and
the row.
I understend that, buth i don't know how to implement it in this delete
loop.
Dejan
<haggis@_your_teeth_nerdshack.com> wrote in message
news:5a********************************@4ax.com...
On Sat, 8 Apr 2006 12:34:20 +0200, "Dejan"
<dd*************@veleri.hr> wrote:
Hy,

Sorry for my terreble english

I have this simple code for deleting rows in mysql table...
Everything works fine with it.
So, what do i wanna do...:
my sql table looks something like this:
id Name Surname pictr0 picrt1 pictr2
pictr3
1 Ivan Johnes 1.jpg 120.jpg 111.jpg
pic11.jpg
2 John Doe 2.jpg pic123.jpg img13.jpg
pic12.jpg

images (pictr0, pictr1...) are located in images/ folder..., and when i
delete one row i wanna pictures deleted too.
I have used this script, buth i realised i'm only deleting data in sql
tabel, and images are filling my web hosting space.
Any ideas?

Tnx in advance
Dejan
----------------------------------------

<?php
$host="localhost"; // Host name
$username="xyxxx"; // Mysql username
$password="xxxxx"; // Mysql password
$db_name="delete"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1"
bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in
mysql</strong> </td>
</tr>
<tr>
.
.(simple html code)
.
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]"
type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete"
type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>


Have a look at this -
http://uk.php.net/file_exists
and then
http://uk.php.net/unlink
Remove _your_teeth_ to reply.

Apr 10 '06 #6

P: n/a
My table structure i's like that.
No one cann't have same image name. My data in the table really look's like
this...

id Name Surname Image1
Image2
mmijic27542 Mirjana Mijic mmijic27542_776_mijic1_v.jpg
mmijic27542_2_778_mijic3_v.jpg

and manny more coloumns in this row.
Id, and images names are generated random during insert, depending on
peoples name, surname, and random function.
So no one cann't have the same id, or images names.

Dejan

strawberry <za*******@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Also, is that really the structure of your table!?!

I'd consider a structure like this instead...

users

user_id f_name l_name
1 John Lennon
2 Paul McCartney
3 George Harrison
4 Ringo Starr

images

image_id image_name
1 yoko.jpg
2 julia.jpg
3 linda.jpg
4 heather.jpg
5 patti.jpg
6 olivia.jpg
7 barbara.jpg

user_images

user_id image_id
1 1
1 2
2 3
2 4
3 5
3 6
4 7

Apr 10 '06 #7

P: n/a
I know how to delete files in simple code, but here i have some kind of
loop. As i have writen before this is my req:
I'm looking for a way to implement something like this:
When i'm deleting row, script select's this 4 coloumns form that row first, put's it into an array.Then i have file names in array, and i cann delete (unlink etc.) them, and the row.I understend that, buth i don't know how to implement it in this delete loop.

Dejan
-----------------
fastrat <tu**********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com... hey pal :) you are not deletin the files
because in php code you wrote del " $sql = "DELETE FROM $tbl_name WHERE
id='$del_id'";
$result = mysql_query($sql);" but wont you try do write php code to
delete files too like"ifdel name then del *.jpg;" :) then put it there its
gonna work by the way i am not php programmer but i undestand it because i have
knowledge of other programs and thats the mistake yuo have put there
you delete only name by code (only one action) but not jpgs

Apr 10 '06 #8

P: n/a
Dejan wrote:
Hy,

Sorry for my terreble english

I have this simple code for deleting rows in mysql table...
Everything works fine with it.
So, what do i wanna do...:
my sql table looks something like this:
id Name Surname pictr0 picrt1 pictr2
pictr3
1 Ivan Johnes 1.jpg 120.jpg 111.jpg
pic11.jpg
2 John Doe 2.jpg pic123.jpg img13.jpg
pic12.jpg

images (pictr0, pictr1...) are located in images/ folder..., and when i
delete one row i wanna pictures deleted too.
I have used this script, buth i realised i'm only deleting data in sql
tabel, and images are filling my web hosting space.
Any ideas?

Tnx in advance
Dejan
----------------------------------------

<?php
$host="localhost"; // Host name
$username="xyxxx"; // Mysql username
$password="xxxxx"; // Mysql password
$db_name="delete"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1"
bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in
mysql</strong> </td>
</tr>
<tr>
.
.(simple html code)
.
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]"
type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete"
type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>


You'll need to fetch the rows first, then delete. For instance - if `id` is the
primary key to your table:

foreach($checkbox as $del_id) { // Personal preference - I like foreach
$query = "select pictr0, pictr1, pictr2, pictr3 from $tblname where id=$del_id";
$result = mysql_query($query);
if ($result && mysql_num_rows > 0) {
$pics = mysql_fetch_array($result);
unlink($pictr0);
unlink($pictr1);
unlink($pictr2);
unlink($pictr3);
$query = "DELETE FROM $tblname WHERE id='$del_id'";
$result = mysql_query($query);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
else
if ($result == null)
echo "Error fetching data: " . mysql_error();
else
echo "No data returned";
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 10 '06 #9

P: n/a
This is true help, thank you very much
Dejan.
Jerry Stuckle <js*******@attglobal.net> wrote in message
news:MY******************************@comcast.com. ..
Dejan wrote:
Hy,

Sorry for my terreble english

I have this simple code for deleting rows in mysql table...
Everything works fine with it.
So, what do i wanna do...:
my sql table looks something like this:
id Name Surname pictr0 picrt1 pictr2
pictr3
1 Ivan Johnes 1.jpg 120.jpg 111.jpg
pic11.jpg
2 John Doe 2.jpg pic123.jpg img13.jpg
pic12.jpg

images (pictr0, pictr1...) are located in images/ folder..., and when i
delete one row i wanna pictures deleted too.
I have used this script, buth i realised i'm only deleting data in sql
tabel, and images are filling my web hosting space.
Any ideas?

Tnx in advance
Dejan
----------------------------------------

<?php
$host="localhost"; // Host name
$username="xyxxx"; // Mysql username
$password="xxxxx"; // Mysql password
$db_name="delete"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1"
bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in
mysql</strong> </td>
</tr>
<tr>
.
.(simple html code)
.
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]"
type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete"
type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>
You'll need to fetch the rows first, then delete. For instance - if `id`

is the primary key to your table:

foreach($checkbox as $del_id) { // Personal preference - I like foreach
$query = "select pictr0, pictr1, pictr2, pictr3 from $tblname where id=$del_id"; $result = mysql_query($query);
if ($result && mysql_num_rows > 0) {
$pics = mysql_fetch_array($result);
unlink($pictr0);
unlink($pictr1);
unlink($pictr2);
unlink($pictr3);
$query = "DELETE FROM $tblname WHERE id='$del_id'";
$result = mysql_query($query);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
else
if ($result == null)
echo "Error fetching data: " . mysql_error();
else
echo "No data returned";
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Apr 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.