update one specific row from an existing user by a webform - webforms

i have try multiple ways but i need to update an specific row from an existing user by a webform i have try this code but i have some thing wrong on the code.
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
while($row = mysqli_fetch_array($result)){
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="user_wallet" value="'.$row['user_wallet'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
}
if(isset($_POST['update'])){
$user_wallet = $_POST['user_wallet'];
$sql = mysqli_query("UPDATE users SET user_wallet = '$user_wallet' WHERE id = '$id'");
$retval = mysqli_query( $sql, $con );
if(! $retval ){
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
?>

Related

Fetch corresponding value of a drop downlist

I want to fetch the corresponding value of my dropdown the image below shows the form and the database. the table was inside the javascript and Iam having a dificult time in figuring out this problem. please help me how to solve this problem... Thank you very much in advance...
Database
forms entry
My Controller
function getFeetypeEndDate() {
$feetype_id = $this->input->get('feety_id[]');
$data = $this->feegroup_model->getFeetypeByEndDate($feetype_id);
echo json_encode($data);
}
My Model
public function get($id = null) {
$this->db->select()->from('feetype');
$this->db->where('is_system', 0);
if ($id != null) {
$this->db->where('id', $id);
} else {
$this->db->order_by('id');
}
$query = $this->db->get();
if ($id != null) {
return $query->row_array();
} else {
return $query->result_array();
}
}
My Javascript in the View Section
<script>
$(document).ready(function (){
$("body").on('click', '.btn-add-more', function (e) {
e.preventDefault();
var amount = $("#amount").val();
var penalty = $("#penalty").val();
var $sr = ($(".jdr1").length + 1);
var rowid = Math.random();
var $html = '<tr class="jdr1" id="' + rowid + '">' +
'<td><span class="btn btn-sm btn-default">' + $sr + '</span><input type="hidden" name="count[]" value="'+Math.floor((Math.random() * 10000) + 1)+'"></td>' +
'<td><select id="feetype_id[]" name="feetype_id[]" class="form-control" >' +
'<?php foreach ($feetypeList as $feetype) { ?>' +
' <option value="<?php echo $feetype['id'] ?>" ' +
'<?php if (set_value('feetype_id[]') == $feetype['id']) { echo "selected =selected"; } ?>><?php echo $feetype['type'] ?></option> <?php $count++; } ?> </select></td>' +
'<td><input type="text" id="startDate" name="startDate" value="<?php echo date($this->customlib->getSchoolDateFormat($feetype->start_date), $this->customlib->dateyyyymmddTodateformat($feetype->start_date)); ?>" placeholder="Start Date" class="form-control input-sm"/></td>' +
'<td><input type="text" id="endDate" name="endDate" value="<?php echo date($this->customlib->getSchoolDateFormat($feetype->end_date), $this->customlib->dateyyyymmddTodateformat($feetype->end_date)); ?>" placeholder="End Date" class="form-control input-sm"/></td>' +
'<td><input type="text" name="amount_td[]" placeholder="Amount" class="form-control input-sm" value="'+amount+'"></td>' +
'<td><input type="text" name="penalty_td" placeholder="Penalty" class="form-control input-sm" value="'+penalty+'"></td>' +
'</tr>';
$("#table-details").append($html);
});
$("body").on('click', '.btn-remove-detail-row', function (e) {
e.preventDefault();
if($("#table-details tr:last-child").attr('id') != 'row1'){
$("#table-details tr:last-child").remove();
}
});
});

Prevent duplicate entry in database Codeigniter

I would like to know how to prevent my upload function to insert duplicate entries into the database by using the uid.
public function upload(){
if(!empty($_FILES['uploaded_file']))
{
$path = FCPATH . "/file_attachments/signature_file/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
$base64 = base64_encode(file_get_contents($_FILES['uploaded_file']['tmp_name']));
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path))
{
$data = array (
'uid' => $this->user->get_uid(),
'image' => $base64,
'name' => basename( $_FILES['uploaded_file']['name']),
);
$this->load->database('employee');
$this->db->insert('signatures', $data);
// echo "The file ". basename( $_FILES['uploaded_file']['name']).
// " has been uploaded";
$alert = "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
}
else
{
// echo "There was an error uploading the file, please try again!";
$alert ="There was an error uploading the file, please try again!";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
}
}
}
Here's my view file. I haven't got the time to edit the unnecessary things in here. The problem is still that if it got duplicate entries the database error still shows up and that is what I'm preventing to do so. Cheers!
<div class="bod">
<div class="insta">
<form enctype="multipart/form-data" style="padding-top: 10px" name="exit_form" method="post" action="<?= base_url() ?>signature_uploader/upload">
<input type="hidden" name="uid" value="<?= $agent->get_uid() ?>" />
<input type="hidden" name="gid" value="<?= $agent->get_gid() ?>" />
<input type="hidden" name="fname" value="<?= $agent->get_fname() ?>" />
<input type="hidden" name="lname" value="<?= $agent->get_lname() ?>" />
<div style="text-align: center; font-size: 25pt; margin-bottom: 15px">Signature Uploader</div>
<table width="105%">
<tr>
<td width="40%"><strong>Name: </strong><input class="textinput" disabled="disabled" type="text" name="full_name" id="textfield3" size="35" value="<?= $agent->get_fullName() ?>" /></td>
<tr/>
<tr>
<td><label>Upload Image File:</label><br /> <input name="uploaded_file" type="file" accept=".png" required/>
</tr>
<table/>
<br />
<input type="submit" value="Submit" class="button1" />
</form>
<br/>
</div>
You can use from Codeigniter form validation to prevent from duplicate entry:
at first you must get the uid in the form view like this:
<input type="hidden" name="uid" value="<?php echo $this->user->get_uid() ?>">
and then in your controller:
public function upload(){
if(!empty($_FILES['uploaded_file']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('uid', 'UID', 'is_unique[signatures.uid]');
if ($this->form_validation->run() == FALSE)
{
// Your Code if the uid is duplicate
$alert ="Could't upload because of duplicate entry!";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
}
else
{
// Your Code if the uid is unique
$path = FCPATH . "/file_attachments/signature_file/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
$base64 = base64_encode(file_get_contents($_FILES['uploaded_file']['tmp_name']));
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
$data = array (
'uid' => $this->input->post('uid'),
'image' => $base64,
'name' => basename( $_FILES['uploaded_file']['name']),
);
$this->load->database('employee');
$this->db->insert('signatures', $data);
// echo "The file ". basename( $_FILES['uploaded_file']['name']).
// " has been uploaded";
$alert = "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
} else{
// echo "There was an error uploading the file, please try again!";
$alert ="There was an error uploading the file, please try again!";
redirect(base_url() . "signature_uploader/search/?id=" . $alert);
}
}
}
}
Either you can check weather such file exists or not, but normally while creating a code which uploads file, has a function which will generate unique name for each file that we upload. So in such cases it will have duplicate entries with different name.
Only way i see is to create a function that will check weather the file name already exists in database.

submit multiple inputs within a forloop in codeigniter

My code is to fetch questions of users saved in the database by a foreach loop and let the admin answer each question and save the answer of each question after checking of validation rules in the database , Here we go :
Model is :
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result();
}
My view is:
foreach ($questions as $id => $row) :
?>
<?php
echo "<h5>".$row->question;
echo "<br>";
echo "from : ".$row->user_name."</h5>";
echo date('Y-m-d H:i');
echo "<br>";
$q_no='save'.$row->id;
$ans_no='answer'.$row->id;
echo "<h4> Answer:</h4>";
echo form_open('control_panel');
?>
<textarea name='<?php echo 'answer'.$row->id; ?>' value="set_value('<?php echo 'answer'.$row->id; ?>')" class='form-control' rows='3'> </textarea>
<input type='hidden' name='<?php echo $q_no ; ?>' value='<?php echo $q_no; ?>' />
<input type='hidden' name='<?php echo $ans_no ; ?>' value='<?php echo $ans_no ; ?>' />
<?php
echo form_error($ans_no);
echo "
<div class='form-group'>
<div >
<label class='checkbox-inline'>
<input type='checkbox' name='add_faq' value='yes' />
Adding to FAQ page .
</label>
</div>
</div>
<p>";
?>
<input type='submit' name='<?php echo 'save'.$row->id; ?>' value='<?php echo 'save'.$row->id; ?>' class='btn btn-success btn-md'/>
<?php
echo 'answer'.$row->id;
?>
<hr>
<?php endforeach; ?>
and my controller is :
$this->load->model('control_panel');
$data['questions']=$this->control_panel->get_questions();
$data['no_of_questions']=count($data['questions']);
if($this->input->post($q_no))
{
$this->form_validation->set_rules($ans_no,'Answer','required|xss_clean');
if($this->form_validation->run())
{
/* code to insert answer in database */
}
}
of course it did not work with me :
i get errors :
Severity: Notice
Message: Undefined variable: q_no
i do not know how to fix it
I am using codeigniter as i said in the headline.
In your controller on your post() you have a variable called q_no you need to set variable that's why not picking it up.
I do not think name="" in input can have php code I think it has to be text only.
Also would be best to add for each in controller and the call it into view.
Please make sure on controller you do some thing like
$q_no = $this->input->post('q_no');
$ans_no = $this->input->post('ans_no');
Below is how I most likely would do lay out
For each Example On Controller
$this->load->model('control_panel');
$data['no_of_questions'] = $this->db->count_all('my_table');
$data['questions'] = array();
$results = $this->control_panel->get_questions();
foreach ($results as $result) {
$data['questions'][] = array(
'question_id' => $result['question_id'],
'q_no' => $result['q_no'],
'ans_no' => $result['ans_no']
);
}
//Then validation
$this->load->library('form_validation');
$this->form_validation->set_rules('q_no', '', 'required');
$this->form_validation->set_rules('ans_no', '', 'required');
if ($this->input->post('q_no')) { // Would Not Do It This Way
if ($this->form_validation->run() == TRUE) {
// Run Database Insert / Update
// Redirect or load same view
} else {
// Run False
$this->load->view('your view', $data);
}
}
Example On View
<?php foreach ($questions as $question) {?>
<input type="text" name="id" value="<?php echo $question['question_id'];?>"/>
<input type="text" name="q_no" value"<?php echo $question['q_no'];?>"/>
<input type="text"name="a_no" value="<?php echo $question['a_no'];?>"/>
<?php }?>
Model
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result_array();
}

Updating one data on datagrid using checkbox codeigniter

i have some issue when i updating every single or one data using checkbox. i want to do is to update only the selected checkbox updated. How do I stop the foreach from running if a checkbox has not been checked.
this is my model
function save_cargo_details() {
$data = array();
$waybillno = $this->input->post('waybillno');
$quantity = $this->input->post('quantity');
$waybilldate = $this->input->post('waybilldate');
$declared_value = $this->input->post('declared_value');
$consignee = $this->input->post('consignee');
$count = count($waybillno);
if(empty($waybillno)){
}else{
for ($i = 0; $i < $count; $i++) {
$data = array(
'waybillno' => $waybillno[$i],
'quantity' => $quantity[$i],
'waybilldate' => $waybilldate[$i],
'declared_value' => $declared_value[$i],
'consignee' => $consignee[$i],
);
// SUBRACT REMAINING_QUANTITY //
$this->db->select('wd.remaining_qty');
$this->db->where('wd.waybillno',$waybillno[$i]);
$this->db->from(self::WAYBILL_DETAILS_TABLE. " as wd");
$query = $this->db->get()->row();
$qty = $query->remaining_qty;
$remaining = abs($data['quantity'] - $qty);
$this->db->where('waybillno',$waybillno[$i]);
$this->db->set('remaining_qty',$remaining);
$this->db->update(self::WAYBILL_DETAILS_TABLE);
// INSERT DATA //
$this->db->insert('sys_cargodetails', $data);
$this->session->set_flashdata('success', '<p id="success_message">Record has been successfully saved.</p>');
}
}
}
this is my controller
public function create_cargo_manifest(){
$core_model = new Core_m;
$core_model->save_cargo_details();
redirect('core/cargo_lookup/');
}
this is my view
<?php foreach($waybill_header as $waybill_header) { ?>
<?php echo form_open('core/create_cargo_manifest'); ?>
<tr style="text-align: center;">
<td><input type="checkbox" name="waybillno[]" value="<?php echo $waybill_header->waybillno; ?>"></td>
<td><?php echo $waybill_header->waybillno; ?></td>
<td><?php echo $waybill_header->waybilldate; ?><input type="hidden" value="<?php echo $waybill_header->waybilldate; ?>" name="waybilldate[]"></td>
<td><input type="text" size="5" value="<?php echo $waybill_header->remaining_qty; ?>" name="quantity[]">
<input type="hidden" value="<?php echo $waybill_header->declared_value; ?>" name="declared_value[]">
<input type="hidden" value="<?php echo $waybill_header->consignee; ?>" name="consignee[]">
</td>
</tr>
<?php } ?>
You can give an extra field in the database for the checkbox.If the check box is checked it takes 1 as the value and if unchecked 0 as the value.Give the default value as per your requirement.If you want every checkbox to be checked give 1 as default else otherwise.Then you will just need to put a condition before foreach loop,i.e. if value of checkbox is 1 run the foreach loop else not.

How do i get the actual image to show on my page rather than the text?

<?php
require 'db.php';
include_once("header.php");
include_once("functions.php");
if(isset($_POST['search_term'])){
$search_term = mysql_real_escape_string(htmlentities ($_POST['search_term']));
if(!empty($search_term)){
$search = mysql_query("SELECT users.username, users.id, tbl_image.photo, tbl_image.userid FROM users LEFT OUTER JOIN tbl_image ON users.id=tbl_image.userid WHERE users.username LIKE '%$search_term%' and users.business <> 'business'");
$result_count = mysql_num_rows($search);
$suffix = ($result_count != 1) ? 's' : '';
echo '<div data-theme="a">Your search for <strong>' , $search_term ,'</strong> returned <strong>', $result_count,' </strong> record', $suffix, '</div>';
while($results_row = mysql_fetch_assoc($search)){
echo '<div data-theme="a"><strong>',$results_row['photo'], $results_row['username'], '</strong></div>';
$following = following($_SESSION['userid']);
if (in_array($key,$following)){
echo ' <div action= "action.php" method="GET" data-theme="a">
<input type="hidden" name="id" value="$key"/>
<input type="submit" name="do" value="follow" data-theme="a"/>
</div>';
}else{
echo " <div action='action.php' method='GET' data-theme='a'>
<input type='hidden' name='id' value='$key'/>
<input type='submit' name='do' value='follow' data-theme='a'/>
</div>";
}
}
}
}
?>
How do i get the actual image to show on the page rather than the name of the image. I have the images in the file system under a folder called image. How do I echo that image on screen or if echo is not the right way to do it how would you do it?
You mean like this?
echo '<img src="'.$results_row['photo'].'" width="80">';

Resources