I have use the following code to select multiple value for selected checkbox to delete it from database table but when I am print_r its only shows keys for it not showing value for id.
I have use this code to get value in array:-
<?php
echo "Hiiiiiiiiii";
include("conn.php");
$sql="select * from test ";
$res=mysql_query($sql) or die(mysql_error());
?>
<form name="form1" method="POST" action="">
<table width="578" border="1" align="center" id="menu">
<tr>
<th></th>
<th>id</th>
<th>Name</th>
<th>email</th>
<th>phno</th>
</tr>
<?php
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['emailid'];?></td>
<td><?php echo $row['phno'];?></td>
<?php
echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
?>
<?php
}
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";
if(isset($_POST['delete']))
{
if(count($_POST['checkbox']) !=0)
{
$array = array("checkbox" => $_POST['checkbox']);
print_r($array);
$ids = implode(',', $array);
echo "$ids";
$result = mysql_query("DELETE FROM test WHERE id IN ($ids)") or die(mysql_error());
}
}
// if successful redirect to delete_multiple.php
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=teach_delinquent.php\">";
echo "SUCCESS";
}
?>
$_POST['checkbox'] array will contain only checked boxes:
$ids = array();
foreach($_POST['checkbox'] as $val){
$ids[] = (int) $val;
}
$ids = implode(',', $ids);
I've done the foreach to make all id's INT for security purposes.
Quick fix of your question is as follows
Replace below line
$array = array("checkbox" => $_POST['checkbox']);
with
$array = $_POST['checkbox'];
or
$array = array_map('intval',$_POST['checkbox']); // if all values are integer ---for security
because $_POST['checkbox'] is already an array
Hope this will fix your problem
First of all you need to count how many checkboxes are there
$counter = sizeof($_POST['checkbox']);
The run a loop to access all of these values
for($i=0;$i<=$counter;$i++){
$checkbox_val = $_POST['checkbox'][$i];
}
That's all you need
Related
how can I get multiple ID while POST multiple selection option value to next form? I only get the first selection ID from array. Can you guys can suggest any ideas to me?
here is my code when select the value.
<tr>
<label>Auditor: </label>
<select class="form-control" name="auditor[]" multiple="multiple" >
<?php
$result = $db->query("SELECT * FROM auditor");
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row["auditor_name"].'">'.$row["auditor_name"].'</option>';
}
echo "</select>";
?>
</tr>
here is another code while POST to the next page.
$myselected = $_POST["auditor"];
if(count($myselected)>1){
$auditor = implode ("','",$myselected);
}else{
$auditor =$myselected;
}
$query10 = "SELECT * FROM auditor WHERE auditor_name IN ('$auditor') ";
$result10 = $db->query($query10);
$row10 = $result10->fetch_array();
?>
<form action="audit_action/audit_action.php" role="form" method="post" name="auditformdetails" onsubmit="return(validate());">
<table width='100%' border='0' class="table">
<tr>
<td colspan=6>Audit details</td>
<td colspan=6>Outlet details</td>
</tr>
<tr>
<td><b>Auditor:</b></td>
<td colspan='5'>
**<?php
echo'<input type="hidden" name="auditor_id" value="'.$row10["id"].'">';
foreach ($myselected as $auditor){
echo $auditor."<br>\n";
}
?>**
</td>
You can not compare string with mysql IN Clause. So, you have to connect each of your value with or condition in query as i written below.
$myselected = $_POST["auditor"];
$sql_cond = "";
if(count($myselected)>1){
foreach($myselected as $selected){
if($sql_cond != "")
$sql_cond.=" or auditor_name = ".$selected;
else
$sql_cond.=" auditor_name = ".$selected;
}
}else{
$auditor =$myselected;
}
$query10 = "SELECT * FROM auditor WHERE ".$sql_cond;
In this code I don't have any error messages but it doesn't delete the data. By the way the database has many records and the names of all the fields are correct.
<?php
if ($connect = mysqli_connect('localhost', 'root', 'adminpass', 'flip'))
{
$id = $_GET['id'];
$sql = "SELECT * FROM threads ORDER BY id DESC";
$query = mysqli_query($connect, $sql);
$num = mysqli_num_rows($query);
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST" >
<table border="1" width="400" cellpadding="0" cellspacing="0">
<tr>
<td>#</td>
<td>id</td>
<td>subject</td>
</tr>
<?php
while ($row = mysqli_fetch_array($query))
{
?>
<tr>
<td> <input type="checkbox" name="checkbox[]" value="<?php echo $row['id'] ?>"></td>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['topic'] ?></td>
</tr>
<?php
}
?>
<input type="submit" name="delete" value="delete" >
<?php
if (isset($delete))
{
for ($i = 0; $i < $num; $i++)
{
$del_id = $checkbox[$i];
$sql2 = "DELETE FROM threads WHERE id='$del_id'";
$query2 = mysqli_query($connect, $sql2);
}
if ($query2)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysqli_close($connect);
?>
</table>
</form>
What is the problem with it?
You can try this:
<?php
/* ESTABLISH CONNECTION */
$connect=mysqli_connect("localhost","root","adminpass","flip");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
if(isset($_POST['delete'])){ /* IF DELETE IS CLICKED */
$checkbox=$_POST['checkbox'];
$hiddencounter=mysqli_real_escape_string($connect,$_POST['hiddencounter']); /* PREVENT A BIT OF SQL INJECTION */
for($i=0;$i<$hiddencounter;$i++){ /* FOR LOOP BASED ON THE NUMROWS BELOW */
if(!empty($checkbox[$i])){ /* IF THE CHECKBOX IS TICKED */
$del_id=mysqli_real_escape_string($connect,$checkbox[$i]); /* PREVENT A BIT OF SQL INJECTION */
$sql2="DELETE FROM threads WHERE id='$del_id'";
$query2=mysqli_query($connect,$sql2); /* IMPLEMENT THE DELETE QUERY */
}
} /* END OF FOR LOOP */
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
} /* END OF ISSET DELETE */
?>
<form action="" method="POST" > <?php /* YOU CAN LEAVE THE ACTION BLANK, TO SUBMIT THE FORM ON THE PAGE ITSELF */ ?>
<table border="1" width="400" cellpadding="0" cellspacing="0">
<tr>
<td>#</td>
<td>id</td>
<td>subject</td>
</tr>
<?
$sql="SELECT * FROM threads ORDER BY id DESC";
$query=mysqli_query($connect,$sql);
$num=mysqli_num_rows($query);
/* SUBMIT THE NUMBER OF ROWS QUERIED THROUGH HIDDEN INPUT */
echo "<input type='hidden' name='hiddencounter' value='$num'>";
while($row=mysqli_fetch_array($query)){ /* FETCH DATA */
?>
<tr>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $row['id'] ?>"></td>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['topic'] ?></td>
</tr>
<?
} /* END OF WHILE LOOP $QUERY */
?>
<input type="submit" name="delete" value="delete" >
<?php
mysqli_close($connect);
?>
</table>
</form>
Explanation why your code did not work:
Your isset($delete): where does $delete variable came from?
Your for loop doesn't determine if the checkboxes submitted has values or not.
Your <meta> redirect. It redirects to delete.php after the for loop, so that is the reason you don't see any errors. But I'm pretty sure you would see some errors if you remove the <meta> redirect.
Even if the delete is working, you wouldn't see the result right after because the delete query is after your fetching of data.
You have an error in your Variable:
if (isset($delete))
Should be:
if (isset($_POST['$delete']))
I don't see where $delete variable is ever set in your code. At the moment $delete is not set, therefor if(isset($delete)) is false.
To me it would appear you are trying to use Registered Globals. Or you just forgot to set $delete to one of your $_GET[] or $_POST[] variables.
"Warning
This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0."
PHP manual - Security Globals
Predefined Variables
i have a list of checkboxes, right now i am able to retrieve data at database from multiple selected checkbox, and display them in a table.. below this is the code..
But my problem is how to display similar output data just like in comparetable.php to a histogram..i tried to used google chart but it just display data of a histogram exactly from the database, not getting from multiple selected checkboxes. Thank you very much for your time...
history.php
<FORM NAME ="form1" METHOD ="POST" action="comparetable.php">
<table>
<tr>
<th></th>
<th>TITLE</th>
<th>ACTION</th>
</tr>
<?php
$query = "SELECT * FROM compareresult where idmember='$idmembersession'";
$sql_query = mysql_query($query) or die('Error 3 :'.mysql_error());
while($data = mysql_fetch_array($sql_query,MYSQL_ASSOC)){
$title = $data['subject'];
?>
<tr>
<td><input type="checkbox" name="selectedcheck[]"
value="<?php echo $title ?>"/></td>
<?php
echo "<td>$title</td>"
}
?>
</tr>
</table>
<INPUT TYPE = "Submit" Name = "submit1" VALUE = "COMPARE SELECTED"></form>
comparetable.php
<body>SUSTAINABILITY OF PERCENTAGE </br></br></br>
<table border='1'>
<tr>
<th>TITLE</th>
<th>PERCENTAGE RESULT</th>
</tr>
<?php
if(isset ($_POST["submit1"]))
{
$checkbox = isset($_POST['selectedcheck']) ? $_POST['selectedcheck'] : array();
foreach($checkbox as $title)
{
$query = "SELECT * FROM compareresult where subject='".$title."'";
$sql_query = mysql_query($query) or die('Error 3 :'.mysql_error());
while($data = mysql_fetch_array($sql_query))
{
$result = $data['result'];
echo "<tr>";
echo "<td>".$title."</td>";
echo "<td>".$result."</td>";
echo "</tr>";
}
}
}
?>
</table></body></html>
I am searching some records using two text boxes and then updating the selected records in database. i am able to see the value row id of the selected checkbox but when i want to get the value for updation in database it gives 0, i.e showing no record in array
Here is my code
if($_POST["search"])
{
$nitnumber = $_POST["nitnumber"];
$workno = $_POST["workno"];
$query = "select * from print where nit = $nitnumber and work = $work";
$result = mysql_query($query) or die ("<font color =red>NIT Number and/or Work Number is Missing</font>");
$count = mysql_num_rows($result);
if($count == 0)
echo "<font color=red>Record not found</font>";
else
{
while($record = mysql_fetch_assoc($result))
{
?>
<tr class="odd">
<td><div align="center"><?php echo $record['a']; ?></div></td>
<td><div align="center"><?php echo $record['b']; ?></div></td>
<td><div align="left"><?php echo $record['c']; ?></div></td>
<td> <div align="left">
<?php
enter code hereecho $record["d"];
?>
</td>
<td>
<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>">
<?php echo $record["id"];?>
</td>
<?php } } }?>
<tr>
<td colspan="5" align="right"> <input type="submit" value="Save" name="save"> </td>
</tr>
<?php
if ($_POST['save'])
{
$num_chkboxes=count($_POST['checkbox']);
for($i=0; $i<$num_chkboxes; $i++){
$complete = intval($checkbox[$i]);
echo $complete;
var_dump($complete);
echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
//$Result1 = mysql_query($updateSQL, $FamilyOrganizer) or die(mysql_error());
}
}
?>
<?php
if ($_POST['save'])
{
$checkbox1 = $_POST['chk1'];
$selected_checkbox = "";
foreach ($checkbox1 as $checkbox1)
{
$selected_checkbox .= $checkbox1 . ", ";
}
$selected_checkbox = substr($selected_checkbox, 0, -2);
$updateSQL = "" // your update query here
}
?>
<input type="checkbox" name="chk1[]" value=""> // take checkboxes like this
First, the problem started when you fill the values of the checkboxes:
<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>" />
you must gave them the value like
value="<?php echo $record['id'] ?>"
Second, you don't get the values of the checkboxes as you should:
for($i = 0; $i < count($_POST['checkbox']); $i++){
$complete = intval($_POST['checkbox'][$i]);
//echo $complete;
//var_dump($complete);
echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
}
Another thing you should take care about is the assigning of the elements ids:
id="checkbox[]"
you must give unique ids for each element:
id="checkbox-1"
id="checkbox-2"
I am using the code (pasted below) to delete the record from the table when it's selected by the checkbox. It is working, but it is not deleting the record from the table, it is only showing the echo "Records deleted Successfully.".
Please have a look at my code and suggest me some changes.
<?php
echo "Hiiiiiiiiii";
include("conn.php");
$sql="select * from test ";
$res=mysql_query($sql) or die(mysql_error());
?>
<form name="form1" method="POST" action="">
<table width="578" border="1" align="center" id="menu">
<tr>
<th></th>
<th>id</th>
<th>Name</th>
<th>email</th>
<th>phno</th>
</tr>
<?php
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['emailid'];?></td>
<td><?php echo $row['phno'];?></td>
<?php
echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
?>
<?php
}
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";
if(isset($_POST['delete']))
{
$delete_id = $_POST['checkbox'];
$id = count($delete_id );
if (count($id) > 0)
{
foreach ($delete_id as $id_d)
{
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if($delete)
{
echo $id." Records deleted Successfully.";
}
}
>?
Add error_reporting(E_ALL); to the top of your file. Now you will be able to see all errors. http://php.net/manual/en/function.error-reporting.php
Also, use var_dump() to check what is actually in your variables. When you run var_dump($_POST); you can clearly see what is actually in your post.
http://php.net/manual/en/function.var-dump.php
if(isset($_POST['delete'])){
$delete = false;
$ids = array();
foreach($_POST['checkbox'] as $val){
$ids[] = (int) $val;
}
$ids = implode("','", $ids);
$sql = "DELETE FROM `test` WHERE id IN ('".$ids."')";
$delete = mysql_query($sql);
$id = mysql_affected_rows();
if($delete){
echo $id." Records deleted Successfully.";
}
}
also your check-boxes should have:
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>">
Put the delete code before selecting the records, then only you can see the actual records available in the DB.
<?php
echo "Hiiiiiiiiii";
include("conn.php");
if(isset($_POST['delete']))
{
$delete_id = $_POST['checkbox'];
$id = count($delete_id );
if (count($id) > 0)
{
foreach ($delete_id as $id_d)
{
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if($delete)
{
echo $id." Records deleted Successfully.";
}
}
$sql="select * from test ";
$res=mysql_query($sql) or die(mysql_error());
?>
<form name="form1" method="POST" action="">
<table width="578" border="1" align="center" id="menu">
<tr>
<th></th>
<th>id</th>
<th>Name</th>
<th>email</th>
<th>phno</th>
</tr>
<?php
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['emailid'];?></td>
<td><?php echo $row['phno'];?></td>
<?php
echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
?>
<?php
}
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";
>?
<? echo $rows['id']; ?>
there is no $rows here remove the s put
<? echo $row['id']; ?>
You should name the checkboxes something like
name="checkbox[<?php echo $row['id'] ?>]"
And then when you're trying to delete
foreach ($_POST['checkbox'] as $id => $delete) {
// delete id
}
Your way of selecting an id by count is rather awkward, you're also only deleting 1 row, which is apparently non-existant because the count is not an id.
Also, the value of a checkbox is meant to be an indicator off the checkbox being ticked or not, it's not usually holding a value, the usual way is to make the name hold what the checkbox actually means.
There is a Typo in the code that outputs the id:
... value="<? echo $rows['id']; ?>">
It should be
... value="<? echo $row['id']; ?>">
Note that your code is also vulnerable to SQL-injection attacks. You should check if the ids in the POST-request are really integers.
you should move this code below of include("conn.php"); in your code
if (isset($_POST['delete'])) {
$delete_id = $_POST['checkbox'];
$id = count($delete_id);
if (count($id) > 0) {
foreach ($delete_id as $id_d) {
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if ($delete) {
echo $id . " Records deleted Successfully.";
}
}
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id'];?>"></td>
<td><?php echo $row['id'];?></td>
Need to change as below to work it perfectly:
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="**<?php** echo $rows['id']; ?>"></td>