Updating a checked record

I have a table with checkboxes.  I want to select a record(s) then hit a delete button to delete the selected record(s).  The problem is when I check a box it doesn't update in the database.  How can I do this?

echo '<td><input type="checkbox" value="1" /></td>';

Open in new window

DS928Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Marco GasiConnect With a Mentor FreelancerCommented:
First, in your css put this rule:

a{
  color: /*a color visible both on white and on azure background*/ :-)
}

Open in new window


About the working page, I can't debug. I guess you don't receive any message and that you jut see the record is not deleted. Then you can plòace this in your delete.php

<?php
//this grants all issues will be noticed and printed oin the screen
error_reporting(E_ALL);
ini_set('display_errors', 'On');
if (isset($_POST['id'])
{
//this show us the content of the array $_POST
    echo "<pre>";
    var_dump($_POST);
    echo "</pre>";
    $id = $_POST['id'];
    $sql = "DELETE FROM table WHERE ReordId='$id' ";
    //this will execute the query and in case of error will show us the error message
    try{
        $conn->query($sql);
        echo "Record $id successfully deleted";
     }catch ( Exception $e  )
     {
         echo $e->getMessage();
     }
}

Open in new window

Tjis page will be blank without any style, but for the moment will be enough. Then you'll can add all styles and element of a page with a button to come back to your table. Otherwise, you'll can change the link and point it to the same page where the table is and put this code at the very top of this page. But we'll can ee this later: now, try to see what's going wrong, right? :-)
0
 
Marco GasiFreelancerCommented:
Hi DS928.

First, you always must give a name to your inputs to get them posted. In this case the name must have array notation since you have many checkboxes.
Second, you must give as value something can be ued to identify the record in the database, for instance RecordId

echo '<td><input type="checkbox" name="RecordIds[]" value="{$row['RecordId']}" /></td>';

Open in new window


Notice the {} around the query result value: they allow you to avoid to close quote and use concatenation:

echo '<td><input type="checkbox" name="RecordIds[]" value="' . $row['RecordId'] . '" /></td>';

Open in new window


In the receiving script, ypou'll check if isset $_POST['RecordIds'] (remember that checkboxes and radios are post if and only if they are checked)

if (isset($_POST['RecordIds'])
{
    foreach ($_POST['RecordIds'] as $RecId)
    {
        $sql = "DELETE FROM table WHERE ReordId='$RecId'";
        $conn->query($sql);
    }
}

Open in new window


This way all selected records will be deleted, so you might consider to put a confirmation page between your form and the actual deletion of the records.
0
 
DS928Author Commented:
Great!  Not sure where to stick the if statement....
if ($conn->connect_error) {
    			die("Connection failed: " . $conn->connect_error);
			} 

			$sql = "SELECT recordId, recordCust, recordSite, recordUser, recordPass, recordDateAdded FROM records";
			$result = $conn->query($sql);

			if ($result->num_rows > 0) {
    		// output data of each row
  			
			echo '<table class="CSSTableGenerator">';
				//echo "<thead>";//open teùable headers
				echo '<tr>
				<td>Record ID</td>
				<td>Customer ID</td>
				<td>Site</td>
				<td>User Name</td>
				<td>Password</td>
				<td>Date Added</td>
				<td>Select</td>
				</tr>';
				//echo "</thead>";//close headers row
				//echo "<tbody>";//open table body
    		while($row = $result->fetch_assoc()) {
				echo '<tr>';
  			if(!empty($row['recordId'])) {
      			echo '<td>' . $row['recordId'] . '</td>';
    			}
					else
					{
      			echo '<td>NA</td>';
					}
  			if(!empty($row['recordCust'])) {
      			echo '<td>' . $row['recordCust'] . '</td>';
    			}
					else
					{
      			echo '<td>NA</td>';
					}
  			if(!empty($row['recordSite'])) {
      			echo '<td>' . $row['recordSite'] . '</td>';
   	 			}
					else
					{
      			echo '<td>NA</td>';
					}
    		if(!empty($row['recordUser'])) {
      			echo '<td>' . $row['recordUser'] . '</td>';
   	 			}
					else
					{
      			echo '<td>NA</td>';
					}
			if(!empty($row['recordPass'])) {
      			echo '<td>' . $row['recordPass'] . '</td>';
   	 			}
					else
					{
      			echo '<td>NA</td>';
					}
			if(!empty($row['recordDateAdded'])) {
      			echo '<td>' . $row['recordDateAdded'] . '</td>';
   	 			}			
					else
					{
      			echo '<td>NA</td>';
					}
					
			if(!empty($row['recordAction'])) {
      			echo '<td>' . $row['recordAction'] . '</td>';
   	 			}			
					else
					{
      			 //echo '<td><input type="checkbox" value="1" /></td>';
				 echo '<td><input type="checkbox" name="RecordIds[]" value="' . $row['RecordId'] . '" /></td>';
					}
				echo '</tr>'; //closing row
				}
  			//echo '</tbody>';
  			echo '</table>';
			
	} else {
    echo "0 results";
	}
	$conn->close();

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Marco GasiFreelancerCommented:
In the script your form points to: okay, you have not a form, uh? And where are the links to pst data? I sugget to leave the checkboxes and use simple links: you're still learning and you have a lot to learn, o it's better keeping it a simple as possible. Later you'll can manage more records at once. o I remove checkboxes and I show you what I meant in the other thread.
I also don't understan what is RecordActions: it doesn't make sense treating actions as a table column. And I don't understand you're not using <thead> and <th> to print table headers: that's the right way to do it.

if ($conn->connect_error) {
    			die("Connection failed: " . $conn->connect_error);
			} 

			$sql = "SELECT recordId, recordCust, recordSite, recordUser, recordPass, recordDateAdded FROM records";
			$result = $conn->query($sql);

			if ($result->num_rows > 0) {
    		// output data of each row
  			
			echo '<form method="post">';
			echo '<table class="CSSTableGenerator">';
				//echo "<thead>";//open teùable headers
				echo '<tr>
				<td>Record ID</td>
				<td>Customer ID</td>
				<td>Site</td>
				<td>User Name</td>
				<td>Password</td>
				<td>Date Added</td>
				<td>Select</td>
				</tr>';
				//echo "</thead>";//close headers row
				//echo "<tbody>";//open table body
    		while($row = $result->fetch_assoc()) {
				echo '<tr>';
  			if(!empty($row['recordId'])) {
      			echo '<td>' . $row['recordId'] . '</td>';
    			}
					else
					{
      			echo '<td>NA</td>';
					}
  			if(!empty($row['recordCust'])) {
      			echo '<td>' . $row['recordCust'] . '</td>';
    			}
					else
					{
      			echo '<td>NA</td>';
					}
  			if(!empty($row['recordSite'])) {
      			echo '<td>' . $row['recordSite'] . '</td>';
   	 			}
					else
					{
      			echo '<td>NA</td>';
					}
    		if(!empty($row['recordUser'])) {
      			echo '<td>' . $row['recordUser'] . '</td>';
   	 			}
					else
					{
      			echo '<td>NA</td>';
					}
			if(!empty($row['recordPass'])) {
      			echo '<td>' . $row['recordPass'] . '</td>';
   	 			}
					else
					{
      			echo '<td>NA</td>';
					}
			if(!empty($row['recordDateAdded'])) {
      			echo '<td>' . $row['recordDateAdded'] . '</td>';
   	 			}			
					else
					{
      			echo '<td>NA</td>';
					}
				echo '<td><a href="delete.php?id='. $row['recordId'] .'"> Delete </a></td>';
				echo '</tr>'; //closing row
				}
  			//echo '</tbody>';
  			echo '</table>';
			echo '</form>';
	} else {
    echo "0 results";
	}
	$conn->close();

Open in new window


Then in delete.php you put the code below:
if (isset($_POST['id'])
{
    $id = $_POST['id'];
    $sql = "DELETE FROM table WHERE ReordId='$id' ";
    $conn->query($sql);
}

Open in new window

0
 
DS928Author Commented:
Thank you said.  I am a beginner :)  So with that said, I am still having difficulty.

Here is the page.  It looks great with your help, However; I need it to function.

http://www.mediascrubber.com/records.php
0
 
Marco GasiFreelancerCommented:
Oh, what a stupid, I'm uising $_POST array but I suggested to use links which use a querystring! In delete.php, replace all occurrences of $_POST with $_GET.
We'll do this just for the moment: when you do write operation on the database, you never should use GET method for security reasons.
So, when you'll have see the code work, you'll can delete the form and put a form in each action column:
//The last column of your table, Actions
echo "<td>
<form method='post' action='delete.php'>
<input type='hidden' name='id' value='$id' />
<input type='submit' value='Delete' /> 
</form>
</td>

Open in new window

0
 
DS928Author Commented:
Thank you.
0
All Courses

From novice to tech pro — start learning today.