Page 1 of 1

Deleting from a mySQL database

Posted: Sun Nov 10, 2002 3:49 pm
by jonat8
Hi,

I think I'm confusing myself. What I want to do is to show the user all the records in a mySQL table and give them the option to delete the record by clicking on a field.

So far, I've got

Code: Select all

<?php
$dbconn = mysql_connect("localhost", "test", "test");
$result = mysql_select_db("jonat8", $dbconn);
if ( $result == false )
{
	echo mysql_error();
} else {
	$sql = "SELECT * from dalmedia";
	$result = mysql_query( $sql );
	if ( $result != false )
	{
		while ( $data = mysql_fetch_assoc( $result ) )
		{
			echo 'Track Details: '.
			$dataї'artist'].' (<a href="javascript:deleteMedia('.
			$dataї'filename'].')"><b>'.
			$dataї'trackname'].'</a></b>)<br>';
		}
	} else {
		echo mysql_error();
	}
}

?>
That pulls all the records from the database. But I'm not sure what the best way to go about querying the database to delete the entry would be.

My idea was a javascript that nests a PHP statement, if you see what I mean, to delete the entry from the database (criteria being the filename), or is there a more efficient way to delete the record? I'm confusing myself with it... The filename is passed to the script, i.e.

Code: Select all

<SCRIPT LANGUAGE=JavaScript>
function deleteMedia(filename)
I don't know whether this is the best way, I'm just experimenting.

Any suggestions greatly appreciated,

Thanks
Jonathan

Posted: Sun Nov 10, 2002 3:54 pm
by volka
My idea was a javascript that nests a PHP statement, if you see what I mean
not really but I fear the worst ;). Please read Sticky: Before Post Read: Frames, JavaScript, and PHP Overview first.

Does your table contain an unique field (a primary index, auto-increment field, ...)? If so use it as key in your DELETE-query's WHERE-clause

Search this board (esp. the Database-forum), there have been several thread on similar topics.

Posted: Sun Nov 10, 2002 4:06 pm
by jonat8
Well a flash of inspiration suddenly came to me...

Have 2 scripts, one that shows the records and you click on the filename to delete the record.

Then that calls the script that actually deletes the file, and passes the filename to it, so it can use it as the criteria for deleting the record.

Code: Select all

delete.php?filename=abc.wma
I don't have a primary key on the table but my logic is as the same file can only appear in the same folder once, that's good enough to use.

Not the most roundabout way to do it but hopefully the logic is there