i can't figure out how to delete my records in php. my page displays all the records from the database, and i want to place a checkbox beside each record and the function delete. please help me.
code is
php code

<?php

	//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
	//Select Database
mysql_select_db($dbname) or die(mysql_error());
	// Retrieve data from Query String


	//build query

$query="SELECT * FROM employee";
	//Execute query
$qry_result = mysql_query($query) or die(mysql_error());

	//Build Result String
$display_string = "<table border='2' cellpadding='3'>";
$display_string .= "<tr>";
$display_string .= "<th>First Name</th>";
$display_string .= "<th>Last Name</th>";
$display_string .= "<th>Qualification</th>";
$display_string .= "<th>Status</th>";
$display_string .= "<th>Delete</th>";
$display_string .= "</tr>";

	// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
	$display_string .= "<tr>";
	$display_string .= "<td>$row[Fname]</td>";
	$display_string .= "<td>$row[Lname]</td>";
	$display_string .= "<td>$row[Qualification]</td>";
	$display_string .= "<td>$row[Status]</td>";
	$display_string .= "<td><a href='remove.php'>DELETE</td>";
	$display_string .= "</tr>";
	
}

$display_string .= "</table>";
echo $display_string;
?>

my html with ajax is

<html>
<head>

<title>SM PORTAL WEBSITE</title>

<meta http-equiv="Content-Type" content="text/html;">
<script src="validation.js" type="text/javascript"></script>

<SCRIPT language=JavaScript>
function makeArray() {
     for (i = 0; i<makeArray.arguments.length; i++)
         this[i] = makeArray.arguments[i];
 }

function getFullYear(d) {
    var y = d.getYear();
    if (y < 1000) {y += 1900};
    return y;
}

//var zone = "EDT";
var days = new makeArray("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var months = new makeArray("January","February","March","April","May","June","July","August","September","October","November","December");

function format_time(t) {

    var Day = t.getDay();
    var Date = t.getDate();
    var Month = t.getMonth();
    var Year = t.getFullYear();

    timeString = "";
    timeString += days[Day];
    timeString += ", ";
    timeString += " ";
    timeString += months[Month];
    timeString += " ";
    timeString += Date;
    timeString += ", ";
    timeString += " ";
    timeString += Year;

   return timeString;
 }
</SCRIPT>
<link rel="stylesheet" href="styles.css" type="text/css">

<style type="text/css">
<!--
.style1 {
	font-size: 14px;
	font-weight: bold;
}
.style2 {font-size: 16px}
.style3 {font-size: 12px}
-->
</style>
</head>
<body bgcolor="#B49BCD" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" link="#747AC6" vlink="#B49BCD">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
  <tr> 
    <td valign="top" height="84" background="images/header_fill.jpg"><img src="images/smheaderlogo.jpg" width="321" height="84"></td>
  </tr>
  <tr> 
    <td valign="top" height="22" background="images/blue_fill.gif" align="right"> 
      <table width="220" border="0" cellspacing="0" cellpadding="0" height="22">
        <tr> 
          <td width="37"><img name="blue_crn" src="images/blue_crn.gif" width="37" height="22" border="0"></td>
          <td background="images/lt_blue_fill.gif" class="date" width="200"> 
            <script language=JavaScript>
			<!--
			d = new Date();
			document.write(format_time(d));
			// -->
		</script>

          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr> 
    <td valign="top"> 
      <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
        <tr> 
          <td width="140" bgcolor="#92A2E1" valign="top" align="center"> <br>
            <table width="80%" height="228" border="0" cellpadding="0" cellspacing="0">
              <tr> 
                <td class="links"> <p><a href="home.htm"><img src="images/square.gif" width="7" height="7" border="0"></a> HOME<br>
                    <br>
                    <a href="http://10.101.150.184/nagios/"><img src="images/square.gif" width="7" height="7" border="0"></a> NAGIOS<br>
                    <br>
                    <a href="http://10.101.150.184/cgi-bin/smokeping.cgi"><img src="images/square.gif" width="7" height="7" border="0"></a> SMOKE 
                    PING <br>
                    <br>
                    <a href="http://10.101.160.59:9675/account/login"><img src="images/square.gif" width="7" height="7" border="0"></a> SPICEWORKS<br>
                    <br>
                    <img src="images/square.gif" width="7" height="7"> HELP 
                    DESK <br>
                    <br>
                    <a href="humanresourcemanagement1.htm"><img src="images/square.gif" width="7" height="7" border="0"></a> HUMAN 
                    RESOURCE MANAGEMENT</p>
                  <p><img src="images/square.gif" width="7" height="7"> NETWORK 
                RESOURCE MANAGEMENT</p></td>
              </tr>
            </table>
          </td>
          <td valign="top" bgcolor="#FFFFFF"> 
            <table width="100%" border="0" cellspacing="0" cellpadding="30" height="100%">
              <tr> 
			  
			  

                <td height="370" valign="top" bgcolor="#FFFFFF" class="text"><form name="form2" method="post" action="adminpro.php">

                  <p class="style1">&nbsp;</p>
                  <p align="center" class="style1 style2">&nbsp;</p>
                  <p align="center">
                    <script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	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('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var age = document.getElementById('Fname').value;
	var wpm = document.getElementById('Lname').value;
	var queryString = "?Fname=" + Fname + "&Lname=" + Lname;
	ajaxRequest.open("GET", "searchajax.php" + queryString, true);
	ajaxRequest.send(null); 
}

//-->
                    </script>
                  </p>
                  <form name='myForm'>
                    <p align="center">First Name:</p>
  <p align="center">
    <input type='text' id='Fname' /> 
  </p>
  <p align="center">Last Name:</p>
  <p align="center">
    <input type='text' id='Lname' />
  </p>
  <p align="center">
    <input type='button' onclick='ajaxFunction()' value='Search' />
  </p>
</form>
<div id='ajaxDiv'>
  <div align="center">Your result will display here</div>
</div>

                  <p>&nbsp;   </p>
                  <p>
                    <label></label>
                  </p>
                </form>                
                  <p>&nbsp;</p></td>
              </tr>
            </table>
          </td>
        </tr>
        <tr> 
          <td width="140" bgcolor="#92A2E1" height="27">&nbsp;</td>
          <td align="right" height="27" bgcolor="#FFFFFF"><img name="footer_right" src="images/footer_right.gif" width="110" height="27" border="0"></td>
        </tr>
        <tr> 
          <td width="140" background="images/footer_fill.jpg" height="38">&nbsp;</td>
          <td align="right" height="38" background="images/footer_fill.jpg"> 
            <table width="100%" border="0" cellspacing="0" cellpadding="0" background="images/footer_fill.jpg">
              <tr> 
                <td valign="bottom" align="center" class="footer">Copyright &copy; 
                  SM Mart Inc. All Rights Reserved.</td>
                <td align="right" width="110"><img name="footer_right2" src="images/footer_right2.gif" width="110" height="38" border="0"></td>
              </tr>
            </table>
          </td>
        </tr>
        <tr> 
          <td width="140" height="10">&nbsp;</td>
          <td align="right" height="10">&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
Member Avatar for langsor

I'm assuming you have your Ajax working properly, so I'm not going to look over that section.

You will want to pass in the unique-primary-key with the rest of the data and use that in your remove.php function. I'm using the value Employee_ID for that value

$display_string .= "<td><a href="remove.php?eid={$row['Employee_ID']}">DELETE</a></td>";

In your remove.php you want t line like this

// get the passed in ID
$eid = $_GET['eid'];
// delete that record in the table by ID
mysql_query( "DELETE FROM `employee` WHERE Employee_ID='$eid'" );

A note on security...make sure you secure this script from malicious users because you are providing a way for all of your employee records to be wiped out if this gets into the wrong hands -- just a thought.

Member Avatar for langsor

Oh right, you said with a checkbox. Here's one way to do that.

the javascript

<head>
window.onload = init_delete;

function init_delete () {
  document.getElementsByTagName('html').item(0).onclick = function ( event ) {
    var action = ( typeof event == 'undefined' ) ? window.event : event;
    var target = ( typeof action.target == 'undefined' ) ? action.srcElement : action.target;
    if ( target.nodeName == '#text' ) { // Safari nodeType 3 work-around
      target = target.parentNode;
    }
    if ( /delete/.test( target.className ) ) {
      if ( target.checked ) {
        // send your Ajax request here
        alert( target.value ); // testing
      }
    }
  };
}
</head>

This way you can put a checkbox anywhere you like and if it has the class="delete" the value will be sent to the Ajax function when it gets checked. You might want to put a confirm() type measure in this to protect against accidental *clicking*

the PHP

$display_string .= "<td><input type="checkbox" class="delete" value="{$row['Employee_ID']}" /></td>";

Hope this helps

thank you very much for your reply. i will try the code now

hi whenever i insert this code

$display_string .= "<td><input type="checkbox" class="delete" value="{$row['empId']}"/></td>";

i get an error when i query the table and all its data

Parse error: syntax error, unexpected T_STRING
Member Avatar for langsor

You need to escape your nested double-quotes ...

Change this ...

$display_string .= "<td><input type="checkbox" class="delete" value="{$row['empId']}"/></td>";

... to this ...

$display_string .= "<td><input type=\"checkbox\" class=\"delete\" value=\"{$row['empId']}\"/></td>";

See if that works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.