Delete MySQL row with PHP and javascript

This page pulls information from multiple tables based on a variable it retrieved from a previous page. It pulls the information fine, but what it isn't doing is deleting like it's supposed to. I know the problem, just not how to fix it. I am using the userID field to join the tables since it's the only common field they share. With the code the way it is, if you view source on the page when it is executed, the deleteLicense(id) portion remains blank, meaning it never pulls the userID. If i remove the table joins it gives the ID, but doesn't give me the information I need. Just trying to figure out why it's doing that and if there is a workaround.
<?php
ob_start();
session_start();
require_once("../includes/config.php");
require_once("../includes/Sajax.php");
sajax_init();
sajax_export("buildTable", "deleteLicense");
sajax_handle_client_request();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Software License Entries</title>

</head>
<body>

<?php
function buildTable() {
	$result = mysql_query("SELECT * FROM users_software LEFT JOIN users ON users_software.userID = users.userID LEFT JOIN pcinfo ON users_software.userID = pcinfo.userID WHERE softwareID = '".$_GET['id']."'") or die(mysql_error());
	
	//row colors
	$normalcolor = "#EEEEEE";         
	$highlightcolor = "#FFFFFF";  

	//build table
	$html = '
	<h3 align="center">Users</h1>
	<table width=300 border=0 cellspacing=2 cellpadding=2 align=center>
		<tr>
			<td width=50%></td>
			<td></td>
			<td width=25></td>
		</tr>';

	if ($result) {
		while ($myrow = mysql_fetch_array($result)) {

			$html .= '<tr onMouseOver="this.style.backgroundColor=\''.$normalcolor.'\';" onMouseOut="this.style.backgroundColor=\''.$highlightcolor.'\';">'.
				'<td><a href="../users/details.php?id='.$myrow[userID].'" onclick="return hs.htmlExpand(this, { objectType: \'iframe\', width: 500 } )">'.$myrow[username].' </a></td>'.
				'<td>'.$myrow[pcName].'</td>'.
				'<td><a href="#"><div align=center><img src="../images/delete_row.png" border="0" id="delete'.$myrow[userID].'" onClick="deleteLicense('.$myrow[userID].')"></div></a></td>'.
			'</tr>';
		}
	} else {

		$html .= '
			<tr>
				<td>No Users Found</td>
				<td></td>
				<td></td>
			</tr>';
	}

	$html.= '</table>';

	return $html;
}

function deleteLicense($id) {
	mysql_query("DELETE FROM users_software WHERE userID='".$id."' AND softwareID='".$_GET['id']."'");
}
?>

<script type="text/javascript">
function refreshTable() {
	x_buildTable(updateTable);
	parent.refreshTable();
}

function updateTable(html) {
	document.getElementById('licensesTable').innerHTML = html;
}

function deleteLicense(id) {
	if(confirmDelete(id)) {
		var deleteID = 'delete' + id;
		var el = document.getElementById(deleteID);
		el.onclick = '';
		el.setAttribute('src', '../images/delete_off.png');
		//call delete function
		x_deleteLicense(id, null);
		refreshTable();
	}
}

function confirmDelete(id) {
	return confirm("Do you want to remove this user from this piece of software?")
}

<?php sajax_show_javascript(); ?>

</script>

<?php
echo '<div id="licensesTable">'.buildTable().'</div>';
#you must put this at the bottom of every page
echo "</td></tr></table></body>";
?>

Open in new window

bpbarberhelpAsked:
Who is Participating?
 
hieloCommented:
OK, let's try something else. Instead of:
SELECT *

LIST the fields that you ACTUALLY need. The * will give you all the fields for the tables involved. I suspect you have a field named userID on more than one of the tables involved and mostlikely the field you are getting in $myrow is from a table where the field does not have a value. So try something like:
$result = mysql_query("SELECT users_software.userID,users.username,pcinfo.pcName FROM users_software LEFT JOIN users ON users_software.userID = users.userID LEFT JOIN pcinfo ON users_software.userID = pcinfo.userID WHERE (pcinfo.userID IS NOT NULL) AND softwareID = '".$_GET['id']."'") or die(mysql_error());

Open in new window

0
 
hieloCommented:
change:
if ($result) {
            while ($myrow = mysql_fetch_array($result)) {
...


to:
if ($result) {
            while ($myrow = mysql_fetch_assoc($result)) {
                    echo "<pre>" .  print_r($myrow) . "</pre>";
...


Do you see a value for userID? Or do you have userId? (check case sensitivity)
0
 
MichaelT_Commented:
Try using $myrow['userID'] you need the quotes.
0
 
bpbarberhelpAuthor Commented:
Array ( [userID] => [softwareID] => 159 [username] => abivins [location] => SAV [usersOffice] => N/A [dualMonitor] => 0 [comments] => [dataPort] => [voicePort] => [localAdmin] => 0 [pcID] => [pcName] => [compID] => [pcUpdated] => [pcST] => [pcESC] => [pcOS] => )

No, I do not have a value for it. That's the problem. But I don't understand why.
0
 
bpbarberhelpAuthor Commented:
You nailed it man. As soon as you mentioned using the field from a table that had an empty value it hit me. I reversed the order of the query to make user_software last instead of pcinfo (which was the table with the blank entry). Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.