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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am coding a PC repair application using MySQLi. I have an ENUM row in my database called "item_for_repair". In the ENUM list is Laptop, Desktop etc etc.
On my front end html table, when i click edit on a specific job card, under "item for repair" there is a drop down that always has the first value (for example, Laptop) selected and not the actual item that was stored (for example, Desktop), i need the drop down to be pre-selected with the item for repair that was originally stored in the database when storing the job card.
Here is an example image of what is happening. The item for repair in the databse is Desktop, but here you can clearly see its showing Laptop.
My code to connect to database as well as select all from the database:
<?php include 'settings.php'; //This file has all the database connection settings etc.
?>
<?php
$eid=$_GET['editid'];
$ret=mysqli_query($conn,"select * from pcrepairs where job_number='$eid'");
while ($row=mysqli_fetch_array($ret)) {
?>
And here is the select box code:
<select name="item_for_repair" id="item_for_repair">
<option value="Laptop" <?= $item_for_repair === 'Laptop' ? 'selected' : '' ?>>Laptop</option>
<option value="Desktop" <?= $item_for_repair === 'Desktop' ? 'selected' : '' ?>>Desktop</option>
<option value="Television" <?= $item_for_repair === 'Television' ? 'selected' : '' ?>>Television</option>
<option value="Washing Machine" <?= $item_for_repair === 'Washing Machine' ? 'selected' : '' ?>>Washing Machine</option>
<option value="Tumble Dryer" <?= $item_for_repair === 'Tumble Dryer' ? 'selected' : '' ?>>Tumble Dryer</option>
<option value="Dishwasher" <?= $item_for_repair === 'Dishwasher' ? 'selected' : '' ?>>Dishwasher</option>
<option value="Microwave" <?= $item_for_repair === 'Microwave' ? 'selected' : '' ?>>Microwave</option>
<option value="Fridge" <?= $item_for_repair === 'Fridge' ? 'selected' : '' ?>>Fridge</option>
<option value="Printer" <?= $item_for_repair === 'Printer' ? 'selected' : '' ?>>Printer</option>
<option value="Other" <?= $item_for_repair === 'Other' ? 'selected' : '' ?>>Other</option>
</select>
Where have i gone wrong?
Here is a complete example of how to populate an HTML select retrieving data from a MySQL database using MySQLi
<?php
class Country {
public $code;
public $name;
function __construct($code, $name) {
$this->code = $code;
$this->name = $name;
}
}
define("HOST", "localhost");
define("USERNAME", "root");
define("PASSWD", "");
define("DBNAME", "shop");
function country_select_list() {
$array = null;
$link = mysqli_connect(HOST, USERNAME, PASSWD, DBNAME);
$stmt = mysqli_prepare($link, "select code, name from country order by name");
if (!mysqli_execute($stmt)) {
throw new Exception(mysqli_stmt_error($stmt));
}
$code = null;
$name = null;
mysqli_stmt_bind_result($stmt, $code, $name);
while (mysqli_stmt_fetch($stmt)) {
$array[] = new Country($code, $name);
}
mysqli_stmt_close($stmt);
mysqli_close($link);
return $array;
}
$user_country = 'CHE';
?>
<select id="user_country" name="user_country">
<option value=""></option>
<?php
foreach (country_select_list() as $country) {
echo ' <option value="' . $country->code . '"';
if (isset($user_country) && $user_country == $country->code) {
echo ' selected="selected"';
}
echo ">" . $country->name . "</option>\r\n";
}
?>
</select>
This is the output
<select id="user_country" name="user_country">
<option value=""></option>
<option value="ITA">Italy</option>
<option value="CHE" selected="selected">Switzerland</option>
</select>
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.
I am trying to write a php script that takes a value from database and sets it to a dropdown named gender. I have written the following code:
while($row = mysqli_fetch_assoc($result))
{
$gender = $row['gender'];
$output.= "Gender: <select name='gender'>
<option value='0' ".if ($gender != 'M' || $gender !='F') { echo ('selected="selected"');}.">Select gender</option>
<option value='M' ". if ($gender == 'M') { echo ('selected="selected"');}.">Male</option>
<option value='F' ". if ($gender == 'F') { echo ('selected="selected"');}.">Female</option></select>";
}
I get a server error because of the echo statement(echo ('selected="selected"')):
The website encountered an error while retrieving http://localhost/search.php. It may be down for maintenance or configured incorrectly.
What is the solution for this?
You could do just a simple comparison function that returns the select string by comparing two parameters:
function selectGender($gender,$out=false)
{
// You may not need these 3 lines if $gender is either empty, M, or F //
$filter = array('M','F');
if(!in_array($gender,$filter))
$gender = false;
if($gender == $out)
return ' selected="selected"';
}
while($row = mysqli_fetch_assoc($result)){
$gender = $row['gender'];
$output.= "Gender: <select name='gender'>
<option value='0'".selectGender($gender).">Select gender</option>
<option value='M'".selectGender($gender,'M').">Male</option>
<option value='F'".selectGender($gender,'F').">Female</option></select>";
}
Okay, the best option here would to just be to remove the source of the problem, make it an if statement:
if ($gender != 'M' && $gender != 'F') {
output .= "...Enter he html with he generic answer selected...";
}
else if ($gender == 'M') {
output .= "...Enter html with Male selected...";
else {
output .= "...Enter html with Female selected...";
}
It may be a little bulky, but it does the job.
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>
I've a table with multiple rows, each row has a drop down list. I am new to javascript and is unable to retrieve the selected value from the row. Any idea how can it be done? Thanks in advance.
Code:
function chng_page(serialNum, rowid)
{
alert(rowid);
alert(serialNum);
var select_index = document.getElementById('orderStatus').selectedIndex;
//get the value of selected option
var option_value = document.getElementById('orderStatus').options[select_index].value;
//redirect with value as a url parameter
window.location = 'orderStatus.php?serialNum=' + serialNum + '&status=' + option_value;
}
</script>
//code for drop down list
<select id="orderStatus" name="orderStatus" onchange="chng_page(<?php echo $serialNum? >, this.parentNode.parentNode.rowIndex)">
<option value="Pending" <?php echo ($status == "Pending") ? ' selected="selected"' : ''; ?>>Pending</option>
<option value="Cooking" <?php echo ($status == "Cooking") ? ' selected="selected"' : ''; ?>>Cooking</option>
<option value="On The Way" <?php echo ($status == "On The Way") ? ' selected="selected"' : ''; ?>>On The Way</option>
<option value="Delivered" <?php echo ($status == "Delivered") ? ' selected="selected"' : ''; ?>>Delivered</option>
</select>
You didn't mention if the select element in each row has a unique id or not, but it somehow shows in your code that they do.
In that case, you can use JQuery (a popular JavaScript framework) to retrieve the selected value.
Assuming you have included the JQuery file in your HTML, and attached an event handler to the select element (to make it easier, pass in the id of the current select element as a parameter in the function), do this:
var selected_value = $("#id_of_select_element option:selected").val();
If the ids of the select elements are unique, that single line of code will definitely work.
You have the same name/id for the drop down in all table rows
If there is a dropdownlist in each row with same id-> #orderStatus, you should try this,
function chng_page(serialNum, ddlist)
{
var rowid = ddlist.parentNode.parentNode.rowIndex
alert(rowid);
alert(serialNum);
var select_index = ddlist.selectedIndex;
//get the value of selected option
var option_value = ddlist.options[select_index].value;
//redirect with value as a url parameter
window.location = 'orderStatus.php?serialNum=' + serialNum + '&status=' + option_value;
}
<select name="orderStatus" onchange="chng_page(<?php echo $serialNum?>, this)">
<option value="Pending" <?php echo ($status == "Pending") ? ' selected="selected"' : ''; ?>>Pending</option>
<option value="Cooking" <?php echo ($status == "Cooking") ? ' selected="selected"' : ''; ?>>Cooking</option>
<option value="On The Way" <?php echo ($status == "On The Way") ? ' selected="selected"' : ''; ?>>On The Way</option>
<option value="Delivered" <?php echo ($status == "Delivered") ? ' selected="selected"' : ''; ?>>Delivered</option>
</select>