PHP Crud Updating selection shows duplicate - php

I have a PHP Crud system. One of the options when creating a new record is a selection box with two options. Netman and Administrator. When I update the record I see the option, but it duplicates and it has the other option,
like this. I cant see how I can show the record from the DB and the other option. Is this possible?
<?php
include "config.php";
include "header.php";
if(isset($_GET['u'])):
if(isset($_POST['bts'])):
$stmt = $mysqli->prepare("UPDATE passwordtable SET school=?,usertype=?, password=?, notes=? WHERE id_personal=?");
$stmt->bind_param('sssss', $nm, $gd, $tl, $ar, $id);
$nm = $_POST['nm'];
$gd = $_POST['gd'];
$tl = $_POST['tl'];
$ar = $_POST['ar'];
$id = $_POST['id'];
if($stmt->execute()):
echo "<script>location.href='index.php'</script>";
else:
echo "<script>alert('".$stmt->error."')</script>";
endif;
endif;
$res = $mysqli->query("SELECT * FROM passwordtable WHERE id_personal=".$_GET['u']);
$row = $res->fetch_assoc();
?>
And the selection box code us
<label for="gd">User Type</label>
<select class="form-control" id="gd" name="gd">
<option><?php echo $row['usertype'] ?></option>
<option>Administrator</option>
<option>NetMan</option>
</select>

If you still not find answer please have look at below code
<?php
$usertypes = array('Administrator' => 'Administrator', 'NetMan' => 'NetMan');
$selcted_usertype = !empty($row['usertype']) ? $row['usertype'] : "";
?>
<label for="gd">User Type</label>
<select class="form-control" id="gd" name="gd">
<?php
foreach ($usertypes as $user_type_val => $usertype_txt) {
$selected = ($user_type_val == $selcted_usertype) ? "selected='selected'" : "";
?>
<option <?php echo $selected; ?> value="<?php echo $user_type_val; ?>"><?php echo $usertype_txt; ?></option>
<?php }
?>
</select>
You will need to value attribute for option.

Related

how to display selected multiple dropdown values in edit page in CodeIgniter

how to display selected multiple dropdown values in edit page in CodeIgniter
Values ​​are not displaying in multiple dropdowns on the edit page
this is how to retrieve data in db
<?php $assignuserstable = $this->db->get_where('assignuserstable',array('user_id'=>$user_id))->row_array(); ?>
<div class="form-group col-md-6">
<label for="admin_id"><?php echo get_phrase('Assign User'); ?>
<span class="text-danger">*</span></label>
<select class="form-control selectpicker" name="admin_idd[]" id="admin_id"
placeholder="Assign User" required multiple>
<option value="" hidden><?php echo get_phrase('Select User'); ?></option>
<?php
$system_usertable = $this->db->get('system_usertable')->result_array();
foreach($system_usertable as $row2):
?>
<option value="<?php echo $row2['admin_id'];?>"
<?php if($assignuserstable['admin_id'] == $row2['admin_id'])echo 'selected';?>>
<?php echo $row2['first_name'];?>
</option>
<?php
endforeach;
?>
</select>
</div>
This is how multiple array is added to the database
model
public function addclientdetails(){
$data['business_name'] = html_escape($this->input->post('business_name'));
$data['legal_name'] = html_escape($this->input->post('legal_name'));
$data['status'] = html_escape($this->input->post('status'));
$data['rating'] = html_escape($this->input->post('rating'));
$data['SU_id'] =html_escape($this->input->post('admin_id'));
date_default_timezone_set("Asia/Kolkata");
$data['created_at'] = Date('Y-m-d h:i:s');
$data['created_by'] = $this->session->userdata('admin_id');
$this->db->insert('user_table', $data);
$insertId = $this->db->insert_id
$admin_idd =html_escape($this->input->post('admin_idd'));
$result = array();
foreach($admin_idd AS $key => $val){
$result[] = array(
'user_id' => $insertId,
'admin_id' => $_POST['admin_idd'][$key],
'user_type' => html_escape($this->input->post('user_type')),
);
}
$this->db->insert_batch('assignuserstable', $result);
$insertId = $this->db->insert_id();
return $insertId;
}
it is impossible to identify the problem when you don't share your code. Please share your model and controller too.
At first please echo $system_usertable and $row2 and check whether there is a data or not.
and your option should looks like this
<option value="<?php echo $row2['admin_id'];?>" <?php echo (isset($assignuserstable['admin_id']) && $assignuserstable['admin_id'] == $row2['admin_id'])? 'selected' : NULL; ?>><?php echo $row2['first_name'];?></option>

Select an option in a dropdown box and display its associated data in php?

How to select a supplement and display its associated data such as its description and cost.
Any help will be appreciated.
PHP:
require('database.php');
$suppID = filter_input(INPUT_POST, 'suppID');
if($suppID==null || $suppID==false){
$suppID = 'Supplement-1';
}
$query = 'select * from tblsupplements where Supplement_id= :SupplementID';
$statement = $db->prepare($query);
$statement = bindValue(':SupplementID', $suppID);
$statement->execute();
$supplements = $statement->fetchAll();
$statement->closeCursor();
HTML:
<label>Supplement ID:</label>
<select name='suppID'>
<?php foreach ($supplements as $supplement): ?>
<option value="<?php echo $supplement['Supplement_id']; ?>">
<?php echo $supplement['Supplement_id']; ?>
</option>
<?php endforeach; ?>
</select>
Try this, hopefully helps.
<label>Supplement ID:</label>
<select name='suppID'>
<?php foreach ($supplements as $supplement) {
echo "<option value='".$supplement['Supplement_id']."'>".$supplement['Supplement_id']."</option>";
}
?>
</select>

Set the default value on a select option drop down

I want to make the default value the same as set in the database. This is because i am using it to edit items in the databse and want the values to be preset.
This is currently setting it to black at the start.
$_SESSION["menuID"] = $_POST["menuID"];
$menuID = $_POST["menuID"];
$menu_sql = "SELECT * FROM menu WHERE menuID = $menuID";
$menu_query = mysqli_query($dbconnect, $menu_sql);
$menu_aa = mysqli_fetch_assoc($menu_query);
$select_sql = "SELECT * FROM course ORDER BY courseID";
$select_query = mysqli_query($dbconnect, $select_sql);
$select_aa = mysqli_fetch_assoc($select_query);
<select name="courseID">
<?php do { ?>
<option selected="<?php echo $select_aa['courseID']; ?>" value="<?php echo $select_aa['courseID']; ?>"><?php echo $select_aa['name']; ?></option>
<?php } while ($select_aa = mysqli_fetch_assoc($select_query)); ?>
</select>
Setting default using ternary operator.
<select name="courseID">
<?php do { ?>
<option selected="<?php echo ( $select_aa['courseID'] == 'Your default value here' ? "selected" : "" ); ?>" value="<?php echo $select_aa['courseID']; ?>"><?php echo $select_aa['name']; ?></option>
<?php } while ($select_aa = mysqli_fetch_assoc($select_query)); ?>
</select>

How to echo selected values on multiselect from database on a multiselect form

i have a registration form using multiselect which insert options in the value 1,2,3,4,5,6,7 based on selection
However, after inserting i want to provide an option for editing where users edit
and i want the already selected values from database to be preselected while looping other options as unselected
I am experiencing unidentified offset 3, unidentified offset 3........
<div class="form-group">
<label for="touch-spin-1">Course Days</label>
<select multiple="multiple" name="coursedays[]" style="width: 100%;" data-toggle="select2" data-placeholder="<?php //echo $course_days; ?>" data-allow-clear="true" >
<?php
$entities = "1,2,3,4,5,6,7"; $entity = explode(",",$entities); $servs = explode("," , $course_days); $arr_length = count($entity); for($i=0;$i<=$arr_length;$i++){
?>
<option <?php if (in_array($servs, $entity)) { echo "selected";} ?> value="<?php echo $servs[$i]; ?>"> <?php echo $servs[$i]; ?></option>
<?php } ?></select></div>
try in this way
<?php
$course_days = "2,3,4";
$entities = "1,2,3,4,5,6,7";
$entity = explode(",", $entities);
$servs = explode(",", $course_days);
foreach($entity as $en) {
?>
<option value="<?php echo $en; ?>" <?php echo (in_array($en, $servs)) ? 'selected' : ''; ?>>
<?php echo $en; ?>
</option>
<?php } ?>

Throw select php variable into a form and convert it to a POST variable

I'm trying to use a drop down menu to load data from a select value. I want it passed into a php document in order to use the data that I need. What should I do? Thanks in advance.
This is my code for the select menu:
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
$checkz = mysql_numrows($resultz);
$count = 0;
?>
<select name="ltemp">
<?php
while($count<$checkz){
$selectname=mysql_result($resultz,$count,"temp1_name");
?>
<option value="<?php echo "$selectname";?>"><?php echo $selectname;?></option>
<?php
$count++;
}
?>
</select>
<input type="submit" value="Load Template" class="ufbutton"><br></center>
</form>
and this is my php page
$uname = $_GET['username'];
$loadtemp = $_POST['ltemp'];
header("Location:editing1.php?username=$uname&tempname=$loadtemp");
it seems that you have use " inside " in this line :
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
try this:
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
$checkz = mysql_numrows($resultz);
$count = 0;
?>
<select name="ltemp">
<?php
while($count<$checkz){
$selectname=mysql_result($resultz,$count,"temp1_name");
?>
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
<?php
$count++;
}
?>
</select>
Alternative :
$sqlz = "SELECT * FROM content_temp1 WHERE user_uname='$uname'";
$resultz = mysql_query($sqlz);
?>
<select name="ltemp">
<?php
while ($row = mysql_fetch_array($resultz, MYSQL_ASSOC)) {
$selectname=$row["temp1_name"];
?>
<option value="<?php echo \"$selectname\";?>"><?php echo $selectname;?></option>
<?php } ?>
</select>
Be careful this is ripe for sql injection without validating the input;
SQL Injection with GET

Categories