Update query updates all rows in database instead of particular row - php

I am new to Zend framework and I am trying to update data in database and grid, but instead of updating particular row all rows are getting updated. please help me with this.
Here is my controller code.
public function editAction()
{
$form = new Application_Form_user();
$this->view->form = $form;
if($this->getRequest()->isPost())
{
$formData= $this->getRequest()->getPost();
if($form->isvalid($formData))
{
$client= new Application_Model_DbTable_Client();
$firstname = $formData['firstname'];
$lastname = $formData['lastname'];
$email = $formData['email'];
$client->updateClient('Id',$firstname,$lastname,$email);
$this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
else
{
$id=$this->getRequest()->getparam('id');
if($id>0)
{
$client= new Application_Model_DbTable_Client();
$clients = $client->getClient('Id');
$form->populate($clients[0]);
}
}
}
And here is my model code.
public function updateClient($id,$firstname,$lastname,$email)
{
$data=array('firstname'=>$firstname,
'lastname'=>$lastname,
'email'=>$email);
$this->update($data,"Id=$id");
}

$client->updateClient('Id',$firstname,$lastname,$email)
You're passing the literal string 'Id' as the $id parameter to updateClient. Inside updateClient, you build a where condition with "Id=$id". Your where condition becomes where Id=Id, which is true for every record, so every record is updated.
You need to pass a variable containing the actual ID of the record you want to update.

try to change your code, so it looks like this:
if($form->isvalid($formData))
{
$client= new Application_Model_DbTable_Client();
$firstname = $formData['firstname'];
$lastname = $formData['lastname'];
$email = $formData['email'];
$id=$this->getRequest()->getparam('id'); // define your id
$client->updateClient($id,$firstname,$lastname,$email); // here, change Id into proper $id
$this->_helper->redirector('index');
}

Related

Vtiger update field value from related module

I have a code on Save.php of Module Invoice:
public function saveRecord($request) {
...
$received = $request->get('received');
$balance = $request->get('balance');
$relProject = $request->get('relProject');
$project = Project_Record_Model::getInstanceById($relProject);
$fields = $project->getField('cf_938');
$fieldInstance = Vtiger_Field::getInstance($fields->id);
if ($fieldInstance) {
$fieldInstance->value = $received;
$fieldInstance->save();
}
}
Why save() is not work? When I save Invoice my field is not update on project.. I need fix it.
You should use the Record Model object to edit the record, not the Field Model. Try something like this:
public function saveRecord($request) {
...
$received = $request->get('received');
$balance = $request->get('balance');
$relProject = $request->get('relProject');
$project = Project_Record_Model::getInstanceById($relProject);
$project->set('mode', 'edit');
$project->set('cf_938', $received);
$project->save();
}

(date_time_selector) return in moodle

Does any one know if it is possible to have datetimepicker from date_time_selector function in moodle? I want the date/time selected from date_time_selector in a variable to store it into the database.
This is the code what I am working on right now in my edit.php , what am I missing
require_once('../../config.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/blocks/myblcok/myblock_form.php');
global $DB;
require_login($SITE);
$block = new myblock();
$mform = new myblock_form();
if ($data = $mform->get_data()) {
$startdate = $data->startdate;
}
$dataobject = new stdClass;
$dataobject->name = get_string('entryname', 'myblock');
$dataobject->description = get_string('entrydescription',
'myblock');
$dataobject->course = $COURSE->id;
$dataobject->userid = $USER->id;
$dataobject->starttime = $data->date;
$DB->insert_record('event', $dataobject);
redirect($CFG->wwwroot.'/course/view.php?id='.$courseid, '', 0);
I'm presuming this is being used in a form? The return value from data_time_selector is a timestamp which can be stored in a database. eg:
In edit_form.php file
require_once($CFG->dirroot.'/lib/formslib.php');
class edit_form extends moodleform {
function definition () {
$mform =& $this->_form;
$mform->addElement('date_time_selector', 'mytime', get_string('mytime', 'mycomponent'));
}
}
In edit.php
require_once('edit_form.php');
$mform = new edit_form();
if ($data = $mform->get_data()) {
// This is a timestamp.
$mytime = $data->mytime;
}
EDIT:
Ah... you need to pass a data object to insert a record eg:
$newrecord = new StdClass();
$newrecord->mytime = $mytime;
$DB->insert_record('table', $newrecord);
Or if the form field names are the same as your field names, just use the $data object eg:
$DB->insert_record('table', $data);
Have a look through the database API :
http://docs.moodle.org/dev/Data_manipulation_API#Inserting_Records

How to submit data to multiple tables in the same form

I have 2 tables: listings and listings_specifications
Listings table
id
type
status
location
specifications_id
Listings_specifications table
id
listing_id
swimming_pool
water_well
I need to select the specifications (checkboxes) on the same form with which I add a listing. I have created all the forms, views, models, controllers but I think I got some logic wrong.
Listing.php model
protected $table = 'listings';
public function contact()
{
return $this->BelongsTo('contacts');
}
public function specifications()
{
return $this->BelongsTo('listings_specifications');
}
Specification.php model
protected $table = 'listings_specifications';
public function listings()
{
return $this->BelongsTo('listings');
}
ListingsController.php (where the data gets saved in the database)
$listing = new Listing;
$contact = new Contact;
$listing->status = Input::get('status');
$listing->listingfor = Input::get('listingfor');
$listing->propertystatus = Input::get('propertystatus');
$listing->propertytype = Input::get('propertytype');
$listing->userid = Auth::user()->id;
$listing->location = Input::get('location');
$listing->contact_id = $contact_id;
$listing->save();
$specifications = Specification::find($id);
if( $listings->save() ) {
$specifications = new Specification;
$specifications->listing_id = $id;
$specifications->swimming_pool = Input::get('swimming_pool');
$specifications->water_front = Input::get('water_front');
$specifications->save();
}
I'm getting this error: Undefined variable: id
Where did I go wrong?
Thank you
It looks like you have some logic errors.
First of all, you are never setting $id anywhere, but that's okay because you really don't need it.
Remove the $specifications = Specification::find($id); line because that's not doing anything.
Then change your last section to something like this...
if( $listings->save() ) {
$specifications = new Specification;
$specifications->swimming_pool = Input::get('swimming_pool');
$specifications->water_front = Input::get('water_front');
$listing->specifications()->save($specifications);
}
$listing->specifications()->save($specifications); will automatically save the new specification with the correct listing_id for you.
Modify your Listing model's specifications relationship to...
public function specifications()
{
return $this->hasMany('Specification');
}
I'm assuming here one listing can have many specifications. If not, you can easily just change that to a hasOne.
You use $id in the line $specifications = Specification::find($id); but you don't define it before.

Catching the returned value

this may be a stupid question, but every source on the web seems not able to fully explain the logic to my complex brain
There's an edit page getting a $_GET['id'] from a link.
I got a function on my class elaborating this one to create an array of values from the database which must fill the form fields to edit datas. The short part of this code:
public function prel() {
$this->id= $_GET['id'];
}
public function EditDb () {
$connetti = new connessionedb();
$dbc = $connetti->Connessione();
$query = "SELECT * from anammi.anagrafica WHERE id = '$this->id'";
$mysqli = mysqli_query($dbc, $query);
if ($mysqli) {
$fetch = mysqli_fetch_assoc($mysqli);
return $fetch;
}
}
This array (which i tried to print) is perfectly ready to do what i'd like.
My pain starts when i need to pass it to the following function in the same class, which perhaps calls a parent method to print the form:
public function Associa() {
$a = $this->EditDb();
$this->old_id = $a['old_id'];
$this->cognome = $a['cognome'];
$this->nome = $a['nome'];
$this->sesso = $a['sesso'];
$this->tipo_socio_id = $a['tipo_socio_id'];
$this->titolo = $a['titolo']; }
public function Body() {
parent::Body();
}
How do i have to pass this $fetch?
My implementation:
<?php
require_once '/classes/class.ConnessioneDb.php';
require_once '/classes/class.editForm';
$edit = new EditForm();
$edit->prel();
if ($edit->EditDb()) {
$edit->Associa();
$edit->Body();
if (if ($edit->EditDb()) {
$edit->Associa();
$edit->Body();) {
$edit->Associa();
$edit->Body();
your Editdb method is returning a string and you are checking for a boolean condition in if statement. this is one problem.
using fetch-
$fetch=$edit->EditDb();
$edit->Associa();
$edit->Body($fetch);
Posting the full code of it:
public function prel() {
$this->id= $_GET['id'];
}
public function EditDb () {
$connetti = new connessionedb();
$dbc = $connetti->Connessione();
$query = "SELECT * from table WHERE id = '$this->id'";
$mysqli = mysqli_query($dbc, $query);
if ($mysqli) {
$fetch = mysqli_fetch_assoc($mysqli);
return $fetch;
}
}
public function Associa($fetch) {
$this->old_id = $fetch['old_id'];
$this->cognome = $fetch['cognome'];
$this->nome = $fetch['nome'];
$this->sesso = $fetch['sesso']; //it goes on from there with many similar lines
}
public function Body() {
$body = form::Body();
return $body;
}
Implementation
$edit = new EditForm();
$edit->prel();
$fetch=$edit->EditDb();
$edit->Associa($fetch);
$print = $edit->Body();
echo $print;
Being an edit form base on a parent insert form, i added an if in the parent form that sees if is set an $_GET['id] and prints the right form header with the right form action. This was tricky but really satisfying.

Retrieve data from zend form

I'm a Zend Framework newbie and I'm trying to retrieve some values to update the database. I have the following code in the controller. Populating the form works just fine, as does updating the db with some hard-coded values. My problem lies in trying to retrieve an updated value from the form, see the $first_name variable. It gives me a fatal error.
My question: How do I retrieve an updated value from the form?
public function editAction() {
$view = $this->view;
//get the application
$application_id = $this->getRequest()->getParam('id');
$application = $this->getApplication($application_id);
//get the applicant
$applicant_id = $application->applicant_id;
$applicant = $this->getApplicant($applicant_id);
if ($this->getRequest()->isPost()) {
if ($this->getRequest()->getPost('Save')) {
$applicants_table = new Applicants();
$first_name = $form->getValue('applicant_first_name');
$update_data = array ('first_name' => 'NewFirstName',
'last_name' => 'NewLastName');
$where = array('applicant_id = ?' => 16);
$applicants_table->update($update_data, $where);
}
$url = 'applications/list';
$this->_redirect($url);
} else { //populate the form
//applicant data
$applicant_data = array();
$applicant_array = $applicant->toArray();
foreach ($applicant_array as $field => $value) {
$applicant_data['applicant_'.$field] = $value;
}
$form = new FormEdit();
$form->populate($applicant_data);
$this->view->form = $form;
}
}
First off, your example has a problem here:
$first_name = $form->getValue('applicant_first_name');
...your $form isn't created yet, thus the fatal error; you are calling getValue() on a non-object.
Once you get that squared away, you can populate the form with posted data by calling the isValid method on the $form with request data. Here's a quick example:
// setup $application_data
$form = new FormEdit();
$form->populate($applicant_data);
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$first_name = $form->getValue('applicant_first_name');
// save data to the database ...
}
}

Categories