Reputation: 2265
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
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
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
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:
$query
won't work the way you've structured it as each JRequest()
will just be treated as a syntax error.$query
or fix your initial method by using concatenation to build the query.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 );
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'). " )";
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.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