1
jmass
Inserting data into module table
  • 2004/2/15 2:01

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


I am having a very tough time inserting data into a table of my module.

I am passing vars from a form via post. I can get the vars and echo them, but can not seem to insert them into my table.

Can anyone show me some sample code that illustrates how to d othis right? I have looked at the class files for news and such, but can't seem to figure this out.

Thanks,

JMass

2
Dave_L
Re: Inserting data into module table
  • 2004/2/15 4:57

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Do you mean inserting the data into a MySQL database table, or displaying it in an HTML table?

3
jmass
Re: Inserting data into module table
  • 2004/2/15 14:17

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


A MySQL Table.

It should be a very simple common thing to do... But I could not find the XOOPS way of doing it.

Thanks for replying.

JMass

4
fatman
Re: Inserting data into module table
  • 2004/2/15 15:08

  • fatman

  • Friend of XOOPS

  • Posts: 176

  • Since: 2003/12/13



I would use something like:

// SQL Assuming you took your _POST vars and assigned
 // them to $value1, $value2 ect..

 
$sql "INSERT INTO ".$xoopsDB->prefix('tablename');
 
$sql .= " ( field1, field2, field3 ) VALUES ";
 
$sql .= " ( '$value1', '$value2', '$value3' )";

 
// run sql
 
if ( ! $xoopsDB->query($sql) )
 {
   echo( 
$xoopsDB->error." : ".$xoopsDB->errno );
 }  

 
// While I am at it, here's how to SELECT data as well

 
$sql "SELECT * FROM ".$xoopsDB->prefix('tablename');
 
 
$result $xoopsDB->query($sql);

 
$n 0;
 while ( 
$row $xoopsDB->fetchArray($result))
 {
     foreach ( 
$row as $k=>$v )
     {
        
$mydata[$n][$k] = $v;
     }
 
$n++;
 }

5
jmass
Re: Inserting data into module table
  • 2004/2/16 12:23

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


Thanks for the post!
I had:
// SQL Assuming you took your _POST vars and assigned
// them to $value1, $value2 ect..

$sql = "INSERT INTO ".$xoopsDB->prefix('tablename');
$sql .= " ( field1, field2, field3 ) VALUES ";
$sql .= " ( '$value1', '$value2', '$value3' )";

But did not have this:
// run sql
if ( ! $xoopsDB->query($sql) )
{
echo( $xoopsDB->error." : ".$xoopsDB->errno );
}
I will give it a try and post results.

Thanks for the info. XOOPS has such a great community.

JMass

6
jmass
Re: Inserting data into module table
  • 2004/2/16 14:53

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


No Luck! Arghhhh!

This is my code:

$TicketID = '4';
$UID = $xoopsUser->getVar("uid");
$EntryDate = $_POST['entrydate'];
$ClientID = $_POST['clientid'];
$CategoryID = $_POST['categoryid'];
$PriorityID = $_POST['priorityid'];
$TicketSubject = $_POST['ticketsubject'];
$TicketDetails = $_POST['ticketdetails'];

echo $TicketID;
echo $UID;
echo $EntryDate;
echo $ClientID;
echo $CategoryID;
echo $PriorityID;
echo $TicketSubject;
echo $TicketDetails;


// SQL Assuming you took your _POST vars and assigned

$sql = "INSERT INTO ".$xoopsDB->prefix('obsclient_hd_ticketdata');
$sql .= " ( TicketID, UID, EntryDate, ClientID, CategoryID, PriorityID, TicketSubject, TicketDetails ) VALUES ";
$sql .= " ( '$TicketID', '$UID', '$EntryDate', '$ClientID', '$CategoryID', '$PriorityID', '$TicketSubject', '$TicketDetails' )";

echo $sql;

// run sql
if ( ! $xoopsDB->query($sql) )
{
echo( $xoopsDB->error." : ".$xoopsDB->errno );
}

All of the echo statement produce good results including to $sql - but still no insertion

the if statement produes a :

this indicates that the $xoopsDB->error and $xoopsDB->errno do not work either... Mabey I am missing a vital include?

JMass

7
Mithrandir
Re: Inserting data into module table

Turn on MySQL debug and try again (allow pop-up windows for the site and you should get such a pop-up window with all SQL statements executed on the current page - erroneous ones written in red)

8
hsalazar
Re: Inserting data into module table
  • 2004/2/16 16:08

  • hsalazar

  • Just popping in

  • Posts: 78

  • Since: 2003/2/6 1


JMass:

The echo statements work because they are PHP statements. If you don't have data inserted into the table, maybe you don't have an instance of the database object there...

global $xoopsDB;

Perhaps this is too simple, but I've found sometimes when you're deep into some code, you tend to forget obvious things.

Cheers.

9
jmass
Re: Inserting data into module table
  • 2004/2/16 17:30

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


Again thanks for the posts.

I do have global $xoopsDB; ( I know what you mean about forgetting the obvious)

I found the MySQL debug... I am trying ti out now.

Thanks,

JMass

10
jmass
Re: Inserting data into module table
  • 2004/2/16 17:45

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


WooooooooHoooooooo!

Thanks again everyone!

The MySQL debug was just the trick. I had an error in my table creation. It works now.

Thanks again!!!!

JMass

Login

Who's Online

103 user(s) are online (56 user(s) are browsing Support Forums)


Members: 0


Guests: 103


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits