Updating database with multiple checkbox values received from the loop? - php

Basically I am trying to update my database with multiple values. Input fields are in a loop, so I need to update multiple checkboxes and textboxes at the same time.
$cityID = $_POST["cityID"];
$cityStatus = $_POST["cityStatus"];
$cityOrder = $_POST["cityOrder"];
$updateCities = $db->execute("UPDATE cities SET city_status=?, city_order=? WHERE city_ID=$cityID", array($city_status, $cityOrder));
foreach($cities as $row) :
?>
<input type="checkbox" name="cityStatus[]" value="1">
<input type="text" name="cityOrder[]" value="<?php echo $row->city_order ?>">
<input type="hidden" name="cityID" value="<?php echo $row->city_ID; ?>">

What i understood is
1. you have array of 3 fields (cityid, cityorder, city status)
2. you are submitting all these values from the form
3. you want update cityorder and citystatus depends on cityid
The solutions is pretty simple
$i=0;
foreach($_REQUEST['cityOrder']) as $cityorder){
$cityID = $_POST["cityID"][$i];
$cityStatus = isset($_POST["cityStatus"][$i])?$_POST["cityStatus"][$i]:0;
$cityOrder = $_POST["cityOrder"][$i];
$updateCities = $db->execute("UPDATE cities SET city_status=?, city_order=? WHERE city_ID=$cityID", array($city_status, $cityOrder));
$i++;
}

Related

multiple values insertion of checkbox only last value is taking in database

I need to insert more than one values in checkbox but now it is seemed to be only the last value am entering is inserting to database.
this is my HTML code
<input id="option5" type="checkbox" name="product[]" value="<?php echo $result["software"];?>">
<label for="option5"><span><span></span></span><?php echo $result["software"];?></label>
and code for insertion is
$array = array($_POST['product']);
$value = implode(',', $array);
echo $value;
If you want to save multiple values in database then you need to create multiple checkboxes in html and then save the values with implode() method as below:-
<input id="option1" type="checkbox" name="product[]" value="value1">
<label for="option1">Value1</label>
<input id="option2" type="checkbox" name="product[]" value="value2">
<label for="option2">Value2</label>
<input id="option3" type="checkbox" name="product[]" value="value3">
<label for="option3">Value3</label>
now to store in database you can use implode() method.
$values=implode(",",$_POST['product']);
now store the $values in the database.
If i understand your problem you are looking for something like this:-
$array= array(
'0' => 123,
'1' => 456,
'2' => 789,
); // this is your post product array oryou want to get last value of that arrray
-----Or----
$array = $_POST['product']; // your post data after submission
end($array); // move the internal pointer to the end of the array
$key = key($array); // fetches the key of the element pointed to by the internal pointer
echo $array[$key]; // 789
Hope it helps!
Your problem is likely:
You have multiple checkboxes.
And you want to insert all of them into database.
You need to loop your posted checkboxes.
Your problem is likely that you are not looping over the $_POST['product'], so, only, the last product is inserting into database.
So, your checkboxes:
<input id="option5" type="checkbox" name="product[]" value="<?php echo $result["software"];?>">
<label for="option5"><span><span></span></span><?php echo $result["software"];?></label>
The code for posted file:
if (! empty($_POST['product'])) {
foreach ($_POST['product'] as $product) {
// Here, you insert $product into database.
}
}

how can i update value from the value in checkbox

i have 2 checkbox that deliver the different value..
<input type="hidden" name="position[]" value="<?php echo $excheck['check_id']; ?>">
<input name="position[]" class="checkbox" type="checkbox" <?php if($pos == $poss) echo 'checked = "checked"'; ?> value="<?php echo $eq['pos_unit_name']; ?>">
its for determined whether it is checked or not..
if it not checked , then the value is still delivered which is the check_id..
the problem is, if another checkbox is checked, and the current checkbox is remain, how do i add the code?
this is currently my code..
$check = $_POST['position'];
foreach ($check as $id)
{
if(is_numeric($id))
{
$up = mysql_query("delete from checklist where check_id = $id");
}
}
what i want to accomplish is , if its unchecked, the data is deleted based on the check_id and if another checkbox is checked , insert a new data, and if the checked box is remain checked, then it just updated the value based on the check_id
First need to use unique name for each form fields. you have two fields name as position so use different name
and query var should be quoted like
$up = mysql_query("delete from checklist where check_id = '$id'");
What you want to do in this case is to add the identifier as the key:
<input name="position[<?php echo $excheck['check_id']; ?>]" value="1" />
And then:
foreach ($_POST['position'] as $id => $checked) {
// do stuff with $id
}

How do I insert data from one table to another in mysql?

I have a grid with records and them had a checkbox <input name="id[]" value="<?php echo $valor->id ?>" type="checkbox" class="check" /> and I am trying to copy the selected record to another table
$sql = "INSERT INTO `pendientes` SELECT * FROM clientes WHERE id = $idcopia";
mysql_query($sql);
but I don't know how to send the id, my idea is select a checkbox and with a button "Copy" send the id but maybe there is another way.
Most browsers don't send the checkbox's value, just the word "on". To get around this, you can pass the "value" as a key to the array:
<input type="checkbox" name="id[<?php echo $valor->id; ?>]" class="check" />
Then on the PHP side:
$idlist = implode(",",array_map("intval",array_keys($_POST['id'])));
$sql = "INSERT INTO `pendientes` SELECT * FROM `clientes` WHERE `id` IN (".$idlist.")";
mysql_query($sql);

Populating checkboxlists from database

Is it possible to populate a checkboxlist from the database values? If so, please suggest how to do this.
$query2 = "select * from Products where CategoryID = '$CategoryName' ";
mysql_query($query2) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($query2)){
$array[] = $row;}
foreach($array as $val)
{
if($val =='the checkbox value')
<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>
These are the few steps you have to do.
1.Fetch the values from database.
2. Covert the values in array as they are stored as string by (,) separated.
3. Then
foreach($values as $val)
{
if($val =='Bike')
{
$bikeflag = '1';
}
if($val =='Car')
{
$carflag = '1';
}
}
<form>
<input type="checkbox" name="vehicle" value="Bike" <?php if(isset($bikeflag ) =='1'){ ?>checked = checked <?php } ?>/> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" <?php if(isset($carflag) =='1'){ ?>checked = checked <?php } ?>/> I have a car
</form>
Hopefully this will help you.
Get values from database with the appropriate SQL query
Loop through results echoing out a checkbox element
Caveats
Make sure each one has a unique name unless you want them to be an array. Then use array syntax for their name (e.g. name[])
Make sure to give each one a unique value
Yes, its very much possible:
Fetch the records to be populated with the structure:
Id and value
Provide datasource of the checkedbox list as the fetched structured list as mentioned above.
Now define the value field of the checkboxlist as "Id" and text as "value"

PHP inserting into database - complicated

I am trying to insert some values into a database from a form submitted by the user.
$id = mysql_insert_id();//Gets ID of last value inserted into related table
foreach( $_POST[ 'equipment' ] as $checkBoxIndex => $checkBoxValue ) {
mysql_query("INSERT INTO recipeequipment (recipeid, equipmentid, datetimeentered) VALUES('".$id."', '".$checkBoxValue."', '".$datetime."')");
}
The only thing that needs adding to this insert statement is the quantity, which is held in a similar fashion to the checkboxes, but in textboxes. like this:
<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment[]" value="'.$rowequipment['equipmentid'].'"/>
<input type="text" name="count[]" id="count[]" size="3" value="" />'
.$rowequipment['description']."<br />";
}?>
The piece i need help with is the second input column. I need to put the values that correspond with the check boxes into the insert statement, but am unsure of how to do this.
Ignoring all the vunrabilities in your code for now, doing something like this should allow you to get a unique id and match the checkbox up to the input:
<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment['.$rowequipment['equipmentid'].']" value="'.$rowequipment['equipmentid'].'"/>
<input type="text" name="count['.$rowequipment['equipmentid'].']" id="count-'.$rowequipment['equipmentid'].'" size="3" value="" />'
.$rowequipment['description']."<br />";
}?>
Then once that form is submitted you would simply loop through each checkbox by doing something like:
foreach ($_POST['equipment'] as $id => $checkboxValue) {
$textInputValue = $_POST['count'][$id];
// do something with the above value here
}
Although, I'm not quite sure why you're using checkboxes like this - is it so you ignore updating the rows that they don't check off or something?

Categories