tinyhammers
tinyhammers

Reputation: 317

Writing to Joomla database

I'm a front end dev, so my PHP is a bit lacking and I'm stuck.
I'm trying to write some data from a form into the Joomla database.
It's only an email address so not complicated. I've created a form and a table in the database to hold the email addresses. However I can't get it to write to the database and I'm not sure what I'm missing.
I'm not sure what classes I need to include in my code. I'm including joomla/factory so I'm not getting an error telling me the JFactory is not an object, but I don't know if I need to include anything else.

Here's my PHP code:

$emailAdd = $_POST['email'];

require_once ('../../libraries/joomla/factory.php');

$db =& JFactory::getDBO();
$query = "INSERT INTO '#__pdfemails' ('emailaddress')
    VALUES ('thevalue')";
$db->setQuery($query);
$db->query();

I don't get any kind of error at all, but it's not writing to the database.

Can anyone help please?

Upvotes: 1

Views: 2860

Answers (2)

Bobby Jack
Bobby Jack

Reputation: 16018

You have an error in your SQL. This:

$query = "INSERT INTO '#__pdfemails' ('emailaddress')
VALUES ('thevalue')";

should be this:

$query = "INSERT INTO '#__pdfemails' (emailaddress)
VALUES ('thevalue')";

Note the lack of single-quotes around the column name.

If you ever get an error like this again, try executing the SQL directly in MySQL first. In a Joomla environment, there can be many points at which errors get 'hidden'. That's very sensible for a live site, although you should be able to configure a development site to show SQL errors to ease troubleshooting.

Upvotes: 2

Brent Friar
Brent Friar

Reputation: 10609

Here's an easy answer for you - http://extensions.joomla.org/extensions/contacts-and-feedback/forms

There 2 pages of form builders. I know Chronoforms (it's free too) will do exactly what you want. You can be done in under 10 minutes including install time. Plus you will get validation and other functions like data sanitation without having to write it yourself.

Judging from the code you posted, you're headed down a road to XSS and SQL injection attacks. Do yourself a favor and get an extension that works.

Upvotes: 5

Related Questions