Update & Display Data from a session - php

I'm trying to do update profile from a session. I got confused because I can print username because of session. but password, Full Name, and level doesn't match with database.
It should be
Username : Ratih | Full Name : Ratih Cha | Level : Sales
But the result
Username : Ratih | Full Name : Administrator | Level : Admin
Here is my Controller:
public function profile(){
$uprofile = $this->salesModel->openProfile();
$data = array(
"username" => $uprofile[0]['username'],
"fullname" => $uprofile[0]['fullname'],
"level" => $uprofile[0]['level'],
"pic" => $uprofile[0]['pic']
);
$this->load->view('v_navbar');
$this->load->view('v_leftside');
$this->load->view('v_profile', $data);
}
Model
public function openProfile(){
$username = $this->session->userdata('username');
$data = $this->db->get('tuser');
$this->db->where('username', $username);
return $data->result_array();
}
View
<form role="form" action="<?php echo base_url()."index.php/SalesAdmin/update_profile/"?>" method="post">
<div class="form-group">
<label> Username : </label>
<input type="text" class="form-control" id="datepick" name="fu_date" value="<?php echo $_SESSION['username']; ?>" readonly>
</div>
<div class="form-group">
<label> Full Name : </label>
<input type="text" class="form-control" id="datepick" name="fu_date" value="<?php echo $fullname; ?>">
</div>
<div class="form-group">
<label> Level : </label>
<input type="text" class="form-control" id="datepick" name="fu_date" value="<?php echo $level; ?>" readonly>
</div>
<div class="form-group">
<img data-name="<?php echo $_SESSION['username']; ?>" id="profile" class="img-circle" alt="Avatar" width="40px" height="32px"/><span></span></i>[enter image description here][1]
<?php echo form_open_multipart('upload/do_upload');?>
<?php echo form_upload('pic'); ?>
</div>
<div>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>

Related

How to update 3 table existing data by id in codeigniter

I have data that already exists in the database server and I want to change it based on the id table. I want to update one time so that the 3 data tables are updated as well because the table id is in another table too. I tried it but I can't. Please help me
I have joined from several tables.
I've also searched on :link!
:link!
This is my Controller
public function updateservicenew(){
$id= $this->uri->segment(3);
$data['vendor']= $this->model_vendor->show_data_all();
$data['vendor2']= $this->model_vendor->show_data_vendor2();
$data['service']= $this->Madmin2->getservicenew($id)->row_array();
$this->template->viewadmin2('admin2/transaks/updateservicenew',$data);
}
public function saveservicenew(){
$this->Madmin2->saveservicenew($this->input->post());
redirect(base_url('admin2/transaksidone'));
}
This is my Model
public function getservicenew($id){ $this->db->select('kontak_sis.*,vendor.*,vendor2.*,service_sis.*,sumber_info.*,provinsi.*,kota.*,mst_service.*,service.*');
$this->db->from('service_sis');
$this->db->join('vendor', 'service_sis.id_vendor=vendor.id_vendor', 'left');
$this->db->join('vendor2', 'service_sis.id_vendor2=vendor2.id_vendor2', 'left');
$this->db->join('mst_service', 'service_sis.id_mst_service=mst_service.id_mst_service', 'left');
$this->db->join('kontak_sis', 'service_sis.id_kontak=kontak_sis.id_kontak', 'left');
$this->db->join('sumber_info', 'service_sis.id_sumber=sumber_info.id_sumber', 'left');
$this->db->join('provinsi', 'service_sis.id_provinsi=provinsi.id_provinsi', 'left');
$this->db->join('kota', 'service_sis.id_kota=kota.id_kota', 'left');
$this->db->join('service', 'service_sis.id_service=service.id_service', 'left');
$param = array('service_sis.id_service'=>$id);
return $this->db->get_where('',$param);
}
public function simpanservicenew(){
$data_pelanggan['cid']= $this->input->post('cid');
$data_pelanggan['nama_perusahaan']= $this->input->post('nama_perusahaan');
$data_pelanggan['destination']= $this->input->post('destination');
$data_pelanggan['alamat_perusahaan']= $this->input->post('alamat_perusahaan');
$data_pelanggan['pic_perusahaan']= $this->input->post('pic_perusahaan');
$data_pelanggan['telepon_pic']= $this->input->post('telepon_pic');
$data_pelanggan['fax']= $this->input->post('fax');
$data_pelanggan['hp']= $this->input->post('hp');
$data_pelanggan['email_pic']= $this->input->post('email_pic');
$this->db->where('id_kontak',$this->input->post('id_kontak'));
$this->db->update('kontak_sis',$data_pelanggan);
$data_vendor['pic_vendor']= $this->input->post('pic_vendor');
$data_vendor['media_vendor']= $this->input->post('media_vendor');
$data_vendor['kapasitas_vendor']= $this->input->post('kapasitas_vendor');
$data_vendor['telepon_vendor']= $this->input->post('telepon_vendor');
$data_vendor['email_vendor']= $this->input->post('email_vendor');
$this->db->where('id_vendor',$this->input->post('id_vendor'));
$this->db->update('vendor',$data_vendor);
$data_vendor2['pic_vendor2']= $this->input->post('pic_vendor2');
$data_vendor2['media_vendor2']= $this->input->post('media_vendor2');
$data_vendor2['kapasitas_vendor2']= $this->input->post('kapasitas_vendor2');
$data_vendor2['telepon_vendor2']= $this->input->post('telepon_vendor2');
$data_vendor2['email_vendor2']= $this->input->post('email_vendor2');
$this->db->where('id_vendor2',$this->input->post('id_vendor2'));
$this->db->update('vendor2',$data_vendor2);
This is my View
<div class="row">
<div class="box box-primary">
<div class="box-header with-border">
<div id='progress'><div id='progress-complete'></div></div>
<h2 class="box-title"><b>Set Ewo</b></h2>
</div>
<div class="box-body">
<form method="post" action="<?php echo base_url('admin2/simpanservicebaru/'.$this->uri->segment(3)); ?>" enctype="multipart/form-data" id="myForm">
<input type="hidden" name="id_service" value="<?php echo $service['id_service']; ?>">
<input type="hidden" name="id_kontak" value="<?php echo $service['id_kontak']; ?>">
<input type="hidden" name="id_mst_service" value="<?php echo $service['id_mst_service']; ?>">
<input type="hidden" name="id_vendor" value="<?php echo $service['id_vendor']; ?>">
<input type="hidden" name="id_vendor2" value="<?php echo $service['id_vendor2']; ?>">
<fieldset>
<div class="form-group">
<label for="">Registration Form ID</label>
<input type="text" name="cid" class="form-control" placeholder="" value="<?php echo $service['cid'] ?>" readonly>
</div>
<div class="form-group">
<label for="">Customer</label>
<b><input type="text" name="nama_perusahaan" class="form-control" value="<?php echo $service['nama_perusahaan']; ?>"required></b>
</div>
<div class="form-group">
<label for="">Destination</label>
<b><input type="text" name="destination" class="form-control" placeholder="destination"
value="<?php echo $service['destination']; ?>"required></b>
</div>
<div class="form-group">
<label for="">Address</label>
<b><input type="text" name="alamat_perusahaan" class="form-control" placeholder="alamat"
value="<?php echo $service['alamat_perusahaan']; ?>"required></b>
</div>
<div class="form-group">
<label for="">PIC(Engineering)</label>
<input type="text" name="pic_perusahaan" class="form-control" placeholder="pic"
value="<?php echo $service['pic_perusahaan']; ?>">
</div>
<div class="form-group">
<label for="">Phone Number</label>
<input type="text" name="telepon_pic" class="form-control" placeholder="phone number"
value="<?php echo $service['telepon_pic']; ?>">
</div>
<div class="form-group">
<label for="">Fax</label>
<input type="text" name="fax" class="form-control" placeholder="fax"
value="<?php echo $service['fax']; ?>">
</div>
<div class="form-group">
<label for="">HP</label>
<input type="text" name="hp" class="form-control" placeholder="hp"
value="<?php echo $service['hp']; ?>">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" name="email_pic" class="form-control" placeholder="email pic"
value="<?php echo $service['email_pic']; ?>">
</div>
</fieldset>
<fieldset>
<div class="form-group">
<label for="">Vendor1</label>
<select name="id_vendor" class="form-control">
<option value="0">Non Vendor</option>
<?php
foreach ($vendor as $c)
{ ?>
<?php $sel = ($c->id_vendor==$service['id_vendor']) ? 'selected' :''; ?>
<option value="<?php echo $c->id_vendor;?>" <?php echo $sel;?>><?php echo $c->nama_vendor;?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="">PIC</label>
<input type="text" name="pic_vendor" class="form-control" placeholder="pic vendor"
value="<?php echo $service['pic_vendor']; ?>">
</div>
<div class="form-group">
<label for="">Media</label>
<input type="text" name="media_vendor" class="form-control" placeholder="media"
value="<?php echo $service['media_vendor']; ?>">
</div>
<div class="form-group">
<label for="">Capacity</label>
<input type="text" name="kapasitas_vendor" class="form-control" placeholder="kapasitas vendor"
value="<?php echo $service['kapasitas_vendor']; ?>">
</div>
<div class="form-group">
<label for="">Phone</label>
<input type="text" name="telepon_vendor" class="form-control" placeholder="telepon vendor"
value="<?php echo $service['telepon_vendor']; ?>">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" name="email_vendor" class="form-control" placeholder="email vendor"
value="<?php echo $service['email_vendor']; ?>">
</div>
<hr></hr>
<div class="form-group">
<label for="">Vendor2</label>
<select name="id_vendor2" class="form-control">
<option value="0">Non Vendor</option>
<?php
foreach ($vendor2 as $c)
{ ?>
<?php $sel = ($c->id_vendor2==$service['id_vendor2']) ? 'selected' :''; ?>
<option value="<?php echo $c->id_vendor2;?>" <?php echo $sel;?>><?php echo $c->nama_vendor2;?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="">PIC</label>
<input type="text" name="pic_vendor2" class="form-control" placeholder="pic vendor"
value="<?php echo $service['pic_vendor2']; ?>">
</div>
<div class="form-group">
<label for="">Media</label>
<input type="text" name="media_vendor2" class="form-control" placeholder="media"
value="<?php echo $service['media_vendor2']; ?>">
</div>
<div class="form-group">
<label for="">Capacity</label>
<input type="text" name="kapasitas_vendor2" class="form-control" placeholder="kapasitas vendor"
value="<?php echo $service['kapasitas_vendor2']; ?>">
</div>
<div class="form-group">
<label for="">Phone</label>
<input type="text" name="telepon_vendor2" class="form-control" placeholder="telepon vendor"
value="<?php echo $service['telepon_vendor2']; ?>">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" name="email_vendor2" class="form-control" placeholder="email vendor"
value="<?php echo $service['email_vendor2']; ?>">
</div>
</fieldset>
<fieldset>
<br>
<p>*Pastikan semua form sudah terisi dengan benar</p>
<button id="submit" class="btn btn-success">Update Service</button>
</fieldset>
</div>
</div>
</div>
</div>
<script>
$( function() {
var $signupForm = $( '#myForm' );
$signupForm.validate({errorElement: 'em'});
$signupForm.formToWizard({
submitButton: 'submit',
nextBtnName: 'Selanjutnya',
prevBtnName: 'Sebelumnya',
nextBtnClass: 'btn btn-primary btn-flat next',
prevBtnClass: 'btn btn-default btn-flat prev',
buttonTag: 'button',
validateBeforeNext: function(form, step) {
var stepIsValid = true;
var validator = form.validate();
$(':input', step).each( function(index) {
var xy = validator.element(this);
stepIsValid = stepIsValid && (typeof xy == 'undefined' || xy);
});
return stepIsValid;
},
progress: function (i, count) {
$('#progress-complete').width(''+(i/count*100)+'%');
}
});
});
</script>
Tables:
This is my **Table contact**
# Name type
1 id_kontak int(11)
2 cid varchar(10)
3 nama_perusahaan varchar(150)
4 destination varchar(30)
5 alamat_perusahaan varchar(200)
6 pic_perusahaan varchar(200)
7 telepon_pic varchar(30)
8 fax int(15)
9 hp int(15)
10 email_pic (15)
This my **table vendor**
# Name type
1 id_vendor int(11)
2 pic_vendor varchar(200)
3 telepon_vendor varchar(100)
4 media_vendor varchar(200)
5 kapasitas_vendor varchar(200)
6 email_vendor varchar(200)
This my **table vendor2**
# Name type
1 id_vendor2 int(11)
2 pic_vendor2 varchar(200)
3 telepon_vendor2 varchar(100)
4 media_vendor2 varchar(200)
5 kapasitas_vendor2 varchar(200)
6 email_vendor2 varchar(200)
I want to update one time so that the 3 data tables are updated as well because the table id is in another table too.Thank you for the help
What i understand from your code is that you want to update 3 tables using single query. In that case you can use following code.
$this->db->set('a.cid', $this->input->post('cid'));
$this->db->set('a.nama_perusahaan', $this->input->post('nama_perusahaan'));
$this->db->set('b.pic_vendor', $this->input->post('pic_vendor'));
$this->db->set('b.media_vendor', $this->input->post('media_vendor'));
$this->db->set('c.pic_vendor2', $this->input->post('pic_vendor2'));
$this->db->set('c.companyaddress', $this->input->post('pic_vendor2'));
$this->db->where('a.id', 1);
$this->db->where('a.id = b.id');
$this->db->where('a.id = c.id');
$this->db->update('table as a, table2 as b',table3 as c);
But in na More Professional way you should use Codeigniter Transactions. If any of the query fails the other one gets rolled back.
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();

Crud in Codeigniter not affected when Update to database

I've been working to make some CRUD I get nothing wrong.
But when I update data I got nothing affected in database and here my code
Table : Song
Column :
id_song,
id_album,
song,
author,
composer
Code for my Model is below:
function Tampil_song_admin(){
return $this->db->get('songs');
}
function Input_song($data,$table){
$this->db->insert($table,$data);
}
function edit_song($where,$table){
return $this->db->get_where($table,$where);
}
function update_song($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
function delete_song($where,$table){
$this->db->where($where);
$this->db->delete($table);
}
View:
<?php foreach($songs as $data){ ?>
<form action="<?php echo base_url('/index.php/Admin_song/update_song'); ?>" method="post">
<h4 class="header-title m-t-0 m-b-30"></h4>
<div class="form-group">
<label for="userName">id_songs </label>
<input type="text" name="id_song" parsley-trigger="change" required
value="<?php echo $data->id_song; ?>"
class="form-control" disabled="" id="userName">
</div>
<div class="form-group">
<label for="userName">id_album</label>
<input type="text" name="id_album" parsley-trigger="change" required
value="<?php echo $data->id_album; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Songs</label>
<input type="text" name="song" parsley-trigger="change" required
value="<?php echo $data->song; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Author</label>
<input type="text" name="author" parsley-trigger="change" required
value="<?php echo $data->author; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Composer</label>
<input type="text" name="composer" parsley-trigger="change" required
value="<?php echo $data->composer; ?>" class="form-control" id="userName">
</div>
<button type="submit" class="btn btn-success waves-effect w-md waves-light m-b-5">Update</button>
<button type="button" class="btn btn-danger waves-effect w-md waves-light m-b-5">Reset</button>
</form>
<?php } ?>
And this is for Controller:
function index(){
$data['songs'] = $this->m_admin_data->Tampil_song_admin()->result();
$this->load->view('admin/v_admin_song',$data);
}
//Tambah Data
function add_song(){
$this->load->view('admin/v_admin_song_add');
}
function proses_tambah(){
$id_album = $this->input->post('id_album');
$song = $this->input->post('song');
$author = $this->input->post('author');
$composer = $this->input->post('composer');
$data = array(
'id_album' => $id_album,
'song' => $song,
'author' => $author,
'composer' => $composer
);
$this->m_admin_data->Input_song($data,'songs');
redirect('Admin_song/index');
}
//Update/Edit Data
function edit_song($id){
$where = array('id_song' => $id);
$data['songs'] = $this->m_admin_data->edit_song($where,'songs')->result();
$this->load->view('admin/v_admin_song_edit',$data);
}
function update_song(){
$id = $this->input->post('id_song');
$id_album = $this->input->post('id_album');
$song = $this->input->post('song');
$author = $this->input->post('author');
$composer = $this->input->post('composer');
$data = array(
'id_album' => $id_album,
'song' => $song,
'author' => $author,
'composer' => $composer
);
$where = array(
'id_song' => $id
);
$this->m_admin_data->update_song($where,$data,'songs');
redirect('Admin_song/index');
}
//Hapus Data
function delete_song($id){
$where = array('id_song' => $id);
$this->m_admin_data->delete_song($where,'songs');
redirect('Admin_song/index');
}
See I got nothing wrong but when I edit and try to update some data its not affected.
Hope this will help you :
You have disabled you id_song input type in your form just remove disabled attr from it
Because of this u r unable to get id_song post value in your update_song method in turn not getting in your where clause so
id_song input Should be like this :
<div class="form-group">
<label for="userName">id_songs </label>
<input type="text"
name="id_song"
value="<?php echo $data->id_song; ?>"
class="form-control" id="userName">
</div>
Or just make it hidden if you don't want to show it like this :
<input type="hidden"
name="id_song"
parsley-trigger="change" required
value="<?php echo $data->id_song; ?>"
class="form-control" id="userName">

Adding outputted table row to another table using codeigniter

Outputted a row from a table ,am now trying to add some of the contents to another table using a form which is inside the outputted row but its not inserted to the database. am not getting an error.
Here is my view
<?php
foreach ($h->result() as $row)
{?>
<div class="row invoice-info">
<div class="col-sm-1 invoice-col">
</div>
<div class="col-sm-3 invoice-col">
<img src="<?php echo base_url()?>/res/images/goods/1.png">
</div>
<div class="col-sm-4 invoice-col">
<address>
Description: <?php echo $row->description;?><br>
Location Address: <?php echo $row->l_area;?><br>
Destination Address: <?php echo $row->d_area;?><br>
Date: <?php echo $row->dom;?><br>
Time: <?php echo $row->tom;?>
</address>
</div>
<div class="col-sm-2 invoice-col">
<address>
</address>
</div><!-- /.col -->
<div class="col-sm-2 invoice-col">
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</div>
</div>
My model
function bid($data){
$query=$this->db->update('bids',$data);
return $query;
}
My controller
public function bid(){
$this->load->database();
$this->load->model('Truckeraccount_model');
$data['a']=$this->Truckeraccount_model->accepted_bid();
$data['b']=$this->Truckeraccount_model->picked_loads();
$data['h']=$this->Truckeraccount_model->loads();
$data['g']=$this->Truckeraccount_model->notification();
$data['i']=$this->Truckeraccount_model->return_loads();
$data['accepted_return_loads']=$this->Truckeraccount_model->accepted_return_loads();
$data['bid_amount']=$this->Truckeraccount_model->bid_amount(); $this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('bid_amount', 'bid_amount', 'required|min_length[1]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('truckeraccount_view',$data);
$this->load->view('footer');
} else {
$data = array(
'truckerid' => $this->input->post('truckerid'),
'luggage_id' => $this->input->post('luggage_id'),
'bid_amount' => $this->input->post('bid_amount'),
);
$this->Truckeraccount_model->bid($data);
$data['message'] = '';
redirect('truckeraccount_ctrl/');
}
}
<form action="<?php echo site_url('truckeraccount_ctrl/bid'); ?>" method="post">
<input type="hidden" class="form-control" name="truckerid" value="<?php
$truckerid = $this->session->userdata('truckerid');
echo $truckerid; ?>" required>
<input type="hidden" class="form-control" name="luggage_id" value="<?php echo $row->luggage_id;?>" placeholder="Bid">
<input type="text" class="form-control" name="bid_amount" placeholder="Bid">
<button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button>
</form>

wrong <select> value when editing php form

I really hope you can help me.
I've done a form, where I can opt if a client I'm adding to the database is "Active or "Inactive", using a dropdown select box.
My code saves all the data correctly to the datbase, but when I want to edit the client, the option displays always as "Active", ignoring the value from the database.
I have 2 files:
edita_clientes.php - the form where I can edit the clients values
salvar_edicao.php - the file that saves the edition.
Here are the codes:
edita_clientes.php:
<?php
#ini_set('display_errors', '1');
error_reporting(E_ALL);
$id = $_GET["id_cliente"];
settype($id, "integer");
mysql_connect("localhost", "root", "");
mysql_select_db("sistema");
$resultado = mysql_query("select * from tabela where id_cliente = $id");
$dados = mysql_fetch_array($resultado);
mysql_close();
?>
<form id="edita_pj" name="edita_pj" method="post" action="salvar_edicao.php">
<input type="hidden" name="id_cliente" id="id_cliente" value="<?php echo $id;?>" />
<div class="box-body">
<div class="form-group">
<label>Razão Social</label>
<input type="text" name="razao" id="razao" class="form-control" value="<?php echo $dados["razao"];?>" />
</div>
<div class="form-group">
<label>Nome Fantasia</label>
<input type="text" name="fantasia" id="fantasia" class="form-control" value="<?php echo $dados["fantasia"];?>" />
</div>
</div>
<div class="box-body">
<div class="form-group">
<label>CNPJ</label>
<input type="text" name="cnpj" id="cnpj" class="form-control" data-inputmask='"mask": "999.999.999-99"' data-mask value="<?php echo $dados["cnpj"];?>">
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Logradouro</label>
<input type="text" name="logradouro" id="logradouro" class="form-control" value="<?php echo $dados["logradouro"];?>">
</div>
<div class="col-xs-3">
<label>Número</label>
<input type="text" name="numero" id="numero" class="form-control" value="<?php echo $dados["numero"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Bairro</label>
<input type="text" name="bairro" id="bairro" class="form-control" value="<?php echo $dados["bairro"];?>">
</div>
<div class="col-xs-3">
<label>CEP</label>
<input type="text" name="cep" id="cep" class="form-control" data-inputmask='"mask": "99999-999"' data-mask value="<?php echo $dados["cep"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-10">
<label>Cidade</label>
<input type="text" name="cidade" id="cidade" class="form-control" value="<?php echo $dados["cidade"];?>">
</div>
<div class="col-xs-2">
<label>UF</label>
<input type="text" name="uf" id="uf" class="form-control" value="<?php echo $dados["uf"];?>">
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>E-mail</label>
<input type="text" name="email" id="email" class="form-control" value="<?php echo $dados["email"];?>">
</div>
<div class="col-xs-3">
<label>Telefone</label>
<input type="text" name="telefone" id="telefone" class="form-control" data-inputmask='"mask": "(99) 9999.9999"' data-mask value="<?php echo $dados["telefone"];?>"/>
</div>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-xs-9">
<label>Contato</label>
<input type="text" name="contato" id="contato" class="form-control" value="<?php echo $dados["contato"];?>">
</div>
<div class="col-xs-3">
<label>Estado</label>
<select class="form-control" name="estado" id="estado" value=""><?php echo $dados["estado"];?>
<option>Ativo</option>
<option>Inativo</option>
</select>
</div>
</div>
<div class="form-group">
<label>Observações</label>
<textarea class="form-control" name="obs" id="obs" rows="6" ><?php echo $dados["obs"];?>
</textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" name="Submit" class="btn btn-primary">Salvar</button>
</div>
</form>
salvar_edicao.php:
<?php
#ini_set('display_errors', '1');
error_reporting(E_ALL);
$razao = $_POST["razao"];
$fantasia = $_POST["fantasia"];
$cnpj = $_POST["cnpj"];
$logradouro = $_POST["logradouro"];
$numero = $_POST["numero"];
$bairro = $_POST["bairro"];
$cep = $_POST["cep"];
$cidade = $_POST["cidade"];
$uf = $_POST["uf"];
$email = $_POST["email"];
$telefone = $_POST["telefone"];
$contato = $_POST["contato"];
$estado = $_POST["estado"];
$obs = $_POST["obs"];
$id = $_POST["id_cliente"];
mysql_connect("localhost", "root", "");
mysql_select_db("sistema");
mysql_query("UPDATE tabela SET razao = '$razao', fantasia = '$fantasia', cnpj = '$cnpj', logradouro = '$logradouro', numero='$numero', bairro='$bairro', cep='$cep', cidade = '$cidade', uf='$uf', email = '$email', telefone = '$telefone', contato = '$contato', estado = '$estado', obs = '$obs' WHERE tabela.id_cliente = $id");
mysql_close();
header("Location: consulta.php");
?>
You need to add 'selected' to the option that you want to be selected, based on a value from the form/db. Here is an example using $value as the option value that you want selected.
<select class="form-control" name="estado" id="estado">
<option <?php echo $value == 'Ativo' ? selected : '' ?>>Ativo</option>
<option <?php echo $value == 'Inativo' ? selected : '' ?>>Inativo</option>
</select>
Also, your <select> tag does not require a 'value' element..
The HTML select element does not have a value attribute. For the select to do what you want you need to add the selected attribute to the option you want selected. It's always showing as 'Active' because that's the first option and it is the default.
The resulting post-php HTML will need to look something like this stripped back example for 'Inactive' to be selected.
<select>
<option>Active</option>
<option selected>Inactive</option>
</select>
Thank's for all the help!
The solution I've found was:
<?php
$resultado = mysql_query("select * from tabela where id_cliente = $id");
$dados = mysql_fetch_array($resultado);
$query = mysql_query("SELECT * FROM estado");
?>
And the html part:
<select class="form-control" name="estado" id="estado">
<option selected="selected"><?php echo $dados["estado"];?></option>
<option value="Ativo">Ativo</option>
<option value="Inativo">Inativo</option>
</select>

Unable to retrieve ID to update in database

I am unable to identify the mistake
model (here I am matching the id to update)
function get_account_record($a_id)
{
$this->db->where('a_id', $a_id);
$this->db->from('account_info');
$query = $this->db->get();
return $query->result();
}
controller (receiving everything via post accept the ID i-e a_id )
function update($a_id)
{
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
form Validation
if ($this->form_validation->run() == FALSE)
{
$this->load->view('viewUpdate', $data);
}
else {
$data = array(
'a_name' => $this->input->post('a_name'),
'a_website' => $this->input->post('a_web'),
'a_billingStreet' => $this->input->post('a_billingStreet'),
'a_mobile' => $this->input->post('a_mobile'),);
$this->db->where('a_id', $a_id);
$this->db->update('account_info', $data);
display success message
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Successfully Updated!</div>');
//redirect('salesforce' . $a_id);
}
}
view (ID and Name)
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_id" class="control-label">Account ID</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" disabled="disabled" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_name" class="control-label">Account Name</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_name" name="a_name" placeholder="Enter Account Name" type="text" class="form-control" value="<?php echo $view[0]->a_name; ?>" />
<span class="text-danger"><?php echo form_error('a_name'); ?></span>
</div>
</div>
</div>
If you don't want to allow the user to edit then add readonly instead of disabled attribute
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" readonly="readonly" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
If you want to disable then add the hidden element
<input id="a1_id" name="a1_id" type="hidden" value="<?php echo $a_id;?>" />
Controller Function
function update($a_id)
{
//read the value using input library
$a_id = $this->input->post('a_id');
//$a1_id = $this->input->post('a1_id'); your hidden element value can be get here.
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
}
My problem Solved after editing the where clause in update query
$this->db->where('a_id', $_POST['a_id']);

Categories