How can I update my data into my database? There is no error but it doesn't update in my database
This is my model
public function updateNewServices($prod_name,$prod_id,$service_type,$service_gl_acc,$min_amnt,
$def_amnt,$max_amnt,$client,$services_id)
{
$services_id = $this->db->escape_str($services_id);
$prod_name = $this->db->escape_str($prod_name);
$prod_id = $this->db->escape_str($prod_id);
$service_type = $this->db->escape_str($service_type);
$service_gl_acc = $this->db->escape_str($service_gl_acc);
$min_amnt = $this->db->escape_str($min_amnt);
$def_amnt = $this->db->escape_str($def_amnt);
$max_amnt = $this->db->escape_str($max_amnt);
$client = $this->db->escape_str($client);
$basic_data = array(
"prod_name" => $prod_name,
"prod_id" => $prod_id,
"service_type" => $service_type,
"service_gl_acc" => $service_gl_acc,
"min_amnt" => $min_amnt,
"def_amnt" => $def_amnt,
"max_amnt" => $max_amnt,
"client" => $client
);
$this->db->where("services_id=",$services_id);
$this->db->update("services",$basic_data);
}
1) First you should change line $this->db->where('services_id', $services_id);
2) second one you should check data type in database and update data value type also .
3) You are using escape_str. so confirm with each data value data type and with db also.
Related
I am trying to send post request with query params. just like this screenshot
I used Http::post but it supports only json as far as I know. I just want to send request just like the screenshot. cause API data only supports parameter wise. Here What I tried but failed to achieve that.
FormController.php:
public function post_parameter_wise(Request $request,$lp_campaign_id, $lp_campaign_key, $first_name, $last_name, $email, $phone, $zip_code){
$form = new Form();
$fName = $form->first_name = $request->get($first_name);
$lName = $form->last_name = $request->get($last_name);
$cCmail = $form->email = $request->get($email);
$cPhone = $form->phone = $request->get($phone);
$zCode = $form->zip_code = $request->get($zip_code);
$response = Http::post("https://t.vivint.com/post.do", [
"lp_campaign_id" => $lp_campaign_id,
"lp_campaign_key" => $lp_campaign_key,
// "lp_supplier_id" => "",
"first_name" => $fName,
"last_name" => $lName,
"email" => $cCmail,
"phone" => $cPhone,
"zip_code" => $zCode
]);
dd($response);
Can't leave a comment so disregard this as the answer
Preferably, you just need to change it from a Post request to a Get request and it will work
$response = Http::get("https://t.vivint.com/post.do", [...
But a post request will contain the data as a post request should.
If you have to use post and define the query params then you should do it as follows
public function post_parameter_wise(Request $request,$lp_campaign_id, $lp_campaign_key, $first_name, $last_name, $email, $phone, $zip_code){
$form = new Form();
$fName = $form->first_name = $request->get($first_name);
$lName = $form->last_name = $request->get($last_name);
$cCmail = $form->email = $request->get($email);
$cPhone = $form->phone = $request->get($phone);
$zCode = $form->zip_code = $request->get($zip_code);
$response = Http::post("https://t.vivint.com/post.do?lp_campaign_id=".$lp_campaign_id."&lp_campaign_key=".$lp_campaign_key."&first_name=".$fName."&last_name=".$lName."&email=".$cCmail."&phone=".$cPhone."&zip_code=".$zCode, []);
dd($response);
Although this is really just forcing it to
I would like to know if it's possible to use Doctrine to fetch some data in a table which is not an entity.
use Doctrine\DBAL\Driver\Connection;
$connection->fetchAll("SELECT ...");
I tried to use the Connection namespace. this one is working with my entities, but not with the table i want.
This code is actually working, but i'm using PDO to connect the database to execute the query. So the Ajax request is not fast enough. And my SQL query must be prepared to avoid security breaches.
Thanks for your help
/**
* #Route("/api/search", name="map_api_search")
*/
public function search(Connection $connection, Request $request, ObjectManager $manager): Response
{
if ($ajaxRequest = $request->getContent()) {
$requestContent = json_decode($ajaxRequest, true);
$content = $requestContent["content"];
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'smartport',
'user' => 'root',
'password' => '',
'host' => '127.0.0.1:3306',
'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
// Prepare the query
$sql = "SELECT nom, lon, lat, id FROM `chimie_stations2` WHERE nom LIKE '%$content%' AND aasqa = 'PACA'";
// Execute SQL query
$stmt = $conn->query($sql);
//Prepare an array to push all the results from the query
$results = array();
// Processing...
while ($data = $stmt->fetch()) {
$results[] = $data;
}
if (($results)) {
return new JsonResponse([
'result' => true,
'results' => json_encode($results),
]);
} else {
return new JsonResponse([
'result' => false,
]);
}
}
}
If you want to prepare your SQL query :
public function myFunction(EntityManager $entityManager)
{
$connection = $entityManager->getConnection();
$sql = 'SOME SQL HERE';
$stmt = $connection->prepare($sql);
$stmt->execute();
$stmt->fetchAll();
}
I want to use method updateOrcreate(), when data is an empty new record to the database, but when data is existing is updated data,
like image bellow data is empty I want to record new data to database
like image bellow data is existing I want to update existing data and record to database
this is my code
public function simpanDataUn(Request $request)
{
$mapel_ujian_id = $request->mapel_ujian_id;
for($i=0; $i < count($mapel_ujian_id); $i++)
{
$arr = NilaiUjianAKhir::updateOrCreate([
'sekolah_id' => \Auth::user()->sekolah_id,
'siswa_id' => $request->id,
'mapel_ujian_id' => $request->mapel_ujian_id[$i],
'nilai_standar_daerah' => $request->nilai_standar_daerah[$i],
'predikat_standar_daerah' => $request->predikat_standar_daerah[$i],
'nilai_sekolah' => $request->nillai_sekolah[$i],
'predikat_nilai_sekolah' => $request->predikat_nillai_sekolah[$i],
'nilai_akhir' => $request->nilai_akhir[$i],
'predikat_nilai_akhir' => $request->predikat_nilai_akhir[$i],
],
[
'nilai_standar_daerah' => $request->nilai_standar_daerah[$i],
'predikat_standar_daerah' => $request->predikat_standar_daerah[$i],
'nilai_sekolah' => $request->nillai_sekolah[$i],
'predikat_nilai_sekolah' => $request->predikat_nillai_sekolah[$i],
'nilai_akhir' => $request->nilai_akhir[$i],
'predikat_nilai_akhir' => $request->predikat_nilai_akhir[$i],
]);
}
return redirect()->back()->with('success', 'Data UN berhasil di input');
}
I'm using updateOrCreate()method, but when i UPDATED existing data, the data
and even create new data, not updated existing data, like image below:
What part is wrong from my code? how to updated existing data using updateOrCreate() method?
please help me, thanks
Maybe you can change your code like this :
public function simpanDataUn(Request $request)
{
$mapel_ujian_id = $request->mapel_ujian_id;
for($i=0; $i < count($mapel_ujian_id); $i++)
{
$arr = NilaiUjianAKhir::updateOrCreate([
'sekolah_id' => \Auth::user()->sekolah_id,
'siswa_id' => $request->id,
'mapel_ujian_id' => $request->mapel_ujian_id[$i],
],
[
'nilai_standar_daerah' => $request->nilai_standar_daerah[$i],
'predikat_standar_daerah' => $request->predikat_standar_daerah[$i],
'nilai_sekolah' => $request->nillai_sekolah[$i],
'predikat_nilai_sekolah' => $request->predikat_nillai_sekolah[$i],
'nilai_akhir' => $request->nilai_akhir[$i],
'predikat_nilai_akhir' => $request->predikat_nilai_akhir[$i],
]);
}
return redirect()->back()->with('success', 'Data UN berhasil di input');
}
Like #lagbox say the first array passed to updateOrCreate are the where conditions to find a record ... if it can't find a record by those conditions it will create a new one. You won't create the new one when there's same sekolah_id, siswa_id or mapel_ujian_id right? Then the other field can be updated
This should be like this
ID|access1|access2|access3|
and values:
1|1|0|1
//myController
$basic_data = array();
$select_access1 = $_POST("select_access1");
$select_access2 = $_POST("select_access2");
$select_access3 = $_POST("select_access3");
$select_access4 = $_POST("select_access4");
$select_access5 = $_POST("select_access5");
$basic_data[] = array('accs_trans_sec'=>$select_access1,'accs_acctng_sec'=>$select_access2, 'accs_admin_sec'=>$select_access3,'accs_dashboard_sec'=> $select_access4, 'accs_reports_sec'=>$select_access5);
$this->RoleModel->saveRole($basic_data);
//myModel
public function saveRole($basic_data)
{
foreach($basic_data as $value) {
$this->db->insert('roles_global_access', $basic_data);
}}
You can set that data to array just like this:
$data = array(
'column1' => 'My Value 1',
'column2' => 'My Value 2',
'column3' => 'My Value 3'
);
$this->db->insert("table_name", $data);
Let's assume that you are getting the values of your checkbox based on your $_POST variables.
Since you've declared $basic_data as array() no need to cast it as $basic_data[].
So on your controller it should be like this:
$basic_data = array(
'accs_trans_sec'=>$select_access1,
'accs_acctng_sec'=>$select_access2,
'accs_admin_sec'=>$select_access3,
'accs_dashboard_sec'=> $select_access4,
'accs_reports_sec'=>$select_access5
);
And your model there's no need to use loop since you are inserting Object data it should look like this:
public function saveRole($basic_data)
{
$this->db->insert('roles_global_access', $basic_data);
return ($this->db->affected_rows() != 1) ? false : true;
}
so basically, if the model returns true then it successfully inserted the data.
To check if data is inserted successfully:
$result = $this->RoleModel->saveRole($basic_data);
if($result == true){
echo ("Successfully inserted!");
}else{
echo ("Problem!");
}
First, you are not getting post data in the correct way. With $_POST have to use square brackets [].
Second, Don't use foreach loop in the model
Get data in the controller like this
$basic_data = array(
'accs_trans_sec' => $_POST['select_access1'],
'accs_acctng_sec' => $_POST['select_access2'],
'accs_admin_sec' => $_POST['select_access3'],
'accs_dashboard_sec' => $_POST['select_access4'],
'accs_reports_sec' => $_POST['select_access5']
);
$this->RoleModel->saveRole($basic_data);
Model
public function saveRole($basic_data){
return $this->db->insert('roles_global_access', $basic_data);
}
You should try this.
Controller:
$this->RoleModel->saveRole($_POST);
Model:
public function saveRole($basic_data){
extract($basic_data);
$dataset = array(
'accs_trans_sec' => $basic_data['select_access1'],
'accs_acctng_sec' => $basic_data['select_access2'],
'accs_admin_sec' => $basic_data['select_access3'],
'accs_dashboard_sec' => $basic_data['select_access4'],
'accs_reports_sec' => $basic_data['select_access5']
);
$this->db->insert('roles_global_access', $dataset);
}
controller
i need to insert store_merchant_id from $data..but in my coding it takes only the first id..
$data = array(
'merchant_firstname' => $merchant_firstname,
'merchant_lastname' => $merchant_lastname,
'merchant_password' => $merchant_password,
'merchant_email'=>$merchant_email,
);
$merchant_id = Merchant_model::merchant_submit($data);
$data1 = array(
'store_merchant_id'=>$merchant_id,
'store_name'=>$store_name,
'store_phone'=>$store_phone,
'store_address1'=>$store_address1,
);
$return = Merchant_model::merchant_submit1($data1);
Model
return DB::table('le_store')->insert($data1);
I guess you need to get the last inserted id and do something with that
So, i am doing the same what you did
Step 1 : Building the input
1.
$data = array(
'merchant_firstname' => $merchant_firstname,
'merchant_lastname' => $merchant_lastname,
'merchant_password' => $merchant_password,
'merchant_email'=>$merchant_email,
);
Step 2 : Saving the Data
$Merchant = Merchant_model::create($data);
Step 3 : Getting the inserted id
return $Merchant->id;
Now the $Merchant->id will have the last inserted id, You shall return or save this to another model that you wish to do.
Update :
As you want to update the inserted id to another table, Just do this
As usual like previous
$data = array(
'merchant_firstname' => $merchant_firstname,
'merchant_lastname' => $merchant_lastname,
'merchant_password' => $merchant_password,
'merchant_email'=>$merchant_email,
);
$Merchant = Merchant_model::create($data);
Then build $data1
$data1 = array(
'store_merchant_id'=>$Merchant->id,
'store_name'=>$store_name,
'store_phone'=>$store_phone,
'store_address1'=>$store_address1,
);
Note :
store_merchant_id'=>$Merchant->id' should be like this
Then just create it
Merchant_model::create($data1);
Hope this helps you