Solved

Updating a checked record

Posted on 2015-01-26
7
49 Views
Last Modified: 2015-01-27
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

0
Comment
Question by:DS928
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40572058
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
 

Author Comment

by:DS928
ID: 40572104
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
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40572287
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:DS928
ID: 40573177
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
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 40573385
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
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40573405
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
 

Author Closing Comment

by:DS928
ID: 40573847
Thank you.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now