Jump to content

MySQL delete problem


LostSole

Recommended Posts

Hi Guys, i have been using AJAX to great sucess but i think I have a simple error in my PHP or SQL statement, the query executes fine but the mysql_affected_rows() statement returns 0. The row definately exists in the DB and ive checked the name for consistency, any ideas as to why this wont work? Whats especially wierd is that the deleting of sub categories works fine, but not parent categories.

 

 

Values are sent to the PHP (SQL file) from this Javascript Function:

function deleteCategory(catName, parentOrSub) {
// are you sure you want to delete category
var msg = "Are you sure you want to delete?";
if ( confirm(msg) ) {

var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxResultDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var acctID =  "<?= $acctID ?>";
	var queryString = "?catName=" + catName + "&parentOrSub=" + parentOrSub + "&acctID=" + acctID;
	ajaxRequest.open("GET","AJAXdeleteCategory.php" + queryString,true);
	ajaxRequest.send(null); 
	/*document.getElementById(catName).innerHTML = "";*/
// remove row or redraw table
}
}

 

 

 

 

PHP (SQL file) called: AJAXdeleteCategory.php

<?php

$DBConnect = @mysql_connect("localhost", "root", "");
if (!$DBConnect){
die("<p>The database server is not available.</p>");
}
//echo "<p>Successfully connected to the database server.</p>";
$DBSelect = @mysql_select_db("hh_bud_sys");
if (!$DBSelect){
die("<p>The database is not available.</p>");
}
//echo "<p>Successfully opened the database.</p>";

// Retrieve data from Query String
$catName   = $_GET['catName'];
$parentOrSub  = $_GET['parentOrSub'];
$acctID       = $_GET['acctID'];

// Escape User Input to help prevent SQL Injection
$catName = mysql_real_escape_string($catName);
$parentOrSub = mysql_real_escape_string($parentOrSub);
$acctID = mysql_real_escape_string($acctID);

// Build query
if (parentOrSub == 1) { // if it is a parent category
$query = "DELETE FROM parentgroup WHERE Name = '$catName' AND AcctID = '$acctID'";
}
else { // if it is a sub category
$query = "DELETE FROM subgroup WHERE Name = '$catName' AND AcctID = '$acctID'";	
}

if (mysql_query($query)) {
$affected = mysql_affected_rows();
echo "affected rows: $affected <br>";
  	if ($affected == 1) { 
	echo "<h5>successfully deleted $affected category!</h5>";
}
else {
echo "<h5>Not deleted $catName $parentOrSub</h5>";
}
}
?>

 

Any help much appretiated, and if more info is required, please feel free to ask!

 

 

Link to comment
https://forums.phpfreaks.com/topic/92726-mysql-delete-problem/
Share on other sites

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.