Saving data from custom form to db - php

i have been trying to create a very simple form for a drupal site, the form displays all fields correctly. when trying to save the information to the database however i get an error.
i get: PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) on line 286
i have been looking at severel guides on how to save data in drupal, and all agree that i should be using db_insert instead of a db_query with an insert statement, but for the life of me, i can't understand why i get this error. i have been looking at several examples including;
https://www.drupal.org/node/1164398
can anyone tell me why it isnt saving? and im getting the error?
function drupalform_form1_submit($form, &$form_state) {
$values = array(
// CONTACT INFO
'email' => $form_state['values']['email'],
'phone' => $form_state['values']['phone'],
// PARTICIPANT 1 VALUES
'p1name' => $form_state['values']['p1name'],
'p1nationality' => $form_state['values']['p1nationality'],
'p1University' => $form_state['values']['p1University'],
'p1year' => $form_state['values']['p1year'],
// PARTICIPANT 2 VALUES
'p2name' => $form_state['values']['p2name'],
'p2nationality' => $form_state['values']['p2nationality'],
'p2University' => $form_state['values']['p2University'],
'p2year' => $form_state['values']['p2year'],
// PARTICIPANT 3 VALUES
'p3name' => $form_state['values']['p3name'],
'p3nationality' => $form_state['values']['p3nationality'],
'p3University' => $form_state['values']['p3University'],
'p3year' => $form_state['values']['p3year'],
// PARTICIPANT 4 VALUES
'p4name' => $form_state['values']['p4name'],
'p4nationality' => $form_state['values']['p4nationality'],
'p4University' => $form_state['values']['p24niversity'],
'p4year' => $form_state['values']['p4year']
);
$insert = db_insert('bte_handin')->fields(array(
// INSERTING CONTACT INFO
'email' => $values['email'],
'phone' => $values['phone'],
// INSERTING PARTICIPANT 1
'p1name' => $values['p1name'],
'p1nationality' => $values['p1nationality'],
'p1University' => $values['p1University'],
'p1year' => $values['p1year'],
// INSERTING PARTICIPANT 2
'p2name' => $values['p2name'],
'p2nationality' => $values['p2nationality'],
'p2University' => $values['p2University'],
'p2year' => $values['p2year'],
// INSERTING PARTICIPANT 3
'p3name' => $values['p3name'],
'p3nationality' => $values['p3nationality'],
'p3University' => $values['p3University'],
'p3year' => $values['p3year'],
//INSERTING PARTICIPANT 4
'p4name' => $values['p4name'],
'p4nationality' => $values['p4nationality'],
'p4University' => $values['p4University'],
'p4year' => $values['p4year'],
))->execute(); This is where the error is occuring
drupal_set_message("Your answer has ben saved");
}

Related

How to update data in laravel - there is array value in data

There is field affiliated_facility which is in array and i am trying to update my values:
My controller code:
<?php
$affiliated_facility = implode(",", $userProfile['affiliated_facility']);
$userBasicInfoId = UserBasicInfo::where('user_id', $userProfile['id'])->value('id')->update([
'work_phone' => $userProfile['work_phone'],
'fax' => $userProfile['fax'],
'extension' => $userProfile['extension'],
'primary_facility' => $userProfile['primary_facility'],
'employed_by' => $userProfile['employed_by'],
'emergency_phone' => $userProfile['emergency_phone'],
'designation' => $userProfile['designation'],
'department' => $userProfile['department'],
'employment_type' => $userProfile['employment_type'],
'biography' => $userProfile['biography'],
'hiring_date' => $userProfile['hiring_date'],
'from' => $userProfile['from'],
'to' => $userProfile['to'],
'affiliated_facility' => $affiliated_facility]); // this is in array so i used implode in top of this code
if ($userBasicInfoId) {
$userBasicInfo = $this->userBasicInfo->find($userBasicInfoId);
$userBasicInfo->fill($userProfile)->save();
} else {
$userBasicInfo = $this->userBasicInfo->create($request->only($this->userBasicInfo->getModel()->fillable));
}
?>
But when I hit my request it says
Call to a member function update() on integer
There is some mistakes in my code i want to update my record and there is one field coming which is in array
"affiliated_facility": [1,2]
can someone please help me to modify this code i am stuck on this your help will be highly appreciated!
Thankyou in advance
UserBasicInfo::where('user_id', $userProfile['id']) is a Builder instance. you should get Model instance to update it. So, try:
UserBasicInfo::where('user_id', $userProfile['id'])->first()->update([...]);

how can i get the value of an an auto incremented id as a foreign key in a different table in Laravel inside a controller

In the table called template, I get the id of staffmembers by using session. However, I really don't know how I can to it for the table module. I am coding using the Laravel framework.
I also get this error message :
Invalid datetime format: 1366 Incorrect integer value: 'templates_id' for column 'templates_id' at row 1 (SQL: insert into modules (moduleName, moduleCode, year, templates_id)
template::create([
'paperTitle' => $request->paperTitle,
'markedElementOne' => $request->markedElementOne,
'scoreOne' => $request-> scoreOne,
'markedElementTwo' => $request->markedElementTwo,
'scoreTwo' => $request-> scoreTwo,
'staffmembers_id'=>\Session::get('staffmembers_id'),
]);
module::create([
'moduleName' => $request->moduleName,
'moduleCode' => $request->moduleCode,
'year' => $request->year,
'templates_id'=>('templates_id'),
]);
Get the template back to a variable when creating.
$tempalte = template::create([
'paperTitle' => $request->paperTitle,
'markedElementOne' => $request->markedElementOne,
'scoreOne' => $request-> scoreOne,
'markedElementTwo' => $request->markedElementTwo,
'scoreTwo' => $request-> scoreTwo,
'staffmembers_id' => \Session::get('staffmembers_id'),
]);
get template id from that variable to use to create 'module'
module::create([
'moduleName' => $request->moduleName,
'moduleCode' => $request->moduleCode,
'year' => $request->year,
'templates_id' => $template->id
]);

mongoDB, PHP update specific value not all the values

I am having a problem in updating values i get from web service ..
$collection = $modb->$table;
$collection->update(array("id" => (int)$row['id']),
array('$set' => array(
"user_id" => (int)$post_data_array['user_id'],
"story" => (int)$post_data_array['story'],
"surprize_sub1" => (int)$post_data_array['surprize_sub1'],
"surprize_sub2" => (int)$post_data_array['surprize_sub2'],
"surprize_sub3" => (int)$post_data_array['surprize_sub3'],
"exr_solve" => (int)$post_data_array['exr_solve'],
"exr_assessmnt" => (int)$post_data_array['exr_assessmnt'],
"exr_refresh" => (int)$post_data_array['exr_refresh'],
"sound_control" => (int)$post_data_array['sound_control'],
"clock_control" => (int)$post_data_array['clock_control'],
"switch_user" => (int)$post_data_array['switch_user'],
"exr_print" => (int)$post_data_array['exr_print'],
"write_on_wall" => (int)$post_data_array['write_on_wall'],
"switch_letter" => (int)$post_data_array['switch_letter'],
"view_controls" => (int)$post_data_array['view_controls'],
)));
I get these values from end users.. i want the specific field sent to be updated without loosing all the rest of data ..
in this code only sent data is set while removing the rest .. i want to change only sent ones by keeping the rest as they are, please advice
you need to use updateOne instead of update .
updateOne
Use the MongoDB\Collection::updateOne() method to update a single document matching a filter.
$collection = $modb->$table;
$collection->updateOne(array("id" => (int)$row['id']),
array('$set' => array(
// .... array elements
)));

Saving one-to-many relationship to DB (laravel)

I'm trying to save a relationship the way I saw on the docs, but isn't working.
On my Contato (Contact) model I've:
public function filhoContato()
{
return $this->hasMany('FilhoContato', 'id_contato');
}
Along with my fillables to enable mass-assignment
My FilhoContato (ContactChildren) model:
public function contato()
{
return $this->belongsTo('Contato');
}
And on my controller:
$contato = Contato::create(array(
'nome' => Input::get('nome'),
'nascimento' => $data,
'cpf' => Input::get('cpf'),
'tel_principal' => Input::get('telefone'),
'idade' => Input::get('idade'),
'email' => Input::get('email'),
'tipo_end' => Input::get('tipo'),
'cep' => Input::get('cep'),
'estado' => Input::get('estado'),
'cidade' => Input::get('cidade'),
'bairro' => Input::get('bairro'),
'rua' => Input::get('rua'),
'numero' => Input::get('numero'),
'logradouro' => Input::get('logradouro'),
'genero' => Input::get('genero'),
'estadoCivil' => Input::get('estadoCivil'),
'mae' => Input::get('mae'),
'pai' => Input::get('pai'),
'filhos' => Input::get('filhos'),
'grupo' => Input::get('grupo'),
'caminho' => $filename . $extension,
'ativo' => Input::get('ativo'),
'exaluno' => Input::get('exaluno')
));
$filhocontato = new FilhoContato(array('nome' => Input::get('name')));
$contato = Contato::find(1);
$filhocontato = $contato->filhoContato()->save($filhocontato);
However only the Contato (contact) table data gets inserted.
How can I save both tables at same time? I need a loop to get all value from Input::get('name') too.
Just to clarify, if my Contato(contact) has a children then he will insert their names in a dynamically generated form text field and when he clicks submit all of his data will go to Contato(contact) table and his children (if he has) will go to filhocontato table which structure is (id, nome, id_contato). That's what I'm aiming for at least :/

Medoo - Updating table row matching field

I am using Medoo but know very little php, the update below works fine but I only want it to update when the clientId matches what is posted:
$database->update("clientInfo", array(
"businessName" => $_POST['businessName'],
"contactName" => $_POST['contactName'],
"businessEmail" => $_POST['businessEmail'],
"businessPhone" => $_POST['businessPhone'],
"businessWebsite" => $_POST['businessWebsite'],
"businessAddress" => $_POST['businessAddress'],
"businessAddress2" => $_POST['businessAddress2'],
"businessCity" => $_POST['businessCity'],
"businessState" => $_POST['businessState'],
"businessZip" => $_POST['businessZip']
));
//$_POST["clientId"];
I am using a newer version of PHP and am a little confused on the syntax, here is how it is with older versions of PHP
http://medoo.in/api/update
I assume you have clientId field in that table, you can try:
$database->update("clientInfo", array(
"businessName" => $_POST['businessName'],
"contactName" => $_POST['contactName'],
"businessEmail" => $_POST['businessEmail'],
"businessPhone" => $_POST['businessPhone'],
"businessWebsite" => $_POST['businessWebsite'],
"businessAddress" => $_POST['businessAddress'],
"businessAddress2" => $_POST['businessAddress2'],
"businessCity" => $_POST['businessCity'],
"businessState" => $_POST['businessState'],
"businessZip" => $_POST['businessZip']
), array(
"clientId" => $_POST['clientId']
));
Hope it helps.

Categories