vickram
vickram

Reputation: 67

How to insert values in to database in joomla 2.5

I am working on a joomla 2.5 website & I am trying to insert values in database entered in the form on form submit. But I dont know why they are not inserting in the DB. Please help me out guys.

I've tried many different ways but I am not getting where I am going wrong.

This is my code:

    if(isset($_POST["buttonSubmit"]))
    {
        $name = $_POST["name"];
        $location = $_POST["location"]; 
        $email = $_POST["email"];   
        echo $name;
        $db =& JFactory::getDBO();
        echo $query = "INSERT INTO '#__pxa_map' ('name', 'location','email') VALUES ($name, $location,$email)";
        $db->setQuery( $query );
        $db->query();  
    }

How to insert values in to database in joomla 2.5

Upvotes: 0

Views: 3919

Answers (1)

jeroen
jeroen

Reputation: 91792

You should add error handling and use prepared statements (preferably, don't know how that works in Joomla 2.5), but your query is wrong:

  1. You don't quote the table and field names, you escape them with a backtick if necessary;
  2. You do quote your values if you don't use a prepared statement;
  3. You need to run your input through $db->quote() to prevent sql injection.

So it should look like:

$db = JFactory::getDbo();
$name = $db->quote($_POST["name"]);
// etc.

$query = "INSERT INTO `#__pxa_map` (`name`, `location`,`email`) VALUES ('$name', '$location','$email')";

Upvotes: 3

Related Questions