Jump to content

[SOLVED] Looping through a data set deleting from a db problem


joesaddigh

Recommended Posts

Hi I have a piece of code which loops through some course dates and deleting the data but it does not seem to work. If i echo the delete query and use it phpmyadmin it works??

 

Can anybody see anything wrong with what I am doing.

 

The code is as follows:

 

The delete functionality

 

<?php
//Writing to the database so needs admin privileges 
require "../connectadmin.php";	

//Get the username	
echo $date = $_POST['Delete_CourseDates'];
echo $title = $_POST['CourseTitle'];


//Get the course dates
$query = "SELECT * FROM course WHERE CourseTitle = '$title'"; 

//Use this query below 		  
$result = mysql_query($query, $conn)
	or die ("Unable to perform query of course");

//Loop through the data set so that all of the course dates for the coursetitle are deleted
    while($row= mysql_fetch_array($result))
{	
	//Get the courseid 
	echo $id = $row['CourseId'];

	//Delete admin details from admin table
	echo $queryadmin = "DELETE FROM CourseDates
				   WHERE CourseStartDate = '$date'
				   AND CourseID =". $id;				   

	//Perform the query	  
	$admindetails = mysql_query($queryadmin, $conn)
		or die("Unable to delete course dates");
}

//Redirect user back to admin page form
//header("Location: course.php");
mysql_close($conn);
exit();			
?>

 

An example of its query output is as follows....

 

2010-06-14General English44

 

DELETE FROM CourseDates WHERE CourseStartDate = '2010-06-14' AND CourseID =44

 

DELETE FROM CourseDates WHERE CourseStartDate = '2010-06-14' AND CourseID =45

 

DELETE FROM CourseDates WHERE CourseStartDate = '2010-06-14' AND CourseID =53

 

DELETE FROM CourseDates WHERE CourseStartDate = '2010-06-14' AND CourseID =54

 

etc....

 

Thanks

 

:D

stick

or die("NO :" . mysql_error());

at the end of your query and see if you get an error

 

just about to say the same thing.

 

<?php

$admindetails = mysql_query($queryadmin, $conn)
		or die("MYSQL ERROR: " . mysql_errno() . " - " . mysql_error()); ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.