Forums

Custom Code to DELETE record

CyborgPrime 05 Dec, 2016
Hello-

Now that I am able to get some more advanced things done with Chronoforms I am really excited to create some great form apps.

i made a form that lets the user recall a record from the table, edit it, and save it. Everything works.

I want to add the functionality to delete the record instead of saving it.

I created a checkbox on the form for the user to check if they want to delete the record.

I setup an event switcher and made sure it worked for updating the selected record (by event_id key).

So now I am trying to figure out how to execute a php/mysql command in custom code that would DELETE the record with ID of event_id.

Any suggestions?
CyborgPrime 05 Dec, 2016
I guess the sql command would be something like:

$sql = "DELETE FROM events WHERE id='$event_id';

WOuld that work? How do I make chronoforms execute that in custom code?
CyborgPrime 05 Dec, 2016
oops i think i made a typo. Shouldn't it be:

$sql = "DELETE FROM events WHERE event_id='$event_id';
GreyHead 05 Dec, 2016
Hi Cyborprime,

<?php
$db = \JFactory::getDBO();
$query = "
    DELETE
        FROM `#__table_name`
        WHERE `id` = '{$form->data['event_id']}' ;
";
$db->setQuery($query);
$db->execute();
?>

BUT I would usually have a status column and set that to 'deleted' rather than actually delete the record as there is no easy way back from the deletion.

Bob
CyborgPrime 05 Dec, 2016
something like this?

 <?php
 $query = 'DELETE FROM `events` WHERE event_id = '$event_id';
  $result = mysql_query($query,$link);
?>


I'm not sure how to trigger the query in custom code.

ow! my brain hurts! Thanks for indulging a noob,
CyborgPrime 05 Dec, 2016
oh - ok, I tried this, but it didnt work:

    <?php
    $db = \JFactory::getDBO();
    $query = "
        DELETE
            FROM `events`
            WHERE `event_id` = '{$form->data['event_id']}' ;
    ";
    $db->setQuery($query);
    $db->execute();
    ?>


I changed your code from 'id' = to 'event_id' =

SHould I leave that alone as 'id'= ?
CyborgPrime 05 Dec, 2016
duh - gotta take out the quotes i think.. brb
CyborgPrime 05 Dec, 2016
arrg! I have no idea what I'm doing or doing wrong here.

<?php
    $db = \JFactory::getDBO();
    $query = "
        DELETE
            FROM events
            WHERE event_id = '{$form->data['event_id']}'
    ";
    $db->setQuery($query);
    $db->execute();
?>
CyborgPrime 05 Dec, 2016
i've tried all different combinations and none worked.

So with my example of:
table: events
key of current record: event_id

Should the code be:

    <?php
    $db =& JFactory::getDBO();
    $query = "
      DELETE FROM `events`
        WHERE `event_id` = '{$form->data['event_id']}' ;
    ";
    $db->setQuery($query);
    $db->query();
    ?>


im not having any luck
CyborgPrime 05 Dec, 2016
oh sorry- looks like the switch for calling the delete event isnt working right, so the code you sent isnt getting called.

I'll troubleshoot this further and report back. Sorry - my bad.
CyborgPrime 05 Dec, 2016
ok i took out that code and put in a display message that shows the switch *is* working.

I'm going to put the code you send in a custom code box and see what happens.
CyborgPrime 05 Dec, 2016
ok no matter what I do i get error 114 - what does that mean?
CyborgPrime 05 Dec, 2016
I'm using an external database if that matters?
CyborgPrime 05 Dec, 2016
1 Likes
ok - I see by the error messages that it is looking in the joomla database instead of my external database.

so I figured it out - you have to use the joomla database connectors in your php script to link into and manipulate the external database otherwise you are hooking into the joonla database.
CyborgPrime 06 Dec, 2016
Answer
Here's the solution:

<?php
$option = array(); //prevent problems 
$option['driver']   = 'mysql';            // Database driver name
$option['host']     = 'mysql.database.name';    // Database host name
$option['user']     = 'my_database_user';       // User for database authentication
$option['password'] = 'my_password';   // Password for database authentication
$option['database'] = 'my_calendar';      // Database name
 
$db = JDatabaseDriver::getInstance( $option );
$query = "DELETE FROM `events` WHERE `event_id` = '{$form->data['event_id']}';
    ";
$db->setQuery($query);
$db->execute();

?>
This topic is locked and no more replies can be posted.