Store and delete data with checkboxes in mysql - php

I am having a bit of a problem, i am trying to store data with checkboxes, but i cant seem to figur it out. Right now i have made a script that can show multiple checkboxes all with the same name. So my question is now, how do i make the page where the data is sended to making them seperated? like:
<input type='checkbox' checked name='cpu-link[]' value='1' />
<input type='checkbox' checked name='cpu-link[]' value='2' />
<input type='checkbox' checked name='cpu-link[]' value='3' />
how can i get it stored for each checkbox in a new row in the mysql database?
so it would be like:
Data 1
Data 2
Data 3
And then i have a related qestion, how do i make so that the checkboxes that is not checked will get removed if they already is in the database.
so if it is like this:
<input type='checkbox' checked name='cpu-link[]' value='1' />
<input type='checkbox' name='cpu-link[]' value='2' />
<input type='checkbox' name='cpu-link[]' value='3' />
Then the row with the id 2 and 3 will get removed but the row with id 1 will get created or just stay if it already is in the database.
It is all a kind of a link between 2 tables.
I have a table with pc's in, and a table with hardware in.
then i have the table with the links in, so if i choose a hardware that needs to bee in a machine, it will create a row in the link table so that when i display the pc, it shows with that hardware oppotionity.
sorry for my bad english.
Thank You in Advance

$all_cpu_links = array(); // here is your array with all possible values of cpu-link
$previously_checked = array(); // array of values that are already in your database
$to_be_removed = array();
$to_be_inserted = array();
// It is better to use here here a prepared statement by the way
foreach ($_POST['cpu-link'] as $cpu_link)
{
if (!in_array($cpu_link, $previously_checked))
{
// validate $cpu_link here
mysql_query("INSERT INTO table (`cpu_link`) VALUES (".$cpu_link.");");
}
}
foreach ($all_cpu_links as $cpu_link)
{
if (!in_array($cpu_link, $_POST['cpu-link']) && in_array($cpu_link, $previously_checked))
$to_be_removed[] = $cpu_link;
}
mysql_query("DELETE FROM table WHERE `cpu_links` IN (".implode(' ,', $to_be_removed).");");

Related

Update selected rows with value in table by using a checkbox for each row

I have tried searched, but I am missing somthing or not phrasing myself correctly I guess.
php, sql and Codeigniter.
What I have is a list of reports in a database.
Theese have a column that states if they are "active" 1 or 0.
What I want to do is add a checkbox on the list that prints the rows in my view as that I can select and then when I have selected the rows I want to be able to press a button and by sending form data change the selected rows in the database to a 1 or 0.
example of how I am thinking of presenting it:
echo "<form action='' method='POST'><table>";
foreach($reports->result() as $row)
{
echo "<tr>";
echo "<td><input type="checkbox" name=""></td>;
echo "<td>".$row->name."</td>";
echo "<td>".$row->time."</td>";
echo "<td>".$row->spot."</td>";
echo "<td>".$row->priority."</td>";
echo "</tr>";
}
echo "<tr><td colspan='5'><input type='submit'></td></tr>";
echo "</table></form>";
What would be the best is if I could get the checkboxes I select into a array for example that would be lovely.
I have tried with array_push, but I could not find a way to select only the ones I selected to send. Is there some way to do this that I dont know of?
I have found a way to make the selected checkboxes into an array by adding id[] to all checkboxes name like this this:
form_checkbox('id[]',$row->id, FALSE);
I loop out alot of these and might get this for example:
<input type="checkbox" name="id[]" value="12340">
<input type="checkbox" name="id[]" value="12353">
<input type="checkbox" name="id[]" value="12367">
<input type="checkbox" name="id[]" value="12389">
<input type="checkbox" name="id[]" value="12391">
Then in my controller I added this where my form takes action to and loops through the ones that I have checked and take action on them.
$arr = array();
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $selected) {
$this->db->where('id', $selected);
$this->db->update('rapporter' , $data);
}
}else{}
This is working great for me. Now I only update the rows I have checked!
If someone have a more efficient way of doing this I am all ears!

Checkbox with quantity field to array

I'm trying to create something of a packing list that will export into an array that I can print out.
The basic code is as follows:
<form method='post' id='items' action='items.php'>
<input type="checkbox" name='list[]' value='sunglasses' />sunglasses
<input type='checkbox' name='list[]' value='sunblock' />sunblock
<input type='checkbox' name='list[]' value='socks' />Socks<input type="text" name="socks_qty">
<?php
if (isset($_POST['list'])) {
print_r($_POST['list']);
}
?>
Only items that are checked will appear in the array.
I would like to add functionality that will allow me to specify a quantity of SOME items (ie. socks)with a text input field next to the checkbox so that I can extract the quantity from the array somehow. I'm not sure the best way to approach this as not all items need to have a quantity and I'm not sure how to tie the quantity to a specific item.
The output might be something like 'list[socks][3]'.
What would be the best way to approach something like this?
You can use the item names as keys instead of values, then another key after that to indicate yes/no vs. quantity.
<input type="checkbox" name='list[sunglasses][checked]' value='1' />sunglasses
<input type='checkbox' name='list[sunblock][checked]' value='1' />sunblock
<input type='checkbox' name='list[socks][checked]' value='1' />Socks
<input type="text" name="list[socks][qty]">
Remember with checkboxes that you won't get anything in $_POST unless it was checked, so if you're going to use this to determine what you still need, you'll have to set some default false values for all the various keys and then update with the values from $_POST for the things that were checked.
Another possibility is to forgo the checkbox for things that you want a quantity of (unless the quantity is optional) because with a quantity > 0, the checkbox value is kind of redundant. Unless you want the ability to express that you have a certain number of socks, but you aren't bringing them. ;-)
<input type="checkbox" name='list[sunglasses]' value='1' />sunglasses
<input type='checkbox' name='list[sunblock]' value='1' />sunblock
socks<input type="number" name="list[socks]">

Checkbox Insert / Delete From Database

A little about my code:
I have code that dynamically displays emails with a checkbox next to each email.
<?
foreach($er as $row){ ?>
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="check" checked="checked">
<?
echo $row[email]."<br><br>";
echo "<input name='emailID' id='emailID' type='hidden' value='".$row[emailID]."' />";
} $emailquery->execute(); ?>
I can't seem to come up with a way that deletes the emailID of each email from a specific database table when you uncheck the checkbox. When you re-check the checkbox, I want to insert it back into the database table.
The emails won't go away, because they are stored in a completely different table than the one I want to insert/remove it from.
I know this is kind of a full question, so I will answer any questions you may have. Thank you in advance for all your help!
first, change your input to
<input name="emailcheckbox[]" value="<?php echo htmlspecialchars($row[email]);?>" ...
so, after you post this form back to server you will have
$_POST['emailcheckbox'] == array('checkedemail1', 'checkedemail2'...)
so you will need to delete all e-mails from your table and insert emails from this array, with this you will delete unchecked ones and save only checked ones
You can do this way:
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="[tablename][email]" checked="checked">
Insert Page:
<?
foreach($_POST['emailcheckbox'] as $item)
{
$query = "INSERT INTO ".$item[0]." VALUES(".$item[1].")";
.....
}....
?>

Sending selected checkboxes' values to PHP

I'm building a site where the admins need to be able to select multiple database entries (clients) via checkboxes. Once the relevant clients are selected, they can click on a button that redirects them to a page where an email can now be composed for only the selected clients. My checkboxes are set up as below:
<input name = 'checked_residents' value='1' id = '1' type='checkbox' />
<input name = 'checked_residents' value='2' id = '2' type='checkbox' />
<input name = 'checked_residents' value='3' id = '3' type='checkbox' />
I then have a button (id = 'mail_selected') which, when clicked, is able to construct an array with all the id's for the selected checkboxes. See code below:
$('#mail_selected').click(function(event) {
$(':checkbox[name="checked_residents"]:checked');
var selectedID = [];
$(':checkbox[name="checked_residents"]:checked').each (function () {
var tempStr = this.id;
selectedID.push(tempStr);
});
});
My problem is that I need to send the selectedID array to my php file "mailer_client.php", but I need to redirect to that page immediately, and populate my "Email To" input field with all emails from the respective ID's.
I realize that it may be difficult to understand what exactly I'm trying to do...I'm unsure myself. Please ask if anything regarding the problem is unclear.
If you change the name of your checkbox to be like an array, like this:
<input name = 'checked_residents[]' value='1' id = '1' type='checkbox' />
<input name = 'checked_residents[]' value='2' id = '2' type='checkbox' />
<input name = 'checked_residents[]' value='3' id = '3' type='checkbox' />
then you will receive them as an array in PHP in $_REQUEST['checked_residents'] and won't need your current code to construct the array yourself and send it.
You could then fill emails as follows:
foreach ($_REQUEST['checked_residents'] as $val) {
$email_to .= get_email_for_id($val) . ' , ';
}

Checkbox Deleting - If statement inside For loop deletes wrong entries

I have a page which displays my database information. Through each loop, there is a checkbox printed that is to delete the associated entry if checked and submitted. The checkbox names are 'delete[]' and there is the hidden value which contains the row id is named 'id[]'.
This is the relevant part of my form:
<tr><td valign='top'>
<label class='amarillo med' style='color:#C00;'>Delete Section </label>
<input type='checkbox' name='delete[]' />
<input type='hidden' name='id[]' value='" . $row['about_id'] . "' />
</td></tr>
This is my php and query
$deleteCount = count($_POST['delete']);
for ($x = 0; $x < $deleteCount; $x++) {
if(isset($_POST['delete'][$x])) {
$sql = 'DELETE FROM about WHERE about_id = \''.$_POST['id'][$x].'\'';
$result = mysql_query($sql);
}
}
This is what happens.
If three rows are returned and I want to delete only the third, I check the third box and click submit. This deletes the first row that was returned. Again, if three rows are returned and I want to delete the first and third, I submit and the first two rows are deleted.
What it looks like is happening is for every checkbox that is checked, that many rows are deleted starting with the first. Any advice would be greatly appreciated.
The best and much easier way is to use
<input type="checkbox" name="delete[]" value="<?=$row['about_id']?>"/>
and then
foreach($_REQUEST['delete'] as $delID)
{
...
}
Use the following code
<tr><td valign='top'>
<label class='amarillo med' style='color:#C00;'>Delete Section </label>
<input type='checkbox' name='delete[$row['id']]' />
<input type='hidden' name='id[$row['id']]' value='" . $row['about_id'] . "' />
</td></tr>
where $row['id'] is the id of the row

Categories