Why my post method is not working properly - php

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

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

Codeigniter - Show and Edit Last Id recorded

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

Unable insert in mysql table when I call another php

When I press register button in crudindex.php It will insert data in to cruduser(table)
once done Call crudadd.php and insert data into crud(table) with max(id) from cruduser table as id.
The issue is :Once I press REGISTER button Cruduser(table) is getting inserted properly.
But In crud(table) it inserts only the id and other fields are blank.
I have a doubt the post is not picking the values or insert command some issues.
But when I give static values ex : firstname as 'Tim', lastname as 'cook' etc its inserting in the table crud.
Also when I run crudadd.php separately it inserts properly.
Structure :
cruduser(
id(int),
username(varchar),
password(varchar)
)
crud(
id(int),
firstname(varchar),
lastname(varchar),
email(varchar),
gender(varchar),
age(varchar)
)
used : tables : cruduser and crud
php : crudindex.php and crudadd.php
Core issue : data is not properly inserted in to crud table
Crudindex.php
<?php
$con = mysqli_connect("127.0.0.1", "kkits996_ganesh", "mysql123#", "kkits996_testmysql") or die("Error " . mysqli_error($con));
if (isset($_POST) && (!empty($_POST))){
$uname=mysqli_real_escape_string($con,$_POST["uname"]);
$pwd=mysqli_real_escape_string($con,$_POST["pwd"]);
$cpwd=mysqli_real_escape_string($con,$_POST["cpwd"]);
if (isset($_POST['register'])) {
# Register-button was clicked
$createsql1="INSERT INTO cruduser(id,username,password) VALUES
('','$uname','$pwd')";
if (mysqli_query($con,$createsql1)) {
echo "Insert Successful in Table cruduser";
mysqli_close($con);
include ("crudadd.php");
}
else
{
die(mysqli_error($con));
}
}
mysqli_close($con);
}
?>
<!--DocType HTML -->
<! bootstrap link is downloaded from bootstrapcdn.com for css and js -->
<! col-mod-6 col-mod-offset are bootstrap related-->
<HTML>
<head>
<title>"Add records in CRUD Table"</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<form method="post" class="form-horizontal col-mod-6 col-mod-offset-3">
<h2>Create The table CRUD</h2>
<div class="form-group">
<label for="input" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="uname" class="form-control" id="input1" placeholder="Username"/>
</div>
</div>
<div class="form-group">
<label for="input" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="pwd" class="form-control" id="input1" placeholder="Password"/>
</div>
</div>
<div class="form-group">
<label for="input" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="cpwd" class="form-control" id="input1" placeholder="Confirm Password"/>
</div>
</div>
<div class="row">
<div class="col-mod-6 col-mod-offset-3">
<button id="submit1" name="register" class="btn btn-primary pull-right">Register</button>
<button id="submit2" name="login" class="btn btn-secondary pull-right">Login</button>
</div>
</div>
</form>
</body>
</html>
Crudadd.php
<?php
//session_start();
//$maxiid = $_SESSION['id'];
//echo $maxiid;
$con = mysqli_connect("127.0.0.1", "kkits996_ganesh", "mysql123#", "kkits996_testmysql") or die("Error " . mysqli_error($con));
$result = mysqli_query($con,"SELECT * FROM cruduser WHERE id=(SELECT MAX(id) FROM cruduser)");
$row1 = mysqli_fetch_array($result);
$c1 = $row1['id'];
mysqli_close($con);
$con = mysqli_connect("127.0.0.1", "kkits996_ganesh", "mysql123#", "kkits996_testmysql") or die("Error " . mysqli_error($con));
if (isset($_POST) && (!empty($_POST))){
$fname=mysqli_real_escape_string($con,$_POST["fname"]);
$lname=mysqli_real_escape_string($con,$_POST["lname"]);
$email=mysqli_real_escape_string($con,$_POST["email"]);
$gender=$_POST["gender"];
$age=$_POST["age"];
print "I am here";
echo $finame;
echo $liname;
print email;
//Notes : In Insert if numeric do not use quotes. if string use quotes.for auto use ''
$createsql="INSERT INTO crud(id,firstname,lastname,email,gender,age) VALUES
($c1,'$fname','$lname','$email','$gender','$age')";
if (mysqli_query($con,$createsql)) {
echo "Connection Successful";
}
else
{
echo "Connection Issue";
die(mysqli_error($con));
}
mysqli_close($con);
}
?>
<!--DocType HTML -->
<! bootstrap link is downloaded from bootstrapcdn.com for css and js -->
<! col-mod-6 col-mod-offset are bootstrap related-->
<HTML>
<head>
<title>"Add records in CRUD Table"</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<form method="post" class="form-horizontal col-mod-6 col-mod-offset-3">
<h2>Create The table CRUD</h2>
<div class="form-group">
<label for="input" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="finame" class="form-control" id="input1" placeholder="First name"/>
</div>
</div>
<div class="form-group">
<label for="input" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="liname" class="form-control" id="input1" placeholder="Last name"/>
</div>
</div>
<div class="form-group">
<label for="input" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="eimail" class="form-control" id="input1" placeholder="Email"/>
</div>
</div>
<div class="form-group">
<label for="input" class="col-sm-2 control-label">Gender</label>
<div class="col-sm-10">
<label>
<input type="radio" name="giender" id="optionsRadios1" value="male" checked> Male
</label>
<label>
<input type="radio" name="giender" id="optionsRadios1" value="female" > Female
</label>
</div>
</div>
<div class="form-group">
<label for="input" class="col-sm-2 control-label">Age</label>
<div class="col-sm-10">
<select name="aige" class="form-control">
<option>Select your age</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
</select>
</div>
</div>
<input type="submit" class="btn btn=primary col-md-2 col-md-offset-10" value="submit"/>
</form>
</body>
</html>
While clicking on 'Register' you are sending uname, pwd and cpwd ina POST request which are properly used to be inserted into cruduser. After that you are including the crudadd.php (include ("crudadd.php");). But remember you are still at the same request. So the only data available is uname, pwd and cpwd. This is why your rows are semi-empty.
if Id is AUTO INCREMENT.
you must use
$createsql1="INSERT INTO cruduser(id,username,password) VALUES
(NULL,'$uname','$pwd')";
not ((you can not set string for id value. Your id is of int type ))
$createsql1="INSERT INTO cruduser(id,username,password) VALUES
('','$uname','$pwd')";

Php get from data which runs inside loop fetched from database

Hello i have a php code where i fetch my data from database and loop it around with each form. now i want to get the value from each looped form data. here is my code:
$query = "SELECT question,type,option1,option2,option3,option4,option5,option6,answer FROM question WHERE exam_id = '$exam_id'";
$result = mysqli_query($connection, $query);
while($row=mysqli_fetch_assoc($result))
{
if ($row['type'] == "true/false") {
echo '
<form class="form-horizontal" role="form" method="POST" action="">
<div class="form-group">
<div class="col-sm-10">
<p>'. $row["question"] . ' </p>
</div>
</div>
<div id="form-label">
<p class="alignleft"><b>Mark this question as:</b></p>
<div style="clear: both;"></div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-2">
<input type="radio" placeholder="" name="answer" value = "true" id="" required> True
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-2">
<input type="radio" placeholder="" name="answer" value= "false" id=""> False
</form>';
Maybe i understand what do you want.
You must to use only 1 form, and use array of object for many input (question).
question_page.php
$query = "SELECT question,type,option1,option2,option3,option4,option5,option6,answer FROM question WHERE exam_id = '$exam_id'";
$result = mysqli_query($connection, $query);
echo '<form class="form-horizontal" role="form" method="POST" action="response_question_page.php">';
while($row=mysqli_fetch_assoc($result))
{
if ($row['type'] == "true/false") {
echo '
<div class="form-group">
<div class="col-sm-10">
<p>'. $row["question"] . ' </p>
</div>
</div>
<div id="form-label">
<p class="alignleft"><b>Mark this question as:</b></p>
<div style="clear: both;"></div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-2">
<input type="radio" placeholder="" name="question[][answer]" value = "true" id="" required> True
</div>
</div>
<!-- Text input-->
<div class="form-group">
<div class="col-md-2">
<input type="radio" placeholder="" name="question[][answer]" value= "false" id=""> False';
echo '<input type="hidden" name="question[][question]" value="'.$row["question"].'">';
}
}
echo '<input type="submit" value="Send all response"></form>';
in your response_question_page.php the object passed is:
{"question" => [
{
"question" => "what is your name",
"answer" => "true"
}
]}

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