[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Updating a checked record

Posted on 2015-01-26
7
Medium Priority
?
56 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 31

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 31

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 31

Accepted Solution

by:
Marco Gasi earned 2000 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 31

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

656 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