Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Delete row don't work correctly
Old 03-04-2010, 04:54 PM Delete row don't work correctly
Experienced Talker

Posts: 32
Name: Kleidi
Trades: 0
Hello to everyone!

I have a problem with a delete function. I have a script that lists all database container and a little script that offer the possibility to delete the row of that db. When i click on the delete link of a row, the delete function should get the id of the row that i want to delete and delete it.
The script looks like this:

- Main page:
PHP Code:
<?php
session_start
();

if(!isset(
$_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}
?>
<html>
<head>
<script type="text/javascript">
var form_id;
function confirm_delete(go_url)
{
var answer = confirm("Jeni te sigurte per fshirjen e ketij evenimenti?");
if (answer)
{
location=go_url;
}
}
</script>
</head>

<body>
<?php
include '/includet/variabla.php';
include (
BPATH_ADM 'includet/dbconfig.php');
include (
BPATH_ADM 'includet/dblidhja.php');
$query="SELECT * FROM `ndeshje` ORDER BY `ndeshje`.`ora`,`data`";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();
?>
<br /><br /><center><div class="ndeshjeshfaq">
<table width="598" border="0" align="center" class="ndeshjekoka">
  <tr>
    <td width="25" class="ndshfaqid">ID</td>
    <td width="35" class="ndshfaqsporti">Sporti</td>
    <td width="265" class="ndshfaqndeshja">Ndeshja</td>
    <td width="50" class="ndshfaqmenyra">Menyra</td>
    <td width="50" class="ndshfaqora">Ora</td>
    <td width="90" class="ndshfaqdata">Data</td>
    <td width="110" class="ndshfaqmod">X - Mod</td>
  </tr>
</table></center>


<?php
$i
=0;
while (
$i $num) {

$id=mysql_result($result,$i,"ID");
$ndeshja=mysql_result($result,$i,"ndeshja");
$ora=mysql_result($result,$i,"ora");
$data=mysql_result($result,$i,"data");
$menyra=mysql_result($result,$i,"menyra");
$sporti=mysql_result($result,$i,"sporti");
?>
<center>
<table width="598" border="0" class="ndeshjetabela">
  <tr>
    <td width="25" class="ndshfaqid"><?php echo $id?></td>
    <td width="35" class="ndshfaqsporti"><img src="..<?php echo $sporti?>" width="13"></td>
    <td width="265" class="ndshfaqndeshja"><?php echo $ndeshja;  ?></td>
    <td width="50" class="ndshfaqmenyra"><?php echo $menyra;  ?></td>
    <td width="50" class="ndshfaqora"><?php echo $ora;  ?></td>
    <td width="90" class="ndshfaqdata"><?php echo $data;  ?></td>
    <?php 
include (BPATH_ADM 'includet/dbconfig.php');
include (
BPATH_ADM 'includet/dblidhja.php');
$query="SELECT * FROM `ndeshje` ORDER BY `ndeshje`.`ID`";
$result=mysql_query($query);
while (
$row mysql_fetch_array($result)){
$id $row['ID'];

}
?>
    <td width="110" class="ndshfaqmod"><a href="#" onClick="confirm_delete('modulet/ndeshje/fshij.php?fshij=true&id=<?php echo $id;?>');">Fshije</a> - <a href="link-for-edit-entry.php">Mod</a></td>
  </tr>
</table></center>
    
</div>
<?php
$i
++;
}
?>
</body>
</html>
- Delete page:

PHP Code:
<html>
<body>
<?php
include '/includet/variabla.php';
include (
BPATH_ADM 'includet/dbconfig.php');
include (
BPATH_ADM 'includet/dblidhja.php');
$sql "delete from ndeshje WHERE ID = '$_GET[id]'";
$result mysql_query($sql);

//print $sql;

if (!$result) {
   echo 
"<div align ='center' class='error'>Fshirja e ndeshjes deshtoi!";
   echo 
"<br>";
   echo 
"<br>";
   echo 
'<form><input type="button" class="buton" value="Kthehu Mbrapa" 
ONCLICK="history.go(-1)"></form>'
;
   } else {
    echo 
"<div align ='center' class='header2'>Ndeshja u fshi me sukses!";
   echo 
"<br>";
   echo 
"<br>";
   echo 
'<form><input type="button" class="buton" value="Kthehu Mbrapa" 
ONCLICK="history.go(-1)"></form>'
;
}
?>
</body>
</html>
But the problem is, that when i click on the link of the row that i want to delete, the delete scripts delete another row, the last one, ex:

I have three rows ordered by time,date:

id time date name
2 20:10 04.03.2010 name 2
1 20:20 04.03.2010 name 1
3 20:30 04.03.2010 name 3

When i click on id 2 for delete, the script deletes the last row, id 3 in my example. I dunno what i'm doing wrong :-[

Can you help me, Please?

Thank you in advance!
Kleidi is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-04-2010, 05:13 PM Re: Delete row don't work correctly
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
May set ID to lowercase.

Also dont $sql = "delete from ndeshje WHERE ID = '$_GET[id]'";

Use
PHP Code:
$sqlid $_GET['id'];
$sqlid mysql_real_escape_string($sqlid);

$sql "delete from ndeshje WHERE id = '$sqlid' LIMIT 1"

I doubt the below is the issue:
is the id column set to primary key? ( unlikely to be the cause)
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 03-04-2010 at 05:16 PM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 03-04-2010, 06:27 PM Re: Delete row don't work correctly
Experienced Talker

Posts: 32
Name: Kleidi
Trades: 0
Quote:
Originally Posted by lynxus View Post
May set ID to lowercase.

Also dont $sql = "delete from ndeshje WHERE ID = '$_GET[id]'";

Use
PHP Code:
$sqlid $_GET['id'];
$sqlid mysql_real_escape_string($sqlid);

$sql "delete from ndeshje WHERE id = '$sqlid' LIMIT 1"
I doubt the below is the issue:
is the id column set to primary key? ( unlikely to be the cause)
Thank you for the reply. I modified that string you told me but the problem persist.
My db looks like:
Code:
`ID` int(11) NOT NULL auto_increment,
  `sporti` text NOT NULL,
  `ndeshja` varchar(255) NOT NULL,
  `permalink` varchar(255) NOT NULL,
  `ora` text NOT NULL,
  `data` text NOT NULL,
  `kodi` text NOT NULL,
  `menyra` varchar(255) NOT NULL,
  PRIMARY KEY  (`ID`)
Do u have another idea? Thx again!
Kleidi is offline
Reply With Quote
View Public Profile
 
Old 03-04-2010, 06:34 PM Re: Delete row don't work correctly
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
try it with uppercase id then?

$sqlid = $_GET['id'];
$sqlid = mysql_real_escape_string($sqlid);

$sql = "delete from ndeshje WHERE ID = '$sqlid' LIMIT 1";



Other than that, I cant see anything glaringly obvious
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 03-04-2010, 06:37 PM Re: Delete row don't work correctly
Experienced Talker

Posts: 32
Name: Kleidi
Trades: 0
Didn't worked
Thank you anyway! Hope that someone can help me!
Kleidi is offline
Reply With Quote
View Public Profile
 
Old 03-04-2010, 06:39 PM Re: Delete row don't work correctly
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
your link definitely has the correct ID in it when you hover over it?
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 03-04-2010, 06:43 PM Re: Delete row don't work correctly
Experienced Talker

Posts: 32
Name: Kleidi
Trades: 0
My link does not appear when hover over it bcz i use java script for confirming dhe deletion. But, when it loads after confirmation, the link loads the last id instead of the id wanted and clicked for deletion.
Kleidi is offline
Reply With Quote
View Public Profile
 
Old 03-04-2010, 06:46 PM Re: Delete row don't work correctly
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Ahhh
Your

$result=mysql_query($query);
while (
$row = mysql_fetch_array($result)){
$id = $row['ID'];

}


Will ALWAYS be the last row in the table.
your whiling through your list and replacing $id each time, To by the timeit end it WILL be your last one
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 03-04-2010, 06:46 PM Re: Delete row don't work correctly
Experienced Talker

Posts: 32
Name: Kleidi
Trades: 0
I have just modified the script and removed the confirmation, what i see is that, wherever i will click i will delete only the last id on the list, in the example gived on first post:
If i click on id 2 i will delete id 3, if i click on 1 i will delete 3, and if i click on 3 i will detele 3 too ... something is going wrong at get id loop i think
Kleidi is offline
Reply With Quote
View Public Profile
 
Old 03-04-2010, 06:47 PM Re: Delete row don't work correctly
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Maybe:


PHP Code:
$result=mysql_query($query);
while (
$row mysql_fetch_array($result)){
$id $row['ID'];
echo 
' <tr> <td width="110" class="ndshfaqmod"><a href="#" onClick="confirm_delete(\'modulet/ndeshje/fshij.php?fshij=true&id='.$id.'\');">Fshije</a> - <a href="link-for-edit-entry.php">Mod</a></td></tr> ';

}
?> 

Edited to fix the bad escaped chars
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE



Last edited by lynxus; 03-04-2010 at 06:49 PM..
lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 03-04-2010, 06:50 PM Re: Delete row don't work correctly
Experienced Talker

Posts: 32
Name: Kleidi
Trades: 0
Quote:
Originally Posted by lynxus View Post
Ahhh
Your

$result=mysql_query($query);
while (
$row = mysql_fetch_array($result)){
$id = $row['ID'];

}


Will ALWAYS be the last row in the table.
your whiling through your list and replacing $id each time, To by the timeit end it WILL be your last one
Resolved ... thank you very very very very much!

It works great now thanks to you!

What i need now is a mode to update my db via a php/html form but i should open a new thread i suppose!
Kleidi is offline
Reply With Quote
View Public Profile
 
Old 03-04-2010, 06:51 PM Re: Delete row don't work correctly
Experienced Talker

Posts: 32
Name: Kleidi
Trades: 0
I did something else:

PHP Code:
 <?php
include (BPATH_ADM 'includet/dbconfig.php');
include (
BPATH_ADM 'includet/dblidhja.php');
$query="SELECT * FROM `ndeshje` ORDER BY `ndeshje`.`ora`";
?>
    <td width="110" class="ndshfaqmod"><a href="modulet/ndeshje/fshij.php?fshij=true&id=<?php echo $id;?>">Fshije</a> - <a href="link-for-edit-entry.php">Mod</a></td>
  </tr>
</table></center>
    
</div>
<?php
$result
=mysql_query($query);
while (
$row mysql_fetch_array($result)){
$id $row['ID'];

}
$i++;
}
?>
</body>
</html>
Anyway, got the idea and it is working now
Kleidi is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Delete row don't work correctly
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.35382 seconds with 12 queries