INSERT not working when selection value is -1 - php

I have problem, that if I not select any item from selection list, they not inserting element in my DB.
I put some come, that if I select value -1 from list, which is ------------, then they should make it NULL in that field.
There is my code.
<p>
<?php
if ($_POST['fk_KOMANDAid_KOMANDA'] == '-1') {
$_POST['fk_KOMANDAid_KOMANDA'] = NULL;}
?>
<label class="field" for="fk_KOMANDAid_KOMANDA">Komanda<?php echo in_array('fk_KOMANDAid_KOMANDA', $required) ? '<span> *</span>' : ''; ?></label>
<select id="fk_KOMANDAid_KOMANDA" name="fk_KOMANDAid_KOMANDA">
<option value="-1">---------------</option>
<?php
$kom = $asmuoObj->getkomanda();
foreach($kom as $key => $val) {
$selected = "";
if(isset($data['fk_KOMANDAid_KOMANDA']) && $data['fk_KOMANDAid_KOMANDA'] == $val['id']) {
$selected = " selected='selected'";}
echo "<option{$selected} value='{$val['id']}'>{$val['pavadinimas']}</option>";}
?>
</select>
</p>
This is INSERT function.
public function insertkomanda($data) {
$query = " INSERT INTO {$this->komanda_lentele}
(
`pavadinimas`,
`sutrumpinimas`,
`ikurimo_data`,
`svetaines_adresas`,
`fk_VALSTYBEid_VALSTYBE`,
`fk_RUNGTYNESid_RUNGTYNES`
)
VALUES
(
'{$data['pavadinimas']}',
'{$data['sutrumpinimas']}',
'{$data['ikurimo_data']}',
'{$data['svetaines_adresas']}',
'{$data['fk_VALSTYBEid_VALSTYBE']}',
'{$data['fk_RUNGTYNESid_RUNGTYNES']}'
)";
mysql::query($query);
}
When I press SUBMIT, then nothing happens, data not intserted in my DB. Should insert with fk_KOMANDAid_KOMANDA eaquls NULL.

Related

How can i echo the name of a button what i pressed?

I'm using a for cycle that creates as many buttons as many names are in my database.
$result = $db->query("SELECT `username` FROM `vote`");
for ($i=0; $i < $result->rowcount(); $i++) {
And every button has a name fetched from the database.
<button name="<?php echo $Class->FileName[$i];?>"
I want to save the name of a button into a variable after it's pressed. How can I achieve this?
You can search the name of the button in the $_POST variable. Will return true if submited.
if($_POST['mi-button']){$button='mi-button';}
<select class="form-control space-bottom" onclick="getHistory(this.value)" id="animal" style="width:40%;display:block;margin-auto;margin-top:70px;margin-left:300px"name="target" required>
<?php
foreach($patients_rows as $key0 => $value0){
$name = implode(' ',$value0);
unset($value0['history'],$value0['ID']);
$aj = implode(' ', $value0);
foreach($value0 as $key =>$value){
if($key == 'pet_name'){
echo '<option " value="'.$aj.'">'.$aj.'</option>';
}
}
}
?>
</select>
Would be like this but with buttons instead of select/option

Codeigniter Form Extend Helper

Im trying to do chained select using jquery chained, but codeigniter echo form dropdown does not allow individual assignment of CLASS.
I would like to assign CLASS to each list like the example below.
<select name="hardware">
<option class="printer" value="" selected="selected"></option>
<option class="printer" value="EPSON">EPSON</option>
<option class="printer" value="HP">HP</option>
<option class="hdd" value="WD">WD</option>
<option class="hdd" value="SEAGATE">SEAGATE</option>
</select>
and here is the codeigniter form dropdown
VIEW PAGE:
<form action="" method="">
$select = 'hardware';
echo form_dropdown('hardware', $hardware,set_value('hardware',$this->input->post('hardware'))); ?>
</form>
I have changed the select form to this.
<select name="supplier">
<?php foreach($supplier as $row){ ?>
<option value="<?php echo $row['supplier'];?>"><?php echo $row['supplier'];?>
</option>
<?php }?>
</select>
how can i return the selected value after a failed validation?
This is not possible using the form_dropdown() because codeigniter is not allowing any parameter to set the "extra" attributes like class in the option tag.
Check this function : this is the core function form_dropdown() :
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
{
if ( ! is_array($selected))
{
$selected = array($selected);
}
// If no selected state was submitted we will attempt to set it automatically
if (count($selected) === 0)
{
// If the form name appears in the $_POST array we have a winner!
if (isset($_POST[$name]))
{
$selected = array($_POST[$name]);
}
}
if ($extra != '') $extra = ' '.$extra;
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
$form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
foreach ($options as $key => $val)
{
$key = (string) $key;
if (is_array($val) && ! empty($val))
{
$form .= '<optgroup label="'.$key.'">'."\n";
foreach ($val as $optgroup_key => $optgroup_val)
{
$sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
$form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
}
$form .= '</optgroup>'."\n";
}
else
{
$sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
$form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
}
}
$form .= '</select>';
return $form;
}
what you want to do, you will have to create your own helper,See "Extending Helpers" here in the docs. I would do what is says and copy a "MY_helper.php" version to your application folder; don't mess with the core unless you really have to.
http://ellislab.com/codeigniter/user-guide/general/helpers.html
You could use the values array and set a class and item in an array (and change the foreach near line 327 in the helper) or pass another array and check it in the foreach.

How to get the selected value from dropdown on post edit

I have the database for categories where i have id and name rows
<select name="category_id">
<option value="<?php if($category_id === 1) { echo 'selected';} ?>">Electronics</option>
<option value="<?php if($category_id === 2) { echo 'selected';} ?>">Automotive</option>
</select>
for some reason this does not show which one was selected when trying to edit the submitted post
and i do retrieve the category_id like so:
$res6 = mysql_query("SELECT * FROM `posts` WHERE `id` = '" . $id . "' LIMIT 1");
if(mysql_num_rows($res6) > 0){
while($row6 = mysql_fetch_assoc($res6)){
$id = $row6['id'];
$category_id = $row6['category_id'];
$price = $row6['price'];
$cover = $row6['cover'];
$title = $row6['title'];
$description = $row6['description'];
}
}
I think the option argument for selected is
<option selected="selected"> </option>
I believe MySQL results are always returned as strings, so $category_id === 1 will be false.
Either try $category_id === "1" or $category_id == 1
Also, you need to echo the selected="selected outside the value attribute
You can also try like this as well.
<select name="category_id">
<?php
$cats = array("1"=>"Electronics", "2"=>"Automotive");
foreach($cats as $k=>$cat){
if( $k == $category_id )
echo "<option value='{$k}' selected='selected'>{$cat}</options>";
else
echo "<option value='{$k}' >{$cat}</options>";
}
?>
</select>

SELECT bit-value in db worked on NAS, not on server

I have some comboboxes with yes/no (value 1/0) that I store as a bit in my db. To put that value in the correct member of my object I do this:
class Member {
var $active;
public function refreshData() {
$mysqli = connectdbMySQLI();
$query = "SELECT * FROM tblMember WHERE ID = " . $this->id;
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC);
$this->setData($row);
$mysqli->close();
}
public function setData($row) {
$this->active = $row['active'];
}
}
Alle varchars, int and text-values or being assigned, except these bit values.
I checked with var_dump and I get an empty string
["active"]=> string(1) ""
Saving doesn't work as well:
$member = new Member(isset($_POST['id']) ? $_POST['id'] : null );
if(isset($_POST['active'])) {
$member->active = $_POST['active'];
$member->save();
}
This I use to select the right value in the combobox:
<select type="text" name="active" id="active" class="txt">
<option value="0" <?php echo $member->active == 0 ? 'selected="selected"' : ''; ?>>No</option>
<option value="1" <?php echo $member->active == 1 ? 'selected="selected"' : ''; ?>>Yes</option>
</select>**
What can be different than on my local NAS?
EDIT:
I tried this (all results are 1):
$this->active = $row['active'] == true ? '1' : '0';
and this (all results are 0):
$this->active = $row['active'] == 1? '1' : '0';
EDIT 2:
just tried a simple query (no mysqli) and I get an empty result for all my bit-fields
the following condition will only work if you have selected the value 1 from the active dropdown
if(isset($_POST['active']))
you need to replace it with the following
if(!$_POST['active']) {
$_POST['active'] = 0;
}else{
$_POST['active'] = 1;
}
OR Solution 2
update the following values in drop down
<select type="text" name="active" id="active" class="txt">
<option value="1" <?php echo $member->active == 1 ? 'selected="selected"' : ''; ?>>No</option>
<option value="2" <?php echo $member->active == 2 ? 'selected="selected"' : ''; ?>>Yes</option>
</select>**
it will not bypass your if condition.

Array doesn't appear to be holding data

I have a system where I rearrange navigational items.
It stores the new order in an array and then I use a for loop to go through the array and update the database.
<?php
$apageid = array();
$apagename = array();
$apageorder = array();
$q = "SELECT g.id, g.title, n.order FROM tbl_general g INNER JOIN tbl_navigation n ON n.pageid = g.id WHERE n.type = '$_SESSION[parent]' ORDER BY n.order";
$return = $database->query($q);
while($row=mysql_fetch_assoc($return)){
$apageid[] = $row['id'];
$apagename[] = $row['title'];
$apageorder[] = $row['order'];
}
$count = count($apageid);
$bpageid = array();
$bpageorder = array();
?>
<div class="form-holder">
<?php
//run through each page one at a time and update the order of the menu
mysql_data_seek( $return, 0 ); //reset the pointer to do the same query
$i = 0; //the count for saving the new configuration
while($row=mysql_fetch_assoc($return)){
$id = $row['id'];
$title = $row['title'];
$order = $row['order'];
?>
<div class="form-row">
<div class="form-label">
Page Name
</div>
<div class="form-field">
<select class="select-small" name="<?php echo $bpageid[$i]; ?>">
<?php
for($j=0; $j<$count; $j++){
if($apageid[$j] == $id) {
$selected = true;
} else {
$selected = false;
}
?>
<option value="<?php echo $apageid[$j]; ?>" <? echo ($selected == true) ? 'selected="selected"' : ''; ?>><?php echo $apagename[$j]; ?></option>
<?php
}
?>
</select>
<select class="select-small" name="<?php echo $bpageorder[$i]; ?>">
<?php
for($k=0; $k<$count; $k++){
if($apageorder[$k] == $order) {
$selected = true;
} else {
$selected = false;
}
?>
<option value="<?php echo $apageorder[$k]; ?>" <? echo ($selected == true) ? 'selected="selected"' : ''; ?>><?php echo $apageorder[$k]; ?></option>
<?php
}
?>
</select>
</div>
</div>
<?php
$i++;
}
?>
This first chunk of code is the menu where you can reorder items.
Initially it loads up the current select and allows you to change the ordering.
function reorderChildren($pageid, $pageorder){
global $database;
$count = count($pageid);
//run through each page one at a time and update the order of the menu
for($i=0; $i<$count; $i++){
//set a few variables
$pid = $pageid[$i];
$porder = $pageorder[$i];
echo "pid = $pid porder = $porder";
$q = "UPDATE tbl_navigation SET order = '$porder' WHERE pageid = '$pid' AND type = '$_SESSION[parent]'";
$database->query($q);
}
return 0;
}
The information then ends up being passed here and the problem appears to be the for loop is never executed.
Can anyone see why this would be?
It's not the naming used, I've checked them.
Am I storing information in the arrays correctly?
Can I make it clear that it's $bpageid and $bpageorder that are the arrays carrying the information forward.
Thanks!
Was a simple fix!
The problem was that the select name needed to be just name="bpageid[]" rather than what I listed above. Silly!
You have just initilized $bpageid = array(); at top after first while loop.No value is assigned
after that you using
<select class="select-small" name="<?php echo $bpageid[$i]; ?>">
but no value is in $bpageid[$i] so name of this select box is blank.
It might be problem. check this and do as required.
you could try to construct a json from the options of the select element store it to a hidden field or send ajax request to php script for processing , order could be made from a "weight" value 1,2,3 etc.... sort it with php and loop to display

Categories