I am parsing a CSV file to add records to a table. this is how I do it.
public function parse_csv($file) {
$this->load->library('CSVReader');
$csvData = $this->csvreader->parse_file($file);
foreach($csvData as $key => $row) {
$data_n[$key] = array(
'mf_date' => $row['mf_date'],
'mf_student_id' => $row['mf_student_id'],
'mf_sender' => $row['mf_sender'],
'mf_amount' => $row['mf_amount'],
'mf_trx_id' => $row['mf_trx_id'],
);
$this->db->insert('monthly_fee', $data_n[$key]);
$this->student_model->update_monthly_due($row['mf_student_id'], $row['mf_amount']);
}
}
I wish to update another table where there is three columns
1)md_student_id //unique
2)md_due
3)md_paid
I want to update that table while uploading the csv where mf_student_id and md_student_id matches with the model function below.
public function update_monthly_due($mf_student_id, $mf_amount)
{
$this->db->select('mf_student_id');
$this->db->from('monthly_fee');
$this->db->join('monthly_due', 'monthly_due.md_student_id = monthly_fee.mf_student_id');
$sql = "UPDATE monthly_due set md_due = md_due - " . $mf_amount . ", md_paid = md_paid + " . $mf_amount . " WHERE md_student_id = " . $mf_student_id;
$this->db->query($sql);
}
But when I upload a csv it generates the following error.
Error Number: 1054
Unknown column 'MCS20145B41' in 'where clause'
UPDATE monthly_due set md_due = md_due - 5500, md_paid = md_paid + 5500 WHERE md_student_id = MCS20145B41
Filename: F:\xampp\htdocs\student\system\database\DB_driver.php
Line Number: 330
What I am doing wrong?!
Thanks in advance
you should replace this line as follow
$sql = "UPDATE monthly_due set md_due = md_due - " . $mf_amount . ", md_paid = md_paid + " . $mf_amount . " WHERE md_student_id = '" . $mf_student_id . "'";
take attention to the end of this line
Why not use CI's active record instead of raw SQL queries.When doing it do it the correct way. This below:
Change this,
$sql = "UPDATE monthly_due set md_due = md_due - " . $mf_amount . ", md_paid = md_paid + " . $mf_amount . " WHERE md_student_id = " . $mf_student_id;
$this->db->query($sql);
To,
$this->db->where('md_student_id', $mf_student_id);
$this->db->set('md_due', "md_due - $mf_amount", FALSE);
$this->db->set('md_paid', "md_paid + $mf_amount", FALSE);
$this->db->update('monthly_due');
Explanation:
The reason this works is because the third (optional) FALSE parameter tells CodeIgniter not to protect the generated query with backticks ('). This means that the generated SQL will be:
UPDATE monthly_due set md_due = md_due - 5500, md_paid = md_paid + 5500 WHERE md_student_id = "MCS20145B41"
Related
How to write this query in laravel? this query was used in Codeigniter
$ci->db->query("SELECT ratings.rating_id," . $table . "." . $namefield . " as thenamefield,ROUND(AVG(ratings.rating_num),2) as rating
FROM ratings," . $table . " WHERE " . $table . "." . $idfield . " = ratings.rating_id GROUP BY rating_id
ORDER BY rating DESC LIMIT " . $limit . "");
try this
$select_part = $table . "." . $namefield;
DB::table('ratings')
->select('ratings.rating_id',DB::raw("{$select_part} as thenamefield"),DB::raw("ROUND(AVG(ratings.rating_num)2) as rating"))
->crossJoin($table)
->whereRaw("{$select_part} = ratings.rating_id")
->groupBy("rating_id")
->orderByRaw("rating DESC")
->limit($limit)
->get();
I stored a Controller#Action in the database, but when I try to run it it don't has value.
I know that eval is very dangerous. If there is an alternative what I can use in this situation I'm open for it.
My code where I wan't to run it:
//$action = "ExchangeController::module_getAllValutaByBank";
//$params = "K&H";
$test = eval('\App\Http\Controllers\ModuleControllers' . "\\" . $action . "('" . $params . "');");
Debugbar::info($test);
The code what I want to eval:
It's an API request to the local ForexChange site. #param bank's name
#return stuctured string
public static function module_getAllValutaByBank($bankName){
$return = '';
$data['bankName'] = $bankName;
$response = self::getRequest($data);
if(is_array($response)){
$return .= $response[0]['bank'] . "\n";
foreach($response as $key => $item){
$return .= $item['valuta'] . " - " . round($item['buy'], 2) . " - " . round($item['sell'], 2) . "\n";
}
} else {
$return = $response;
}
Debugbar::info($return);
return $return;
}
Output of Debugbar::info($return);
K&H Bank GBP - 338.51 - 363.07 AUD - 189.9 - 207.8 DKK - 39.75 - 43.93
JPY - 2.26 - 2.48 CAD - 188.73 - 206.51 NOK - 30.92 - 34.18 CHF -
256.89 - 275.53 SEK - 29.18 - 32.26 USD - 246.14 - 260.32 CZK - 11.51 - 12.97 PLN - 69.87 - 78.79 EUR - 302.92 - 320.38 HRK - 39.39 - 44.41
Output of Debugbar::info($test);
null
Where it went wrong?
EDIT:
Found the solution:
$test = call_user_func('\App\Http\Controllers\ModuleControllers' . "\\" . $action, $params));
Found the solution:
$test = call_user_func('\App\Http\Controllers\ModuleControllers' . "\\" . $action, $params));
Since around March of this year our osTicket system started posting duplicate tickets and duplicate threads.
I've tried everything method I know of to troubleshoot and identity where and why this is occurring. I've even implemented a catch condition to check the db tables prior to inserting tickets and threads to see if the contents of the $vars[]'s match what is already in the DB tables.
No success to solve this problem whatsoever.
I've Googled it and see where this issue has been in existence and dates back a number of years.
Does any one have a solution on how to stop osTicket from consistently creating ducpliate tickets and threads?
I tried the following from piecing together suggestions others have made out there in the various osTicket up through v1.10. Here is the code I tried. It actually worked for 2-days straight, then began to fail again yesterday.
Inserted around line 2498 in ./include/class.ticket.php right before if($errors) return 0;.
/**
* J.Doe added to intercept duplicate ticket entries.
* Updated: 2017-06-12
* #author John Doe <jd#example.com>
*/
$sql1='
SELECT ticketID FROM ost_ticket
WHERE
source = "' . $vars['source'] . '"
AND topic_id = "' . $vars['topicId'] . '"
AND url = "' . $vars['url'] . '"
AND priority_id = "' . $vars['priorityId'] . '"
AND duedate = "' . date( 'Y-m-d H:i:s', strtotime( $vars['duedate'] . ' ' . $vars['time'] ) ) . '"
AND team_id = "' . $vars['assignId'] . '"
AND subject = "' . $vars['subject'] . '"
AND dept_id = "' . $vars['deptId'] . '"
AND email = "' . $vars['email'] . '"
AND name = "' . $vars['name'] . '"
';
$sql2='
SELECT id FROM ost_ticket_thread
WHERE
staff_id = "' . $vars['assignId'] . '"
AND poster = "' . $vars['name'] . '"
AND title = "' . $vars['subject'] . '"
AND body = "' . $vars['message'] . '"
';
$res1=db_query($sql1);
$res2=db_query($sql2);
if( ( $res1 && db_num_rows( $res1 ) ) || ( $res2 && db_num_rows( $res2 ) ) ) {
header( 'Location: http://example.com/workorders/' );
exit;
}
//Any error above is fatal.
if($errors) return 0;
My website shows the following error when host updates the php version
Warning: preg_match(): No ending delimiter '^' found in
in the following code:
<?php
$mid_str = " and mid != '0' and ";
if($_REQUEST['search']){
mysql_select_db($database_myconn, $myconn);
$query_spages = "SELECT id, url, title, description, keywords, active, ip, catID, exp, pdate,
MATCH(title,description,keywords)
AGAINST ('$search_str' IN BOOLEAN MODE) AS score FROM pages
WHERE MATCH(title, description,keywords)
AGAINST ('$search_str' IN BOOLEAN MODE) " . $mid_str . " active = 'Yes' ORDER BY score DESC";
}else// not search fetch rand by catid
$query_spages = "SELECT * FROM pages where " . preg_match("^ and", "", $mid_str) . " active = 'Yes' and catID = '" . $_REQUEST['id'] . "' ORDER BY mid DESC";
mysql_select_db($database_myconn, $myconn);
$spages = mysql_query($query_spages, $myconn) or die(mysql_error());
//$row_spages = mysql_fetch_assoc($spages);
unset($settings);
$settings = mysql_fetch_assoc(mysql_query('select * from settings where id = 1',$myconn));
?>
preg_match expects a start and an end of the regular expression.
For example preg_match("/^ and/", "", $mid_str).
In your case, ^ is taken as start delimiter but the regex does not end with ^ so you get an error. Start and end delimiter can be anything, but most likely /is used to not clash with other specially treated characters.
Also, you probably mixed up preg_matchand preg_replace. I think you want to preg_replacehere, p.e.
$query_spages = "SELECT * FROM pages where " . preg_replace("/^ and/", "", $mid_str) . " active = 'Yes' and catID = '" . $_REQUEST['id'] . "' ORDER BY mid DESC";
Hi i have to pass parameter to a report...with a concat-date ....but its showing error...
like this...am having in list box with 2011-2012,2012-2013...here is my oracle query
DATE FOMART IN DATABASE:25-Mar-2012
oracle query
select tran_date,
ROUND (nvl(sum(WALKIN_WITHOUT_CGROUP),0)/ COUNT(*),2)APC
from OUTLET_PAYMODE_REPORT_FACT
Before Open:
var sqld= new String();
yearParam = params["Year"].value;
yearParam = yearParam.split("-");
startYear = yearParam[0];
endYear = yearParam[1];
this.queryText =this.queryText +" where TRAN_DATE between '" + startYear + "01-Mar-' and '" + endYear + "30-Mar-'"+
"group by tran_date"
but am getting error like this.....
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object.
SQL error #1:ORA-01861: literal does not match format string
I think, your sql string should be
" where TRAN_DATE between '01-Mar-" + startYear +
"' and '30-Mar-" + endYear + "' " +
"group by tran_date"