Need help with db insert; 1 form, 2 tables, no db-connect

matthiashansal 30 Dec, 2010
I really, really, really need some help with debugging this code😟

I have two tables, table1 and table2, where table 1 includes an auto-incr. id of type integer to which table 2 refers (with possibly multiple entries). Have been working on getting the form data to be saved in the two tables all day, searching forums and the how-tos so I'm really sorry if this issue has already been solved elsewhere.

For now I have DBConnections disabled and one form with textboxes, a file upload, and a selector. These values are refered to in the variables in table 1. I put this code in the onsubmit box, after email:



<?php

$data = array();

$record =& JTable::getInstance("table1", "Table");
if ( !$record->save($data) ) {
JError::raiseWarning(100, $record->getError());
}

if (!class_exists(''Tabletable1'')) {
class Tabletable1 extends JTable {
var $name = null;
var $logo = null;
var $template = null;
var $id = null;
var $description = null;
var $datecreated = null;
function __construct( &$database ) {
parent::__construct( ''jos_table1'', ''id'', $database );
}
}
}
?>

<?php

//get the stuff down here
$lastid = $row_chronocontact_createspace; 

$user =& JFactory::getUser();
$user_id = $user->id;

$data = array();
$data['relatedid'] = $lastid; 
$data['role'] = 0; 
$data['userid'] = $user_id;
$data['activespace'] = 1;

$record =& JTable::getInstance("sp_table2", "Table");
if ( !$record->save($data) ) {
JError::raiseWarning(100, $record->getError());
}

if (!class_exists(''Tablesp_table2'')) {
class Tablesp_table2 extends JTable {
var $id = null;
var $relatedid = null;
var $role = null;
var $userid = null;
var $activespace = null;
function __construct( &$database ) {
parent::__construct( ''#__table2'', ''id'', $database );
}
}
}
?>


If I use db-connect, it works wonderfully for table1, but I just cannot get the userid and the table1.id saved in table2...😟😟
GreyHead 30 Dec, 2010
Hi matthiashansal,

The simple way to do this is not to use the auto-increment column to link the two tables but to create a new unique key in the OnSubmit Before box and then save that to both tables using the DB Connection.

To use the autoincrement approach you need to use the DB Connection for the first save; then get the cf_id value using:
$MyForm->tablerow['jos_my_table_name']->cf_id
Then you can do the second save in the OnSubmit Afte Email box - provided that the Run Order is set up correctly.

Bob

PS Please edit or add to your posts rather than delete them so that I don't end up replying to the deleted thread :-(
matthiashansal 31 Dec, 2010
Hi Bob,

awesome, thanks, that argument was exactly what I was looking forπŸ™‚πŸ™‚πŸ™‚
Really, thank youπŸ™‚

Matthias

And sorry for that deleted thread.. won't happen again
This topic is locked and no more replies can be posted.