Hi all,
How to I using checkbox for to delete each row in table . Could you anyone help me?
Regards,
Hi all,
How to I using checkbox for to delete each row in table . Could you anyone help me?
Regards,
Wow. Do you have any idea as to the process you're going to use? Do you have existing code? No? How about making some up.
Have you read a tutorial on mySQL and manipulating the data in the database? There are tons out there. Read one. When you have questions, ask here.
~Brett
I'am still newbie about programmer. I don't know how to do it. Could anyone help me to make php code hot to delete each row using checkbok?
Well, you can find that any tutorial online should tell you how to update/delete/insert info in a mySQL database. But for a quick and dirty answer:
STEP 1: Connect to the database host, and select the database
<?php
$conn = mysql_connect('host', 'username', 'password');
// Replace "host", "username", "password" with your values
if(!$conn){ die(mysql_error()); } // If no connection, kill script displaying error
mysql_select_db('database_name', $conn);
// Select the database that we will be querying
STEP 2: Query the table for all results
$query = "SELECT *
FROM `table_name`";
$result = mysql_query($query) or die(mysql_error());
// Query the database, or kill script showing the mysql error
STEP 3: Create the table with checkboxes & item info
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
while($row = mysql_fetch_array($result))
{
// While there is information in the row, i.e. the row is not empty
echo '<input type="checkbox" name="delete[]" value="'.$row['id'].'"> '.$row['title'].'<br>';
// Output a checkbox, and then the title of the row. Each array element in $row must be
// present in your table. So there must be columns "title" and "id" in your table. Otherwise
// change them to what your database is using
}
echo '<input type="submit" name="submit" value="Delete">
</form>';
?>
NOTE: Each step past this should have the code placed above the code in step 1. I will give reference points for help.
STEP 4: Get row IDs into local array
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'Delete')
{
$delete_ids = $_POST['delete'];
}
// Step 1:
$conn = mysql_connect('host', 'username', 'password');
// Continue on with steps 2 & 3 below here...
?>
STEP 5: Create query to delete rows
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'Delete')
{
$delete_ids = $_POST['delete'];
$i=0;
$query = "DELETE FROM `table_name`";
foreach($delte_ids as $key=>$id)
{
if($i==0)
{
$query .= "WHERE id='".$id."'";
}
else
{
$query .= "OR id='".$id."'";
}
$i++;
}
}
// Step 1:
$conn = mysql_connect('host', 'username', 'password');
// Continue on with steps 2 & 3 below here...
?>
STEP 6: Run query deleting rows
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'Delete')
{
$delete_ids = $_POST['delete'];
$i=0;
$query = "DELETE FROM `table_name`";
foreach($delte_ids as $id)
{
if($i==0)
{
$query .= "WHERE id='".$id."'";
}
else
{
$query .= "OR id='".$id."'";
}
$i++;
}
$conn = mysql_connect('host', 'username', 'password');
if(!$conn){ die(mysql_error()); }
mysql_select_db('database_name', $conn);
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_affected_rows();
if($rows>0)
{
echo 'Successfully deleted '.$rows.' rows.';
}
else
{
echo 'There was a problem. No rows were deleted.';
}
}
// Step 1:
$conn = mysql_connect('host', 'username', 'password');
// Continue on with steps 2 & 3 below here...
?>
SO the entire code would be:
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'Delete')
{
$delete_ids = $_POST['delete'];
$i=0;
$query = "DELETE FROM `table_name`";
foreach($delte_ids as $id)
{
if($i==0)
{
$query .= "WHERE id='".$id."'";
}
else
{
$query .= "OR id='".$id."'";
}
$i++;
}
$conn = mysql_connect('host', 'username', 'password');
if(!$conn){ die(mysql_error()); }
mysql_select_db('database_name', $conn);
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_affected_rows();
if($rows>0)
{
echo 'Successfully deleted '.$rows.' rows.';
}
else
{
echo 'There was a problem. No rows were deleted.';
}
}
<?php
$conn = mysql_connect('host', 'username', 'password');
// Replace "host", "username", "password" with your values
if(!$conn){ die(mysql_error()); } // If no connection, kill script displaying error
mysql_select_db('database_name', $conn);
// Select the database that we will be querying
$query = "SELECT *
FROM `table_name`";
$result = mysql_query($query) or die(mysql_error());
// Query the database, or kill script showing the mysql error
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
while($row = mysql_fetch_array($result))
{
// While there is information in the row, i.e. the row is not empty
echo '<input type="checkbox" name="delete[]" value="'.$row['id'].'"> '.$row['title'].'<br>';
// Output a checkbox, and then the title of the row. Each array element in $row must be
// present in your table. So there must be columns "title" and "id" in your table. Otherwise
// change them to what your database is using
}
echo '<input type="submit" name="submit" value="Delete">
</form>';
?>