Delete data (row) in databse using delete icon

Discussion in 'PHP' started by anisFarim, Aug 17, 2010.

  1. #1
    i want delete the row with delete icon clicked. Below is my coding, but it doesn't work. anyone can help me.



    <?php
    $db = mysql_connect("localhost","root")
    or die ("Could Not Connect to Server" . mysql_error());

    /* select database */
    mysql_select_db("ppk4a", $db)
    or die ("Could Not Connect to Server" . mysql_error());

    $query = "SELECT * FROM aktiviti";
    $result = mysql_query($query);

    echo "<center><h3>Senarai Aktiviti</h3><br><table border=1><tr>";
    echo "<td><div align='center'>Nama Aktiviti</div></td>";
    echo "<td><div align='center'>Tarikh</div></td>";
    echo "<td><div align='center'>Masa</div></td>";
    echo "<td><div align='center'>Proses</div></td>";
    echo "</tr>";

    if ($delete){
    $delete = "DELETE FROM aktiviti WHERE nama_akt = $namaAktiviti";
    echo("<SCRIPT LANGUAGE='JavaScript'>
    window.alert('Aktiviti $namaAktiviti sudah dibuang')
    </SCRIPT>"); }

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    echo "<td><div align='left'><a href='$fileName' target='_self'>{$row['nama_akt']}</a></div></td>";
    echo "<td><div align='left'>{$row['tarikh']} - {$row['tarikhLast']}</div></td>";
    echo "<td><div align='left'>{$row['waktu']} - {$row['waktuLast']}</div></td>";
    echo "<td><div align='center'><img src=images/delete_icon.jpg width='16' height='16' STYLE='cursor:hand' name='delete' id='delete'></div></td>";
    echo "</tr>";
    }
    echo "</table></center>";
    echo "<br>";

    mysql_query($query) or die('Error, query failed');
    mysql_close($db);
    ?>
     
    anisFarim, Aug 17, 2010 IP
  2. ZeeshanButt

    ZeeshanButt Well-Known Member

    Messages:
    307
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Doy you get any error when running above code or what?
     
    ZeeshanButt, Aug 17, 2010 IP
  3. anisFarim

    anisFarim Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    not get any error. but when i click delete icon, the data not delete.
     
    anisFarim, Aug 17, 2010 IP
  4. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #4
    It looks like you may have had your script confused with ajax, you didn't assign an action to the image. The easiest way is to turn the image into a link with a $delete variable. Also, you defined the query $delete but didn't actually query the server.

    Here's an example, it's not perfect but is a start.

    
    <?php 
    $db = mysql_connect("localhost","root") 
    or die ("Could Not Connect to Server" . mysql_error());
    
    /* select database */
    mysql_select_db("ppk4a", $db)
    or die ("Could Not Connect to Server" . mysql_error());
    
    //This should be before the SELECT * query
    if (isset($_GET['delete'])){
    $namaAktiviti=mysql_real_escape_string($_GET['delete']);
    $query = "DELETE FROM aktiviti WHERE nama_akt ='$namaAktiviti'";
    mysql_query($query);
    echo("<SCRIPT LANGUAGE='JavaScript'>
    window.alert('Aktiviti $namaAktiviti sudah dibuang')
    </SCRIPT>"); }
    
    $query = "SELECT * FROM aktiviti";
    $result = mysql_query($query);
    
    echo "<center><h3>Senarai Aktiviti</h3><br><table border=1><tr>";
    echo "<td><div align='center'>Nama Aktiviti</div></td>";
    echo "<td><div align='center'>Tarikh</div></td>";
    echo "<td><div align='center'>Masa</div></td>";
    echo "<td><div align='center'>Proses</div></td>";
    echo "</tr>";
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    echo "<td><div align='left'><a href='$fileName' target='_self'>{$row['nama_akt']}</a></div></td>";
    echo "<td><div align='left'>{$row['tarikh']} - {$row['tarikhLast']}</div></td>";
    echo "<td><div align='left'>{$row['waktu']} - {$row['waktuLast']}</div></td>";
    echo "<td><div align='center'><a href='".$_SERVER['REQUEST_URI']."?delete={$row['nama_akt']}' title='Delete {$row['nama_akt']}' ><img src=images/delete_icon.jpg width='16' height='16' name='delete' id='delete'></a></div></td>";
    //didn't know the link for the page so just used $_SERVER['REQUEST_URI']
    echo "</tr>";
    }
    echo "</table></center>";
    echo "<br>";
    
    //mysql_query($query) or die('Error, query failed'); Not sure why this is here
    mysql_close($db);
    ?>
    
    PHP:
    Hope that helps,
    Cheers!
     
    Last edited: Aug 17, 2010
    Narrator, Aug 17, 2010 IP
  5. anisFarim

    anisFarim Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks, . It WORK. Thank you very much. :)
     
    anisFarim, Aug 17, 2010 IP
  6. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Now that will work as long as $namaAktiviti doesn't have special characters you can use urlencode() to fix it, but a better method would be to use the row id for $delete, that way the value should always be numeric.
     
    Narrator, Aug 17, 2010 IP
  7. anisFarim

    anisFarim Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ooo ok. but i use varchar for $namaAktiviti. it is ok right?

    but i have a mistake. before delete the row, i want do a confirmation from user. i try put

    echo("<SCRIPT LANGUAGE='JavaScript'>
    var x=window.confirm('Anda pasti aktiviti $namaAktiviti dibuang')
    </SCRIPT>");

    after

    if (isset($_GET['delete'])){

    but it not work. Can help me.
     
    Last edited: Aug 18, 2010
    anisFarim, Aug 18, 2010 IP
  8. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #8
    There's several ways to add a confirmation, the simplest would be to add an onclick confirm to the link:
    
    <?php 
    $db = mysql_connect("localhost","root") 
    or die ("Could Not Connect to Server" . mysql_error());
    
    /* select database */
    mysql_select_db("ppk4a", $db)
    or die ("Could Not Connect to Server" . mysql_error());
    
    //This should be before the SELECT * query
    if (isset($_GET['delete'])){
    $namaAktiviti=mysql_real_escape_string($_GET['delete']);
    $query = "DELETE FROM aktiviti WHERE nama_akt ='$namaAktiviti'";
    mysql_query($query);
    echo("<SCRIPT LANGUAGE='JavaScript'>
    window.alert('Aktiviti $namaAktiviti sudah dibuang')
    </SCRIPT>"); }
    
    $query = "SELECT * FROM aktiviti";
    $result = mysql_query($query);
    
    echo "<center><h3>Senarai Aktiviti</h3><br><table border=1><tr>";
    echo "<td><div align='center'>Nama Aktiviti</div></td>";
    echo "<td><div align='center'>Tarikh</div></td>";
    echo "<td><div align='center'>Masa</div></td>";
    echo "<td><div align='center'>Proses</div></td>";
    echo "</tr>";
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    echo "<td><div align='left'><a href='$fileName' target='_self'>{$row['nama_akt']}</a></div></td>";
    echo "<td><div align='left'>{$row['tarikh']} - {$row['tarikhLast']}</div></td>";
    echo "<td><div align='left'>{$row['waktu']} - {$row['waktuLast']}</div></td>";
    echo "<td><div align='center'><a href='".$_SERVER['REQUEST_URI']."?delete={$row['nama_akt']}' title='Delete {$row['nama_akt']}' onclick=\"return confirm('Anda pasti aktiviti {$row['nama_akt']} dibuang?');\" ><img src=images/delete_icon.jpg width='16' height='16' name='delete' id='delete'></a></div></td>";
    //didn't know the link for the page so just used $_SERVER['REQUEST_URI']
    echo "</tr>";
    }
    echo "</table></center>";
    echo "<br>";
    
    //mysql_query($query) or die('Error, query failed'); Not sure why this is here
    mysql_close($db);
    ?>
    
    PHP:
    Cheers!
     
    Narrator, Aug 18, 2010 IP
  9. anisFarim

    anisFarim Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Wow...its so easy. i don't know. thanks. sowy coz i'm late to say thanks. i have another assignment.
    thank you very much. :)
     
    anisFarim, Aug 20, 2010 IP