Can't update data in Sqlite3 php - php

I'm trying to make simple api using php .
everything goes smoothly except updating .
here is my code code for index (route):
$router->post('/api/update/{id}', function ($id) use ($product) {
// $_POST['id'] = $id;
echo $product->update($_POST);
//var_dump($_POST);
});
and here is my code update method in products class :
public function update(array $data) {
/** #var TYPE_NAME $id */
$query = $this->db->prepare("UPDATE products SET name= ?, price= ? ,amount= ? WHERE id= ?");
$query->execute(array($data['name'], $data['price'], $data['amount'],$data['id']));
return json_encode(["msg" => "The Product was updated successfully."]);
//return json_encode($data);
}
when I make post request I get this error :
Warning: Undefined array key "name" in /storage/emulated/0/www/products_manager/app/Products.php on line 52
Warning: Undefined array key "price" in /storage/emulated/0/www/products_manager/app/Products.php on line 52
Warning: Undefined array key "amount" in /storage/emulated/0/www/products_manager/app/Products.php on line 52
Warning: Undefined array key "id" in /storage/emulated/0/www/products_manager/app/Products.php on line 52
{"msg":"The Product was updated successfully."}
I try to update product data in my SQLITE3 databases.

Related

TYPO3 V11 : "PHP Warning: Undefined array key" , $this->request->getArguments() is empty

I'm a new user of typo3 and I made a plugin to display users with a searchbar to filter them, but I have this error when I want to display my page :
(1/1) #1476107295 TYPO3\CMS\Core\Error\Exception
PHP Warning: Undefined array key "word" in MyPath/Controller/UserlistController.php line 44
In my controller, I try to get arguments to use it in my filter like this :
public function listAction(int $currentPage = 1)
{
$arguments = $this->request->getArguments();
$users = $this->userlistRepository->findBySearch($arguments['word'] ? $arguments['word'] : '');
somecode ...
}
I tried a dump of $arguments, but it's empty
there is a part of my repository :
/**
* #param string $word
* #return object[]|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* #throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function findBySearch(string $word) {
$query = $this->persistenceManager->createQueryForType(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser::class);
$querySettings = $query->getQuerySettings();
$querySettings->setStoragePageIds([26]);
$query->setQuerySettings($querySettings);
$query->setOrderings([
'lastName' => QueryInterface::ORDER_ASCENDING
]);
Someone know why I can't get arguments ? thanks
If you need more part of code, please tell me
If you just call the List action without sending the filter form the arguments are empty.
You should test each expected argument before accessing it like this:
if($this->request->hasArgument('word')) {
$searchOption = $this->request->getArgument('word'));
}
You could try using the null coalescencing operator here:
$users = $this->userlistRepository->findBySearch($arguments['word'] ?? '');
If that doesn't do the trick, you check if the value is set before (see the answer of Tobias Gaertner). Also keep in mind that
$this->request->getArguments();
only gets the arguments in your current plugin context, so any parameters looking like tx_yourplugin_p1[argument]. You have to make sure you properly pass this parameter. If you have to use the general GET parameters, you can use
\TYPO3\CMS\Core\Utility\GeneralUtility::_GET();
While the handling of the 'word' variable is one issue, I consider that if no search word is sent, perhaps another Repository-Method should be called.
Below I entered $this->userlistRepository->findAll() but it could also show something else, perhaps just a template that states that a choice is required.
public function listAction(int $currentPage = 1)
{
$arguments = $this->request->getArguments();
if($this->request->hasArgument('word')) {
$searchOption = $this->request->getArgument('word');
$users = $this->userlistRepository->findBySearch($searchOption);
}
else {
$users = $this->userlistRepository->findAll();
somecode ...
}
}
I tried all solutions, it turns out the error is coming from my repository, I tried a simple
$users = $this->userlistRepository->findAll();
and I got this error:
HP Warning: Undefined array key "tx_vendor_domain_model_vendor" in /mypath/htdocs/public/typo3/sysext/extbase/Classes/Persistence/Generic/Mapper/DataMapFactory.php line 224

How to solve Message: Trying to get property of non-object error in Codeigniter?

I'm very fresher in CodeIgniter and practicing. I'm now developing a simple Codeigniter application just for practicing. I've Banks and its branches in the database. I just want to show branches with its bank name. Branches are showing but in the controller while getting banks, this error is showing. I tried these in SO links, but nothing found works.
"A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object"
in Line Number: 85 and 91
This is my controller
function ShowBranchList() {
$branch_list = $this->FD_Model->get_all_branches();
$get_bank = $this->FD_Model->get_bank_by_branch($branch_list->bankid); //This is Line Number 85
if($branch_list ){
$data = array(
'pagetitle' => 'Branch List View',
'branch_list_data' => $branch_list,
'br_bank' => $get_bank['bank_name'],//This is Line Number 93
);
}
Model
/*function for getting all branches*/
function get_all_branches() {
$this->db->order_by( $this->brid, $this->order );
return $this->db->get( $this->brtable )->result();
}
/*function for getting banks by branches*/
function get_bank_by_branch( $id ) {
$this->db->where( $this->bid, $id);
return $this->db->get( $this->banktable )->row();
}
And finally, this is my View
foreach ($branch_list_data as $branch_list)
{
<?php echo $branch_list->brname ?>
<?php echo $br_bank; ?>
}
Anyone know the issue?
Update
When I'm tring to view the query
echo $this->db->last_query();
Result
SELECT * FROM tbl_bankmaster WHERE bid IS NULL
The value $branch_list->bankid is null.When I'm hardcoding some values the view page is showing without any error.
do like this
$get_bank = $this->FD_Model->get_bank_by_branch($branch_list[0]->bankid);
AND
'br_bank' => $get_bank[0]->bank_name # this will work maybe
or
'br_bank' => $get_bank[0]['bank_name']
Its 0 indexed array so you need to add the key to access the child's
Change your data in view like this
foreach ($branch_list_data as $branch_list)
{
echo $branch_list['brname']; //change your data like this
echo $br_bank;
}
I finally make it happen with the help of Abdulla Nilam's answer. I just passed the value in array like
'br_bank' => $get_bank->bank_name
Now the view is working.
get_bank_by_branch($branch_list->bankid); This method returning only object.
$get_bank this variable you're accessing by an array, but it's an object.

Call to undefined method HTML_neolegal::legals()

I converted successfully a Joomla-1 into PHP-5.6 except one component, which gives me this error:
PHP Fatal error: Call to undefined method HTML_neolegal::legals() in ../administrator/components/com_neolegal/admin.neolegal.php on line 64
And line 58-65 contains:
// id := 1 (only one record)
function legals( $option ) {
global $database;
$id = 1;
$row = new mosNeolegal ( $database );
$row->load( $id );
HTML_neolegal::legals( $row, $option );
}
I know it's because i use a to new PHP version, but i need a workaround for this. It's not possible to downgrade.
I think that there are two functions with the same name "legals".
Try to find the legals function on the HTML_neolegal Class and change the function name to for example legals_new
then change the call to HTML_neolegal::legals_new( $row, $option )...

Magento Fatal Error when editing customer accounts

I have Magento 1.7.0.2 and when I try to edit a customer account in the admin panel i get this error:
Fatal error: Call to a member function setRenderer() on a non-object in /home/techspec/public_html/magento/includes/src/Mage_Adminhtml_Block_Customer_Edit_Tab_Account.php on line 77
$attributes = $customerForm->getAttributes();
foreach ($attributes as $attribute) {
/* #var $attribute Mage_Eav_Model_Entity_Attribute */
$attribute->setFrontendLabel(Mage::helper('customer')->__($attribute->getFrontend()->getLabel()));
$attribute->unsIsVisible();
}
$disableAutoGroupChangeAttributeName = 'disable_auto_group_change';
$this->_setFieldset($attributes, $fieldset, array($disableAutoGroupChangeAttributeName));
$form->getElement('group_id')->setRenderer($this->getLayout()
->createBlock('adminhtml/customer_edit_renderer_attribute_group')
->setDisableAutoGroupChangeAttribute($customerForm->getAttribute($disableAutoGroupChangeAttributeName))
->setDisableAutoGroupChangeAttributeValue($customer->getData($disableAutoGroupChangeAttributeName)));
if ($customer->getId()) {
$form->getElement('website_id')->setDisabled('disabled');
$form->getElement('created_in')->setDisabled('disabled');
} else {
$fieldset->removeField('created_in');
$form->getElement('website_id')->addClass('validate-website-has-store');
That is line 67 to 87 of the referenced file. Would love any help. Thanks.
looks like the attribute group_id is not in your list of editable attributes. Make sure the attribute is visible. Use this select to check.
SELECT * FROM `eav_attribute` e
LEFT JOIN `customer_eav_attribute` ce ON e.attribute_id = ce.attribute_id
WHERE e.attribute_code = 'group_id'
See the value of field is_visible.
If you don't get any result from the query above then you are in trouble.
If that is 1, maybe the attribute is not set to be shown in the admin form. Get the attribute_id returned by the previous query and do this. Let's say the value is 10.
select * from customer_form_attribute where attribute_id = 10;
If there is no record with form_code = adminhtml_customer then you should add it.

php associative array of objects

I'm trying to create associative array of objects from row result set with member id as the key, but getting some error.
addATravelog() is just a function of the class UserLogsAndSOS(), whose objects i want in array.
Here is what I tried:
class UserArraySet {
private $arrayOfUsers = array();
function createArrayForTravelogs($result) {
While($row = $result->next()) {
if(array_key_exists($row['id'], $this->arrayOfUsers)) {
$this->arrayOfUsers[$row['id']] = new UserLogsAndSOS();
}
$this->arrayOfUsers[$row['id']]->addATravelog($row['title'], $row['blog']); //line 72
}
}
}
On calling createArrayForTravelogs() from the object I got the following error
Here is the error I got:
Notice: Undefined index: 1 in C:\xampp\htdocs\site\classes\userprofile.php on line 72
Fatal error: Call to a member function addATravelog() on a non-object in C:\xampp\htdocs\site\classes\userprofile.php on line 72
Can someone please let me know how to achieve this, I want something like this:
Array (
[1] => objectUserLogsAndSOS1
[5] => objectUserLogsAndSOS2
....
)
where key is the member id from $row.
I also need to check if the key exists, then call a function of that particular object to add data to its member, if not then create an object and then call a function of that particular object to add data to its member.
Thanks
just read the error message: you only create UserLogsAndSOS if there is already an entry - otherwise you call addATravelog on null.
maybe you forgot the "!" in your if clause?
Because the array stays empty.
You only create a new UserLogsAndSOS when there already is an element with the provided ID in the arrayOfUsers. The exact opposite of what you probably wanted.
You're probably missing a ! to reverse the array_key_exist result.
if(!array_key_exists($row['id'], $this->arrayOfUsers)) {
$this->arrayOfUsers[$row['id']] = new UserLogsAndSOS();
}
You missed and '!' I think this is causing the error

Categories