简体   繁体   English

如何在joomla 2.5中将值插入数据库

[英]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. 我正在joomla 2.5网站上工作,我试图在表单提交表单中输入的数据库中插入值。 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 如何在joomla 2.5中将值插入数据库

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: 您应该添加错误处理并使用准备好的语句(最好是,不知道它在Joomla 2.5中的工作方式),但是您的查询是错误的:

  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. 您需要通过$db->quote()运行输入,以防止sql注入。

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')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM