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); ?>
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!
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.
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.
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!
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.