• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DB Update - Not working (that's the way it seems)
I am new to UserSpice, and I have recently started working with another group on a project that has been developed using UserSpice.

There is an issue we have encountered where an entry can be edited but when performing the DB Update, the record is not updated.

I'm still learning the framework and I can see the DB.php file and the functions, and it seems to me the db->update should work but I'm not sure why it does not.

From the following can anyone glean why it would not update?

Thanks for any assistance anyone can provide.

<?php require_once '../users/init.php'; ?>
<?php require_once $abs_us_root.$us_url_root.'users/includes/header.php'; ?>
<?php require_once $abs_us_root.$us_url_root.'users/includes/navigation.php'; ?>

<?php if (!securePage($_SERVER['PHP_SELF'])){die();} ?>
$validation = new Validate();
//PHP Goes Here!
$errors = [];
$successes = [];
$rtuId = Input::get('id');

//Check if selected user exists
  Redirect::to("company_admin_rtus.php"); die();
$rtudetails = fetchRTUDetails(NULL, NULL, $rtuId); //Fetch rtu details

$str = $rtudetails->org_id;
$str .= $rtudetails->rtu_id;

//Forms posted
if(!empty($_POST)) {
    $token = $_POST['csrf'];
      die('Token doesn\'t match!');
    }else {

      //set changed bit in database

              'configChange' => 1,

    //Update rtu name

    if ($rtudetails->rtu_name != $_POST['rtu_name']){
      $rtu_name = Input::get("rtu_name");
        'rtu_name' => array(
          'display' => 'rtu_name',
          'required' => true,
          'min' => 1,
          'max' => 50
      $db->update('rtu_act', $rtuId, $fields);
      $successes[] = "RTU Name Updated";
          ?><div id="form-errors">
After $db->update you can do something like this:
if(!$db->error()) {
#no error happened
} else {
#an error happened, you can find out what by dumping error or errorString(), eg:
#I prefer to log my errors, if this is a logged in page:
logger($user->data()->id,"Log Type","Error updating RTU Name, Error: ".$db->errorString());

I wrote this without an editor so there could be errors. Furthermore you can verify, if no errors, the count of rows updated with $db->count();

Hopefully this helps.
Interestingly enough, there is no error. 

Plus, I just realized that when we first add an entry the record is created, so the insert works fine.

The record can then be read, but upon editing that record the update does not work. 

Even more interesting is, I can update other fields but not the two name fields. I thought maybe it was the privileges but the user has select, insert, update, and reference for privileges.

Based on the UserSpice code how should an db->update be formulated. It seems pretty simple to me:

db->update('A_table', 'An_ID', 'A_Column_to_Update');

Is it perhaps the  $fields=array('rtu_name'=>$rtu_name);?

Maybe the update isn't actually seeing new data?

It is potential that the update is not seeing new data - I would dump($rtu_name) and make sure it is returning what you expected.

If $db->count() is not returning any results but there is no error, its updating, just no new update itself was recorded.

The DB class update function takes the following in this order:
-id (default id or maybe looks for a primary key? im not sure), you can specify an array for this too like array('ID_Column_Name' => id_value)
-fields in array format eg. array('ColumnName' => ColumnValue)

Thank you,
I was able to dump($rtu_name) and it shows the field information.

So it looks as though the update is working, it's just not applying the information from $rtu_name.

So I did and echo to see the values of both $rtu_name and $fields and this is the output (I'm updating the rtu_name with just my name):

echo "$rtu_name, $fields";

Rob, Array
Hi Rob,

I'm unable to fully make out your response-it seems to be formatted incorrectly. Can you check it again?

Thank you,
Thanks, Brandin for your assistance.

Turns out the developer in the project modified the DB.php file to the point the update function was not functioning. Smile

I was able to replace the DB.php file with the original and add only the additional code that was required, and this fixed the update issue.

On another note, I'm trying to figure out how to perform a delete of a record. I'll open another thread on that to keep them separate.

Thanks again.
Glad to hear you were able to figure this out, and thanks for letting me know.

Forum Jump:

Users browsing this thread: 1 Guest(s)