so I have this code for my db transactions:
$to_return = true;
$this->db->trans_begin();
$this->insert_arrest_details($data);
$this->update_barangay($data['barangay_id']);
$this->insert_arresting_officers($data);
$folders = $this->insert_violators($data);
$this->insert_arrest_booking_form($data);
$this->insert_case_report($data);
$this->insert_seizing_officers($data['case_id'],json_decode($data['seizing_officers']));
$this->insert_items(json_decode($data['items']));
$this->insert_nitems(json_decode($data['nitems']));
$this->insert_violator_items(json_decode($data['items']));
$this->insert_violator_nitems(json_decode($data['nitems']));
if($this->db->trans_status() === FALSE){
$array['error_message'] = $this->db->_error_message();
$array['error_number'] = $this->db->_error_number();
$this->db->trans_rollback();
$to_return = $array;
}else{
$img_success = $this->move_violators_images($folders);
$img_success = $this->move_items_images($data['case_id']);
$img_success = 1;
if($img_success == 1){
$this->db->trans_commit();
}else{
$this->db->trans_rollback();
$to_return = false;
}
}
return $to_return;
My problem is, if the error (for example) occurs in $this->insert_arrest_details(); My code doesn't get the error. It returns something like this:
"error_message":"","error_number":0
But when I tried to run 1 method only and that method failed, my code was able to get the error. why is that so?
Your responses will be greatly appreciated.
Related
This has been giving a
404 page error not found
and i don't know why.
public static function getGeneration($currentParent, $rootParent){
// dd($rootParent);
$user = User::where("username", $currentParent)->first();
$generation =[];
$x = 0;
$bool = true;
while ($bool) {
if($x==0){
$generation[$x] = (User::where("username", $user->sponsor_username)
->firstOrfail())
->sponsor_username;
}else{
$generation[$x] = (User::where("username", $generation[$x-1])
->firstOrfail())
->sponsor_username;
}
if( $x > 0 ){
if($news = $generation[$x] == $rootParent){
dd($news);
$bool = false;
}
}
$x++;
}
return $generation;
}
what this method basicaly does is go through a database and find out who brought a particular user into a system, because is user has a sponsor_username against it on the database, the last if statement is meant to stop when it discovers that it has reached the root user.
I have this code:
function saveField($field, $id, $module, $value)
{
$bean = BeanFactory::getBean($module, $id);
if (is_object($bean) && $bean->id != "") {
if ($bean->field_defs[$field]['type'] == "multienum") {
$bean->$field = encodeMultienumValue($value);
}else if ($bean->field_defs[$field]['type'] == "relate" || $bean->field_defs[$field]['type'] == 'parent'){
$save_field = $bean->field_defs[$field]['id_name'];
$bean->$save_field = $value;
if ($bean->field_defs[$field]['type'] == 'parent') {
$bean->parent_type = $_REQUEST['parent_type'];
$bean->fill_in_additional_parent_fields(); // get up to date parent info as need it to display name
}
}else{
$bean->$field = $value;
}
//return here will work
$bean->save(); //this works
//nothing works here
return getDisplayValue($bean, $field);
} else {
return false;
}
}
The problem here is that anything under
$bean->save()
will not work. But I know that save is working as the values are being updated. So how can I debug this problem?
I already tried:
return var_dump($bean->save());
return print_r($bean->save());
if($bean->save()){
return "1";
}else{
return "2";
}
And none of those in the above worked I still get nothing in my return.
There is likely something such as an after_save logic hook that is executing and either causing a fatal error or doing an exit.
Try using xdebug, it should allow you to investigate further into the save method that fails.
I am trying to compare two strings "welford.me" & "welford.me", however, when I do $server ("welford.me") == $allowed ("welford.me") it does not return true. What on earth could be causing this? I've attached an image of the JSON response (which is false).
Here's the culprit:-
$server = parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST);
$data = $result->fetch_assoc();
$allowed = explode(",", $data['api_allowed_domain']);
$found = false;
$Ajax->response['hm'] = array();
foreach($allowed as $xtld){
if($xtld == $server){
$found = true;
}else{
$hm = array($server => $xtld);
array_push($Ajax->response['hm'], $hm);
}
}
if($found){
return "true";
}else{
return "Domain / does not have permission to use this API key.";
}
$server = "welford.me" and "welford.me" is in the allowed domains listed in the database. All output is done upon destruction. Everything else works fine except this. Looks like $found is not being set to true.
Trimming both $xtld & $server seemed to work for me.
foreach($allowed as $xtld){
if(trim($xtld) == trim($server)){
$found = true;
}
}
I am submitting a form, using the following function (prize.php):
loadmodule('validate'); //This just loads the validate.php function.
$validate = new Validate;
if($_POST)
{
$validateForm = $validate->validateForm();
switch($validateForm)
{
case 1:
$error = 'You\'re not logged in..';
$stop = true;
break;
//If no error = success.
if($validateForm['code'] == "100"){
$won = $val['prize'];
$type = $val['type'];
$success = 'You have won! The prize was '.$won.' '.$type.'';
die($success);
}
}
die($error);
}
This is the function to validate the form (validate.php):
function validate()
{
global $userdata;
if(!is_array($userdata))
return 1; // User not logged in - return error code one.
//If no error, lets show a success message.
$prize = "100";
$text = "dollars";
return array("code"=>"100","prize"=>"$prize","type"=>"$text");
}//If won
}
The above code returns:
Notice: Undefined variable: error in /home/.../public_html/pages/prize.php on line 27
Although, it shouldn't throw an error there, since the die($success) should be triggered by the code 100.
What am I doing wrong?
$error = '';
$stop = false;
switch($validateForm){
case 1:
$error = 'You\'re not logged in..';
$stop = true;
break;
}
//If no error = success.
if($validateForm['code'] == "100"){
$won = $val['prize'];
$type = $val['type'];
$success = 'You have won! The prize was '.$won.' '.$type.'';
die($success);
}
first guess is that the if($validateForm['code'] == "100"){ is ment to be outside the switch.
$validateForm = $validate->validateForm();
returns an array.. later on your're doing a if ($validateForm==1) in the switch.. when $validateForm is an array.
you might have better luck with a simple is_array() if statement than the whole switch
I am trying to add a product to my shopping cart.
I am getting an error saying:
Warning: Invalid argument supplied for foreach() in
It is telling me I am getting an error for the following code:
function isInCart($id) {
if (!empty($_SESSION['sess_uid']['cart'])) {
foreach ($_SESSION['sess_uid']['cart'] as $report) {
if ($report['reportID'] == $id) {
// Report ID found in Cart
return true;
}
}
// Looped through cart, ID not found
return false;
} else {
// Cart empty
return false;
}
}
The particular line from the above that is flagging the error is:
foreach ($_SESSION['sess_uid']['cart'] as $report) {
I am also getting the following error message:
Fatal error: Only variables can be passed by reference in
The code this relates to is the following:
function addToCart($id) {
$report = getReportByID($id);
$author = $report['userID'];
if (!empty($report)) {
// Got the report
if (!empty($_SESSION['sess_uid']['cart'])) {
if (!isInCart($id) && !isOwner($author) && !hasPurchased($id)) {
array_push($_SESSION['sess_uid']['cart'], $report);
return true;
} else {
return false;
}
} else {
$_SESSION['sess_uid']['cart'] = array();
if (!isInCart($id) && !isOwner($author) && !hasPurchased($id)) {
array_push($_SESSION['sess_uid']['cart'], $report);
return true;
} else {
return false;
}
}
} else {
// Unable to get report by ID
return false;
}
}
The particular line of code from the above that is flagging the error is:
array_push($_SESSION['sess_uid']['cart'], $report);
The code below is what gets my reports to populate the store
<?php
function getReportByID($id) {
$conn = new mysqli(localhost, root, DBPASS, DBNAME);
$sql = "SELECT * FROM reports WHERE reportID = '" . $conn->real_escape_string($id)."';";
// Performs the $sql query on the server
$report = $conn->query($sql);
return $report->fetch_array(MYSQLI_ASSOC);
}
?>
Any help would be greatly appreciated.
Thanks
i think this wil help:
it typcast your session as an array so even when the session is empty you dont get an error
foreach ((array)$_SESSION['sess_uid']['cart'] as $report) {
let me know if this fix the error?