Codeigniter - Show and Edit Last Id recorded - php

I want to show last insert data according to packageId on package page. For example, after I edit Gold Package, System adds new data of Gold Package and showing the last insert of Gold Package on Package page. But I list data according to packageId and How can I list them according packageId? How can I edit last insert Id of Gold Package again?
Controller Package:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Package extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('service_model');
}
public function index()
{
$this->lang->load('content', $this->session->userdata('userLang'));
$viewData['packs'] = $this->db->get("package")->result();
$this->load->view('package', $viewData);
}
public function edit($packageId)
{
$this->lang->load('content', $this->session->userdata('userLang'));
$viewData['packages'] = $this->db->where("packageId", $packageId)->get("package")->result();
$viewData['packs'] = $this->db->get("package")->result();
$this->load->view('package_edit', $viewData);
}
public function update($packageId) {
$this->lang->load('content', $this->session->userdata('people_lang'));
$viewData['services'] = $this->service_model->update_services($packageId);
$this->load->view('package',$viewData);
$data = array (
"packageEditUser" => $this->input->post("packageEditUser"),
"packageRev" => $this->input->post("packageRev"),
"packageNameEn" => $this->input->post("packageNameEn"),
"packagePrice" => $this->input->post("packagePrice"),
"packagePriceCut" => $this->input->post("packagePriceCut"),
"packageActive" => $this->input->post("packageActive"),
"packageEditDate" => date('Y-m-d H:i:s'),
);
$update = $this->db->where("packageId", $packageId)->insert("package", $data);
if($update) {
redirect(base_url("package"));
}else {
echo "Hata!";
}
}
}
?>
Package View:
<h1> PACKAGES </h1>
<p>Choose Your Special Package Plan. </p>
</div>
<?php foreach($packs as $get) { ?>
<div class="col-lg-3 col-sm-3">
<div class="pricing-table <?php if ($get->packageNameEn == 'Platinum') { echo 'most-popular'; } ?>">
<div class="pricing-head">
<h1> <?php echo $get->packageNameEn; ?> </h1>
<h5><del>€ <?php echo $get->packagePrice ?></del></h5>
<h2><span class="note">€</span><?php echo $get->packagePriceCut ?> </h2>
</div>
<ul class="list-unstyled">
<li>8 hours coverage</li>
<li>500 digital images</li>
<li>100 A3 Hard Copy</li>
<li>Bridal portrait with 11X14</li>
<li>Engagement portrait with 11X14</li>
<li>Income Tax included</li>
</ul>
<div class="price-actions">
<a class="btn" href="javascript:;">Get Now</a>
<a class="btn" href="<?php echo base_url("package/edit/$get->packageId"); ?>">Edit</a>
</div>
</div>
</div>
<?php } ?>
Edit Package View:
<form class="cmxform form-horizontal tasi-form" id="signupForm" enctype="multipart/form-data" method="post" action="<?php echo base_url("package/update/$get->packageId"); ?>">
<?php } ?>
<!-- Hidden Classes -->
<div class="form-group" hidden>
<label class="col-sm-2 col-sm-2 control-label">The User Who Edit</label>
<div class="col-sm-10">
<input type="text" name="packageEditUser" class="form-control" value="<?php echo $this->session->userdata('people_id'); ?>" readonly>
</div>
</div>
<div class="form-group" hidden>
<label class="col-sm-2 col-sm-2 control-label">Rev Id</label>
<div class="col-sm-10">
<?php foreach($packages as $get) { ?>
<input type="text" name="packageRev" class="form-control" value="<?php echo $get->packageId; ?>" readonly>
<?php } ?>
</div>
</div>
<!-- / Hidden Classes -->
<div class="form-group ">
<label for="username" class="control-label col-lg-2">Package Name: </label>
<div class="col-lg-10">
<?php foreach($packages as $get) { ?>
<input class=" form-control" type="text" value="<?php echo $get->packageNameEn; ?>" readonly />
<?php } ?>
</div>
</div>
<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Package Name <span class="text-danger">*</span></label>
<div class="col-lg-9">
<?php foreach($packages as $get) { ?>
<input type="text" name="packageNameEn" class="form-control" placeholder="Package Name" value="<?php echo $get->packageNameEn; ?>">
<?php }?>
</div>
</div>
<!-- /basic select -->
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2" for="inputSuccess">Services:</label>
<div class="col-lg-6">
<div class="checkboxes">
<label class="label_check" for="checkbox-01">
<input name="sample-checkbox-01" id="checkbox-01" value="1" type="checkbox" /> I agree to the terms & conditions.
</label>
<label class="label_check" for="checkbox-02">
<input name="sample-checkbox-02" id="checkbox-02" value="1" type="checkbox" /> Please send me regular updates. </label>
<label class="label_check" for="checkbox-03">
<input name="sample-checkbox-02" id="checkbox-03" value="1" type="checkbox" /> This is nice checkbox.</label>
<label class="label_check" for="checkbox-04">
<input name="sample-checkbox-04" id="checkbox-04" value="1" type="checkbox" /> I agree to the terms & conditions.
</label>
<label class="label_check" for="checkbox-05">
<input name="sample-checkbox-05" id="checkbox-05" value="1" type="checkbox" /> Please send me regular updates. </label>
<label class="label_check" for="checkbox-06">
<input name="sample-checkbox-06" id="checkbox-06" value="1" type="checkbox" /> This is nice checkbox.</label>
<label class="label_check" for="checkbox-07">
<input name="sample-checkbox-07" id="checkbox-07" value="1" type="checkbox" /> I agree to the terms & conditions.
</label>
<label class="label_check" for="checkbox-08">
<input name="sample-checkbox-08" id="checkbox-08" value="1" type="checkbox" /> Please send me regular updates. </label>
<label class="label_check" for="checkbox-09">
<input name="sample-checkbox-09" id="checkbox-09" value="1" type="checkbox" /> This is nice checkbox.</label>
<label class="label_check" for="checkbox-10">
<input name="sample-checkbox-10" id="checkbox-10" value="1" type="checkbox" /> This is nice checkbox.</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<h5>Package Price:</h5>
<div class="input-group m-bot15">
<span class="input-group-addon"><i class="fas fa-euro"></i></span>
<?php foreach($packages as $get) { ?>
<input type="text" name="packagePrice" data-mask="€ 999.99" class="form-control" value="<?php echo $get->packagePrice; ?>">
<?php }?>
</div>
</div>
<div class="col-lg-6">
<h5>Discounted Price:</h5>
<div class="input-group m-bot15">
<span class="input-group-addon"><i class="fas fa-euro"></i></span>
<?php foreach($packages as $get) { ?>
<input type="text" name="packagePriceCut" data-mask="€ 999.99" class="form-control" value="<?php echo $get->packagePriceCut; ?>">
<?php }?>
</div>
</div>
</div>
<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Status <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="packageActive" class="form-control">
<?php foreach($packages as $get) { ?>
<option value="<?php echo $get->packageActive; ?>" readonly><?php if($get->packageActive == 1) {echo 'Active';} else {echo 'Deactive';} ?></option>
<?php }?>
<option value="1">Active</option>
<option value="2">Deactive</option>
</select>
</div>
</div>
<!-- /basic select -->
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-success" type="submit">Edit Package</button>
<button class="btn btn-default" type="reset">Reset</button>
</div>
</div>
</form>
Database:
CREATE TABLE `package` (
`packageId` int(11) NOT NULL AUTO_INCREMENT,
`packageRev` int(11) DEFAULT NULL COMMENT 'Revizyon',
`packageNameEn` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`packageNameAr` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`packageService` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '',
`packagePrice` decimal(6,2) DEFAULT NULL COMMENT '',
`packagePriceCut` decimal(6,2) DEFAULT NULL COMMENT '',
`packageAddUser` int(11) DEFAULT NULL COMMENT '',
`packageAddDate` datetime DEFAULT NULL COMMENT '',
`packageEditUser` int(11) DEFAULT NULL,
`packageEditDate` datetime DEFAULT NULL,
`packageActive` tinyint(4) DEFAULT NULL COMMENT '',
PRIMARY KEY (`packageId`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

If You want to don't lost data after its update. You must insert the data such new data no update and for selecting the latest updated data you must use time, create a two new column update_id, current_time. like this.
After that If You want to show the last data updated use Current_time to select that like this
$this->db->order_by('Current_time','DESC');
$data = $this->db->get('comment_update',1)->result_array();

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();

Why my post method is not working properly

my method $_POST works fine in my other form but in here my $_POST not working.
Here is my form
<h3 class="page-title">Medicine Inventory: Add Stock</h3>
<form method="POST" action="PHP/action_insertaddstock.php" accept-charset="UTF-8">
<div class="panel panel-default">
<div class="panel-heading">
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 form-group">
<label for="medicine_id" class="control-label">Medicine ID (Required) :</label>
<select class="form-control select2" required="" id="medicine_id" name="medicine_id">
<option value="" selected="selected">-- Please select --</option>
<?php foreach($medicines as $val) : ?>
<option value="<?php echo $val['id']; ?>"><?php echo $val['name']; ?></option>
<?php endforeach; ?>
</select>
<p class="help-block"></p>
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
<label for="qty" class="control-label">Quantity (Required) :</label>
<input class="form-control" placeholder="" required="" name="qty" type="number" id="qty">
<p class="help-block"></p>
</div>
</div>
<div class="row">
<div class="col-xs-12 form-group">
<label for="xdate" class="control-label">Expiry Date (Required) :</label>
<input class="form-control " placeholder="" required="" name="xdate" id="xdate">
<p class="help-block"></p>
</div>
</div>
</div>
</div>
<button class="btn btn-success" type="submit" name="submit" >Save</button>
</form>
and my action is here
<?php
if (isset($_POST['submit'])) {
include_once('CONFIG/config.php');
include_once('CONFIG/db.php');
$Med_id = $_POST['medicine_id'];
$Qty = $_POST['qty'];
$Xdate = $_POST['xdate'];
$Timestamp = date("Y-m-d H:i:s");
if(empty($Med_id) || empty($Qty) || empty($Xdate) || empty($Timestamp)){
header("Location: ../added_stock.php?add=empty");
exit();
}else{
$sql = "INSERT INTO medicines_addeds (qty, xdate, created_at,
med_id) VALUES ('$Qty', '$Xdate', '$Timestamp', '$Med_id')";
mysqli_query($conn, $sql);
header("Location: ../added_stock.php?add=success");
exit();
}
} else {
header("Location: ../MIAdd_medicine.php");
exit();
}
the query works fine because i tried to put value in the variables and it insert in my database but when i tried the $_POST it does nothing it run succesfully but my database is empty. Please Help me
table structure is here
1 idPrimary int(11) No None AUTO_INCREMENT
2 qty int(11) Yes NULL
3 xdate date Yes NULL
4 created_at timestamp Yes NULL
5 edited_at timestamp Yes NULL
6 deleted_at timestamp Yes NULL
7 med_id int(11) Yes NULL
Parse your xdate with
$Xdate = date('y/m/d', strtotime($_POST(xdate))

Codeigniter - Forms with multiple dropzones for Picture Gallery & PDF files

Hi I'm working on a real estate project. I have a form which has some inputs & then 3 forms incorporated in the 1st form which have the dropzone class for a simple picture, a picture gallery & pdf files. I tried a lot of techniques but couldn't get the dropzones to work with the main form.
My Issues:
Dropzone sends the inputs & picture before submit button gets hit.
If after that, the submit button gets clicked then some inputs get null & 2 records get inserted.
If I remove or hide the submit button or change it to anchor tag for redirecting then the picture doesn't get uploaded.
Listings_controller:
function insert_listing(){
$pass = rand(0,999);
$data_seller = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'cell' => $this->input->post('cell'),
'sms' => $this->input->post('sms'),
'pass' => MD5($pass),
'status' => 'seller',
'last_date' => date('Y-m-d')
);
$result1 = $this->users_model->add_seller($data_seller);
$data_listing = array(
'code' => $this->input->post('code'),
'address' => $this->input->post('address'),
'city' => $this->input->post('city'),
'state' => $this->input->post('state'),
'zipcode' => $this->input->post('zipcode'),
'process' => $this->input->post('process'),
'step' => $this->input->post('step'),
'price' => $this->input->post('price'),
'agent_id' => $this->input->post('agent'),
'seller_id' => $this->db->insert_id()
);
$result2 = $this->listings_model->add_listing($data_listing);
if($result2){
$this->session->set_flashdata('success','Listing Added Successfully');
}else{
$this->session->set_flashdata('error','Listing Already Exists !');
}
//Dropzone - Pic
if(!empty($_FILES)){
$tempFile = $_FILES['file']['tmp_name'];
$fileName = substr(sha1(rand(000,9999)),0,7).$_FILES['file']['name'];
$targetPath = getcwd().'/assets/admin/images/image-gallery/';
$targetFile = $targetPath.$fileName;
move_uploaded_file($tempFile,$targetFile);
$listing_id = $result2['id'];
$data_listing_pic['listing_id'] = $listing_id;
$data_listing_pic['pic'] = $fileName;
$result3 = $this->listings_model->add_listing_pic($data_listing_pic);
}
redirect('admin/listings');
}
Users_model:
//Seller Add
function add_seller($data_seller){
//Check Duplicate
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('email',$data_seller['email']);
$query = $this->db->get();
if($query->num_rows() > 0){
$seller_id = $query->result_array();
$data_seller['id'] = $seller_id[0]['id'];
//Update Info
$this->db->where('email',$data_seller['email']);
$this->db->update($this->table,$data_seller);
return $data_seller;
}else{
//INSERT Seller
$result = $this->db->insert($this->table,$data_seller);
//GET Last Inserted ID
$seller_id = $this->db->insert_id();
$data_seller['id'] = $seller_id;
return $data_seller;
}
}
Listings_model:
//Listing Add
function add_listing($data_listing){
$result = $this->db->insert($this->table,$data_listing);
$listing_id = $this->db->insert_id();
$data_listing['id'] = $listing_id;
return $data_listing;
}
//Listing Pic Add
function add_listing_pic($data_listing_pic){
$result = $this->db->insert($this->pics_table,$data_listing_pic);
return $result;
}
My View:
<form action="<?php echo base_url();?>admin/insert_listing" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<input type="hidden" name="code" value="<?php echo $listing_code; ?>" />
<div class="row">
<div class="col-lg-6">
<strong>Property Information:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="address" autofocus required />
<label class="form-label">Street Address:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="city" required />
<label class="form-label">City/Town:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="state" required />
<label class="form-label">State:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="zipcode" required />
<label class="form-label">Zip Code:</label>
</div>
</div>
</div>
<div class="col-lg-6">
<strong>Seller Information:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="name" required />
<label class="form-label">Full Name(s):</label>
</div>
<br>
<div class="form-line">
<input type="email" class="form-control" name="email" required />
<label class="form-label">Seller's Email:</label>
</div>
<br>
<div class="form-line">
<input type="text" class="form-control" name="cell" required />
<label class="form-label">Cell Phone Number:</label>
</div>
<br>
<div class="demo-checkbox">
<input type="checkbox" id="md_checkbox_26" name="sms" class="filled-in chk-col-blue" checked />
<label for="md_checkbox_26">Send status updates via SMS</label>
</div>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-6">
<strong>Listing Agent:</strong>
<br /><br />
<select class="form-control show-tick" name="agent" required >
<option value="">-- Please choose from the drop down --</option>
<?php for($j=0; $j < count($agent_list); $j++){ ?>
<option value="<?php echo $agent_list[$j]['id']; ?>" ><?php echo $agent_list[$j]['name']; ?></option>
<?php } ?>
</select>
<br />
</div>
<div class="col-lg-6">
<strong>Listing Status Information:</strong>
<br /><br />
<select class="form-control show-tick" name="process" required >
<option value="">-- Please choose from the drop down --</option>
<option value="Listing Process">Listing Process</option>
</select>
<br /><br />
<select class="form-control show-tick" name="step" required >
<option value="">-- Please choose from the drop down --</option>
<option value="1">Step 1: The Prep</option>
<option value="2">Step 2: File Set Up</option>
<option value="3">Step 3: Pre-listing Checklists</option>
<option value="4">Step 4: Sign Installation</option>
<option value="5">Step 5: Feature Sheets</option>
<option value="6">Step 6: Open Houses</option>
<option value="7">Step 7: You Received an Offer!</option>
</select>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-12">
<strong>Photo and Price:</strong>
<br><br>
<div class="form-group form-float form-group-lg">
<div class="form-line">
<input type="text" class="form-control" name="price" required />
<label class="form-label">Listing Price: $</label>
</div>
</div>
<form action="<?php echo base_url();?>admin/insert_listing_pdf" id="PDFUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop the Photo here or Click to Upload.</h3>
<em>(It is recommended to upload the most attractive photo that will catch the user's eye)</em>
</div>
</form>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-6">
<strong>Upload Gallery Photos:</strong>
<br><br>
<form action="<?php echo base_url();?>admin/insert_listing_gallery" id="GalleryUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop files here or click to upload.</h3>
<em>(Upload all the gallery photos of the property in this section)</em>
</div>
</form>
</div>
<div class="col-lg-6">
<strong>Upload PDF Files:</strong>
<br><br>
<form action="<?php echo base_url();?>admin/insert_listing_pdf" id="PDFUpload" class="dropzone dz-clickable" method="POST" enctype="multipart/form-data">
<div class="dz-message">
<div class="drag-icon-cph">
<i class="material-icons">touch_app</i>
</div>
<h3>Drop files here or click to upload.</h3>
<em>(Upload all the PDF files related to the property in this section)</em>
</div>
</form>
</div>
</div>
<hr />
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-primary btn-lg waves-effect">ADD LISTING</button>
</div>
</div>
</form>

Inserting data into MySQL server

I'm doing a e-commerce admin panel and I need a quick script for inserting data into MySQL. Here's what i've done and it does nothing.
<form action="#" id="form_sample_1" class="form-horizontal" method="post">
<div class="control-group">
<label class="control-label">Package Name<span class="required">*</span></label>
<div class="controls">
<input type="text" name="pkg_name" data-required="1" class="span6 " value=""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Package Price <span class="required">*</span><small>(In Dollars)</small></label>
<div class="controls">
<input name="pkg_price" type="number" class="span6 " value=""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Package Contains</label>
<div class="controls">
<input name="pkg_contains" type="text" class="span6 " value=""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Your Password</label>
<div class="controls">
<input name="sifre" type="password" class="span6 " value=""/>
</div>
</div>
<div class="form-actions">
<button type="button"name="btn" class="btn btn-primary">Send request to server.</button>
</div>
</form>
<!-- END FORM-->
</div> <!--widget box light-grey end-->
<!-- Mass PHP starts here! -->
<?php
echo mysql_error();
include("include/baglan.php");
// set posts here.
$_POST['pkg_name'] = $pkg_name;
$_POST['pkg_price'] = $pkg_price;
$_POST['pkg_contains'] = $pkg_contains;
$sifre = mysql_real_escape_string(md5($_POST['sifre']));
if($_POST['btn'] and $_POST["sifre"] = $sifre){
mysql_query("INSERT INTO packages (pkg_name, pkg_price,pkg_contains) VALUES $pkg_name $pkg_price $pkg_contains");
echo "Success.";
}
else {
echo mysql_error();}
It returns nothing! I've re-written all code but nothing! please help me. The databae variables are;
id, auto incerment
pkg_name text
pkg_price int
pkg_contains mediumtext
Assign variable name should be the left side.
// set posts here.
$pkg_name=$_POST['pkg_name'];
$pkg_price=$_POST['pkg_price'];
$pkg_contains=$_POST['pkg_contains'];
Values() is function, put all vars in bracket and split them with ','.
mysql_query("INSERT INTO packages (pkg_name, pkg_price,pkg_contains) VALUES($pkg_name,$pkg_price,$pkg_contains)");

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>

Categories