Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Delete row don't work correctly

    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!

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Your problem is here

    Code:
    while ($row = mysql_fetch_array($result)){
    $id = $row['ID'];
    mysql_fetch_array only returns numeric keys for the array. What you want to use is mysql_fetch_assoc then you do $row['ID'].

    If you do a print on $id you will see that it is empty.

    Also with the line below you should google sql injection
    Code:
    $sql = "delete from ndeshje WHERE ID = '$_GET[id]'";

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by skywalker2208 View Post
    Your problem is here

    Code:
    while ($row = mysql_fetch_array($result)){
    $id = $row['ID'];
    mysql_fetch_array only returns numeric keys for the array. What you want to use is mysql_fetch_assoc then you do $row['ID'].

    If you do a print on $id you will see that it is empty.

    Also with the line below you should google sql injection
    Code:
    $sql = "delete from ndeshje WHERE ID = '$_GET[id]'";
    Thank you for your reply.
    I'm new on php/mysql, can u help me with the complete string for what you said about mysql_fetch_assoc, PLEASE?
    About sql injection, i'm still reading and learning, anyway, can u give me any string mode example for my case on protection from sql injections, PLEASE?

    Thank you again for your reply!

  • #4
    New Coder
    Join Date
    Oct 2008
    Posts
    26
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Problem resolved. I moved
    PHP Code:
    $result=mysql_query($query);
    while (
    $row mysql_fetch_array($result)){
    $id $row['ID'];


    at the end of the page and this resolved my problem. Seems that this query, replaced all the id's in delete query page with the last id on the list.
    Now it works great. Thank you for your reply anyway

  • #5
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by skywalker2208 View Post
    mysql_fetch_array only returns numeric keys for the array.
    Incorrect. http://us2.php.net/mysql_fetch_array
    Are you a Help Vampire?

  • #6
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Quote Originally Posted by tomws View Post
    Thanks for the heads up. I guess I never read the whole description on php.net because it books it has always said use this for numerical keys and use the assoc one for associative keys.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •