DS928
asked on
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>';
ASKER
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();
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.
Then in delete.php you put the code below:
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();
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);
}
ASKER
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
Here is the page. It looks great with your help, However; I need it to function.
http://www.mediascrubber.com/records.php
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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>
ASKER
Thank you.
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
Open in new window
Notice the {} around the query result value: they allow you to avoid to close quote and use concatenation:
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)
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.