View Single Post
  #1 (permalink)  
Old 07-06-09, 02:00 PM
playa4002 playa4002 is offline
Newbie Coder
 
Join Date: Feb 2008
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Deleteing from mysql table

Hi Im not sure if this should be in javascript or php, kinda same problem. I am working on a ajax script to auto-update requests (for radio) and I am having trouble getting them to delete my old method before this isn't working now any help would be greatly appreciated.

And this current one isnt working or Im doing something wrong lol heres the codes:

boo.php

Code:
<table width="675" border="1"> 
<tr><td>ID</td><td>Song</td><td>Artist</td><td>Requested by</td><td>IP</td><td>Delete</td></tr> 
<?php

// Configure connection settings

$db = '-';
$db_admin = '-';
$db_password = '-';
$tablename = 'request_song';

// Title

echo "Contents of the table:";

// Connect to DB

$sql = mysql_connect("localhost", $db_admin, $db_password)
or die(mysql_error());

mysql_select_db("$db", $sql);

// Fetch the data

$query = "SELECT * FROM request_song ORDER BY id ASC";
$result = mysql_query($query);

// Delete function
if(isset($_GET['delreq']))
{
 
   $query = mysql_query("DELETE FROM request_song WHERE id = '{$_GET['id']}'")or die('Error : ' . mysql_error());  
 
   header('Location: ' . $_SERVER['HTTP_REFERER']);
   exit;
}
// Return the results, loop through them and echo

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($_GET['del']) { 
$delete = $_GET['del']; 
$delque = "DELETE FROM request_song WHERE id='{$delete}'"; 
mysql_query($delque); 
} 
$id = $row['id']; 
echo "<tr><td>{$row['id']}</td><td>{$row['song']}</td><td>{$row['artist']}</td><td>{$row['name']}</td><td>{$row['ip']}</td><td><a href=\"javascript:delreq({$row['id']})\">Delete</a></td></tr>";
}
?>
ajax.js

Code:
// Customise those settings

var seconds = 10;
var divid = "timediv";
var url = "boo.php";

////////////////////////////////
//
// Refreshing the DIV
//
////////////////////////////////

function refreshdiv(){

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

var seconds;
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}


function delreq(id)
{
   if (confirm("Are you sure you want to delete"))
   {
      window.location.href = 'boo.php?delreq=' + id;
   }
}
and finally index.html
Code:
<script src="ajax.js"></script>

<strong>Current Requests(Every 10 Seconds):</strong>

<script type="text/javascript"><!--
refreshdiv();
// --></script>
<div id="timediv"></div>
Reply With Quote