user852610
user852610

Reputation: 2265

Joomla! SQL Query does not insert data in the database

I use Joomla 2.5 and I'm trying to insert data into my database

This is my code

    $db =& JFactory::getDBO();
    $query = 
    "INSERT INTO '#__restaurantes' ('nombre', 'direccion', 'localizacion', 'cod_postal', 'telefono', 'web')
    VALUES ( JRequest::getCmd('restaurante'), JRequest::getCmd('direccion'), JRequest::getCmd('localizacion'), JRequest::getCmd('postal'), JRequest::getCmd('telefonoEmpresa'), JRequest::getCmd('web') )";
    $db->setQuery( $query );
    $db->query();

Everything is fine and I don´t have any errors, but it doesn't insert anything.

Any idea? thanks

Upvotes: 0

Views: 5073

Answers (3)

Kathir
Kathir

Reputation: 1250

This also should work:

$data =new stdClass();
$data->id = null;
$data->field1 = 'val1';
$data->field2 = 'val2';
$data->field3 = 'val3';

$db = JFactory::getDBO();
$db->insertObject( '#__mytable', $data, id );

stdClass is a php base class from which all other classes are extended.

'id' is the name for your primary key.

Upvotes: 1

Søren Beck Jensen
Søren Beck Jensen

Reputation: 1676

You need to put quotes around your values:

$restaurante = JRequest::getCmd('restaurante');
$direccion = JRequest::getCmd('direccion');
$localizacion = JRequest::getCmd('localizacion');
$postal = JRequest::getCmd('postal');
$telefonoEmpresa = JRequest::getCmd('telefonoEmpresa');
$telefonoEmpresa = JRequest::getCmd('telefonoEmpresa');
$query = "INSERT INTO '#__restaurantes' ('nombre', 'direccion', 'localizacion', 'cod_postal', 'telefono', 'web') VALUES ( '$restaurante', '$direccion', '$localizacion', '$postal', '$telefonoEmpresa', '$web' )";
$db->setQuery( $query );

Also, you should add this code after your query to catch any errors:

if ($db->getErrorNum()) {
    JError::raiseError("Error", $db->stderr());
}

Upvotes: 2

Craig
Craig

Reputation: 9330

When trying to figure what's going wrong with your Joomla! code the first thing to do is set the Error Reporting level in the Site->Global Configuration->Server screen to Maximum (or Development if you don't have any extensions installed that crash Joomla! under Maximum.

This will not only report more errors and their details it will also provide a "Debug Console" which has a section called "Database Queries" that will show exactly what is happening with all of the SQL queries.

Now, on the code you've provided:

  1. The $query won't work the way you've structured it as each JRequest() will just be treated as a syntax error.
  2. There are two ways to extract the values, either store them in temporary variables before hand and then use those in the $query or fix your initial method by using concatenation to build the query.

Method 1

In PHP when you wrap a string in " double quotes the string is processed by PHP and any variables or escape sequences are replaced.

$restaurante = JRequest::getCmd('restaurante');
$direccion = JRequest::getCmd('direccion');
$localizacion = JRequest::getCmd('localizacion');
$postal = JRequest::getCmd('postal');
$telefonoEmpresa = JRequest::getCmd('telefonoEmpresa');
$telefonoEmpresa = JRequest::getCmd('telefonoEmpresa');
$query = "INSERT INTO '#__restaurantes' ('nombre', 'direccion', 'localizacion', 'cod_postal', 'telefono', 'web') VALUES ( $restaurante, $direccion, $localizacion, $postal, $telefonoEmpresa, $web )";
$db->setQuery( $query );

Method 2

To use the JRequest() calls as you have you will need to use the '.' (concatenation operator) to build the string and make the calls in line.

$query = "INSERT INTO '#__restaurantes' ('nombre', 'direccion', 'localizacion', 'cod_postal', 'telefono', 'web') VALUES ("
       . JRequest::getCmd('restaurante') . ", "
       . JRequest::getCmd('direccion') . ", "
       . JRequest::getCmd('localizacion') . ", "
       . JRequest::getCmd('postal') . ", "
       . JRequest::getCmd('telefonoEmpresa') . ", "
       . JRequest::getCmd('web'). " )";

Things you should be aware of:

  1. JRequest shouldn't be used unless you're trying to support Joomla! 1.5 as well as 2.5. You should use JInput() - read that link for more information.
  2. The query you're building is an old style string query and doesn't take advantage of JDatabases abstraction layer, you should read more about it here.
  3. JRequest::getCmd() filters all values and only allows characters in the set [A-Za-z0-9.-_], so any non-ascii values will be removed.

Upvotes: 3

Related Questions