Does anyone here know why I can't insert my data in my payment table? I just want to insert it but I'm getting this error:
Uncaught Error: Cannot use object of type stdClass as array in C:\xampp\htdocs\acz-thesis\app\models\account.php:565
Line 565: $total_amount = $row['sales_net_amount'];
Controller
public function InsertPaymentSales() {
if(isset($_SESSION['token']) == $this->input->post('token')) {
$data = array(
'payment_amount' => $this->input->post('payment_amount'),
'payment_date' => $this->input->post('payment_date'),
'payment_remark' => $this->input->post('payment_remark'),
'payment_balance' => $this->input->post('payment_balance'),
'payment_sales_id' => $this->input->post('payment_sales_id')
);
$this->model('account')->sales_payment($data);
}
}
Model
public function sales_payment($data) {
$payment_amount = $data['payment_amount'];
$payment_date = $data['payment_date'];
$payment_remark = $data['payment_remark'];
$payment_balance = $data['payment_balance'];
$payment_sales_id = $data['payment_sales_id'];
$query = $this->db->query("SELECT sales_net_amount, sales_balance FROM tbl_sales_details WHERE sales_id = $payment_sales_id");
$row = $query->fetch_object();
$total_amount = $row['sales_net_amount'];
$balance = $row['sales_balance'];
if($payment_amount >= $payment_balance && $balance > 0) {
$query = $this->db->query("INSERT INTO tbl_sales_payments (payment_amount, payment_date, sales_id, payment_remarks)
VALUES($payment_amount, '$payment_date', $payment_sales_id, '$payment_remark')");
if($query) {
$query = $this->db->query("SELECT SUM(payment_amount) as total_payments FROM tbl_sales_payments WHERE sales_id = $payment_sales_id");
$row = $query->fetch_object();
$total_payments = $row['total_payments'];
$total_balance = $total_amount - $total_payments;
$query = $this->db->query("UPDATE tbl_sales_details SET sales_balance = $total_balance WHERE sales_id = $payment_sales_id");
$message = 'Success';
$query ? notify('success', $message, true) : null;
}
} else {
$message = 'Error';
notify_amaran([false,'#4caf50','#fff',$message]);
}
}
You can try this :
$row = $query->fetch(); $total_amount = $row['sales_net_amount'];
I think that using fetch_object() on $query returns an object instead of an associative array that you want to use with $row['sales_net_amount'].
As #Definitely not Rafal has already commented you cannot access attributes as array
These lines are wrong
$total_amount = $row['sales_net_amount'];
$balance = $row['sales_balance'];
[..]
$total_payments = $row['total_payments'];
change those to
$total_amount = $row->sales_net_amount;
$balance = $row->sales_balance;
[..]
$total_payments = $row->total_payments;
Related
I have this problem, I have to update a record of a table that has the values of a serialized column. the call to the function works and passes the data correctly. I can enter the record, but I can not update. This is my code:
public function update($user_id, $array_check)
{
$arrayB = array();
$array_check = unserialize($array_check);
foreach ($array_check $key => $value) {
if($value["id"] == $array_check){
$idRow = $value["id"];
if($value["value"] == "0"){
$valueRow = "1";
}else{
$valueRow = "0";
}
}else{
$idRow = $value["id"];
$valueRow = $value["value"];
}
$row = array("id" => $idRow, "value" => $valueRow);
$arrayB[] = $row;
}
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = :docs_selected WHERE user_id = :user_id');
$row = $stmt->execute(array(':user_id' => $user_id, ':docs_selected' => serialize($arrayB) ) );
return $arrayB;
}
edit.
Replace this:
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = :docs_selected WHERE user_id = :user_id);
with:
$deseralized = serialize($arrayB);
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = '$deseralized ' WHERE user_id = '$user_id');
I have this code :
public function updateSegmentGender ($product_id, $segment_gender) {
$this->connect();
$product_id = $this->escapeString($product_id);
$row_count = count($segment_gender);
for ($row = 0; $row < $row_count; $row++) {
$this->select('product_seg_gender', '*', NULL,
'product_id = "'.$product_id.'" AND gender = "'.$segment_gender[$row][0].'"', NULL, NULL);
$res = $this->getResult();
$res = count($res);
$gender = $segment_gender[$row][0];
$status = $segment_gender[$row][1];
if ($res <> 0) {
// UPDATE
$data = array ('status'=>$status);
$this->update('product_seg_gender', $data, 'product_id = "'.$product_id.'" AND gender = "'.$gender.'"');
}else {
// INSERT
$data = array ('product_id'=>$product_id, 'gender'=>$gender, 'status'=>$status);
$this->insert('product_seg_gender', $data);
}
}
}
and I'm using that method like this :
$user = new Product($db_server, $db_user, $db_password, $db_name);
$user->connect();
$segment_gender = array ( array ("all", "active"),
array ("female", "active"));
$res = $user->updateSegmentGender ('303', $segment_gender);
print_r($res);
but why I always got this error message :
Warning: Cannot use a scalar value as an array in /home/***/public_html/class/Database.class.php on line 130
however, the database is successfully updated. what did I do wrong?
UPDATE : here's complete line 97-145 of Database.class.php
// Function to SELECT from the database
public function select($table, $rows = '*', $join = null, $where = null, $order = null, $limit = null){
// Create query from the variables passed to the function
$q = 'SELECT '.$rows.' FROM '.$table;
if($join != null){
$q .= ' JOIN '.$join;
}
if($where != null){
$q .= ' WHERE '.$where;
}
if($order != null){
$q .= ' ORDER BY '.$order;
}
if($limit != null){
$q .= ' LIMIT '.$limit;
}
// echo $table;
$this->myQuery = $q; // Pass back the SQL
// Check to see if the table exists
if($this->tableExists($table)){
// The table exists, run the query
$query = $this->myconn->query($q);
if($query){
// If the query returns >= 1 assign the number of rows to numResults
$this->numResults = $query->num_rows;
// Loop through the query results by the number of rows returned
for($i = 0; $i < $this->numResults; $i++){
$r = $query->fetch_array();
$key = array_keys($r);
for($x = 0; $x < count($key); $x++){
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x])){
if($query->num_rows >= 1){
$this->result[$i][$key[$x]] = $r[$key[$x]];
}else{
$this->result[$i][$key[$x]] = null;
}
}
}
}
return true; // Query was successful
}else{
array_push($this->result,$this->myconn->error);
return false; // No rows where returned
}
}else{
return false; // Table does not exist
}
}
Note: I hope you require $res = $user->updateSegmentGender ('303', $segment_gender); need $data value and based on assumption I use $data for return part.
Perhaps due to $data not initialized. I'm trying to declaring the variable $data, as an array, before using it I also make another change that function updateSegmentGender() require return part so I put this.
public function updateSegmentGender ($product_id, $segment_gender) {
$this->connect();
$product_id = $this->escapeString($product_id);
$row_count = count($segment_gender);
$data = array();//Initialize variable...
for ($row = 0; $row < $row_count; $row++) {
$this->select('product_seg_gender', '*', NULL,
'product_id = "'.$product_id.'" AND gender = "'.$segment_gender[$row][0].'"', NULL, NULL);
$res = $this->getResult();
$res = count($res);
$gender = $segment_gender[$row][0];
$status = $segment_gender[$row][1];
if ($res <> 0) {
// UPDATE
$data = array ('status'=>$status);
$this->update('product_seg_gender', $data, 'product_id = "'.$product_id.'" AND gender = "'.$gender.'"');
}else {
// INSERT
$data = array ('product_id'=>$product_id, 'gender'=>$gender, 'status'=>$status);
$this->insert('product_seg_gender', $data);
}
}
//Return funal value in array format...
return $data;
}
I have user_sport_tbl,sport_tbl tables. in user_sport_tbl i have sport_id but not have sport name.Sport name is in sport_tbl.
i have a function of getUserSport() of Model.
my query :-
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
i want sport_name for every sport_id and want to merge sport_name in array.
my code :-
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if($query->num_rows > 0)
{
foreach($query->result() as $item)
{
$query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item->sport_name order by sport_id limit 1");
$sql1 = $query1->row();
$data[] = array_push($item, $sql1->sport_name);
}
return $data;
}
}
my present return data:-
[user_match_id] => 7 [sport_id] => 2 [match_id] => 3 [username] => admin
i want to add [sport_name] => cricket.
I want to return an array with sportname
Try this coding
public function getUserSport()
{
$data = array();
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if($query->num_rows > 0)
{
foreach($query->result() as $key=>$item)
{
$query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item->sport_name order by sport_id limit 1");
$sql1 = $query1->row();
$item[$key]['sport_name'] = $sql1->sport_name;
$data[] = $item;
}
return $data;
}
}
Try This
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if(empty($query))
{
foreach($query->result_array() as $item)
{
$sportsId = $item[0]['sport_name'];
$query1 = $this->db->query("SELECT sport_name FROM sport_tbl WHERE sport_id = $sportsId ORDER BY sport_id LIMIT 1");
$result = $query1->result_array();
$data[] = array_push($item, $result[0]['sport_name']);
}
return $data;
}
}
Or can do with join query too (Not tested. If any error fix it.
or post the message her)
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->query(
"SELECT user_sport_tbl.sport_name, sport_tbl.sport_name
FROM user_sport_tbl
INNER JOIN sport_tbl
ON user_sport_tbl.username = sport_tbl.sport_name
WHERE user_sport_tbl.username = '$username' ORDER BY sport_tbl.sport_id LIMIT 1");
$result = $query->result_array();
return $data;
}
i am beginner in php and codeigniter.can anyone make this php programme as a codeigniter format?
if ($user_id > 0){
$follow = array();
$fsql = "select user_id from following
where follower_id='$user_id'";
$fresult = mysql_query($fsql);
while($f = mysql_fetch_object($fresult)){
array_push($follow, $f->user_id);
}
if (count($follow)){
$id_string = implode(',', $follow);
$extra = " and id in ($id_string)";
}else{
return array();
}
if ($user_id > 0)
{
$follow = array();
$qry = "select user_id from following where follower_id='$user_id'";
$res = $this->db->query(qry);
foreach($res->result() as $row)
{
array_push($follow, $row->user_id);
}
if (count($follow))
{
$id_string = implode(',', $follow);
$extra = " and id in ($id_string)";
}
else
{
return array();
}
}
Try this:
if ($user_id > 0){
$follow = array();
$rs = $this->db->select('user_id')->where('follower_id', $user_id)->get('following')->result_array();
if( is_array( $rs ) && count( $rs ) > 0 ){
$follow = array();
foreach( $rs as $key => $each ){
$follow[] = $each['user_id'];
}
}
if (count($follow)){
$id_string = implode(',', $follow);
$extra = " and id in ($id_string)";
}else{
return array();
}
}
This should be done in the model.
Using avtice record you can do it like this
function getFriends($user_id){
return $this->db
->select('user_id')
->where('follower_id',$user_id)
->get('following')
->result_array() // for single object use row_array()
}
I am passing a number of values to a function and then want to create a SQL query to search for these values in a database.
The input for this is drop down boxes which means that the input could be ALL or * which I want to create as a wildcard.
The problem is that you cannot do:
$result = mysql_query("SELECT * FROM table WHERE something1='$something1' AND something2='*'") or die(mysql_error());
I have made a start but cannot figure out the logic loop to make it work. This is what I have so far:
public function search($something1, $something2, $something3, $something4, $something5) {
//create query
$query = "SELECT * FROM users";
if ($something1== null and $something2== null and $something3== null and $something4== null and $something5== null) {
//search all users
break
} else {
//append where
$query = $query . " WHERE ";
if ($something1!= null) {
$query = $query . "something1='$something1'"
}
if ($something2!= null) {
$query = $query . "something2='$something2'"
}
if ($something3!= null) {
$query = $query . "something3='$something3'"
}
if ($something4!= null) {
$query = $query . "something4='$something4'"
}
if ($something5!= null) {
$query = $query . "something5='$something5'"
}
$uuid = uniqid('', true);
$result = mysql_query($query) or die(mysql_error());
}
The problem with this is that it only works in sequence. If someone enters for example something3 first then it wont add the AND in the correct place.
Any help greatly appreciated.
I would do something like this
criteria = null
if ($something1!= null) {
if($criteria != null)
{
$criteria = $criteria . " AND something1='$something1'"
}
else
{
$criteria = $criteria . " something1='$something1'"
}
}
... other criteria
$query = $query . $criteria
try with array.
function search($somethings){
$query = "SELECT * FROM users";
$filters = '';
if(is_array($somethings)){
$i = 0;
foreach($somethings as $key => $value){
$filters .= ($i > 0) ? " AND $key = '$value' " : " $key = '$value'";
$i++;
}
}
$uuid = uniqid('', true);
$query .= $filters;
$result = mysql_query($query) or die(mysql_error());
}
// demo
$som = array(
"something1" => "value1",
"something2" => "value2"
);
search( $som );
Here's an example of dynamically building a WHERE clause. I'm also showing using PDO and query parameters. You should stop using the deprecated mysql API and start using PDO.
public function search($something1, $something2, $something3, $something4, $something5)
{
$terms = array();
$values = array();
if (isset($something1)) {
$terms[] = "something1 = ?";
$values[] = $something1;
}
if (isset($something2)) {
$terms[] = "something2 = ?";
$values[] = $something2;
}
if (isset($something3)) {
$terms[] = "something3 = ?";
$values[] = $something3;
}
if (isset($something4)) {
$terms[] = "something4 = ?";
$values[] = $something4;
}
if (isset($something5)) {
$terms[] = "something5 = ?";
$values[] = $something5;
}
$query = "SELECT * FROM users ";
if ($terms) {
$query .= " WHERE " . join(" AND ", $terms);
}
if (defined('DEBUG') && DEBUG==1) {
print $query . "\n";
print_r($values);
exit();
}
$stmt = $pdo->prepare($query);
if ($stmt === false) { die(print_r($pdo->errorInfo(), true)); }
$status = $stmt->execute($values);
if ($status === false) { die(print_r($stmt->errorInfo(), true)); }
}
I've tested the above and it works. If I pass any non-null value for any of the five function arguments, it creates a WHERE clause for only the terms that are non-null.
Test with:
define('DEBUG', 1);
search('one', 'two', null, null, 'five');
Output of this test is:
SELECT * FROM users WHERE something1 = ? AND something2 = ? AND something5 = ?
Array
(
[0] => one
[1] => two
[2] => five
)
If you need this to be more dynamic, pass an array to the function instead of individual arguments.