从 Joomla 表单字段插入数据库 [英] insert into database from Joomla form fields

查看:24
本文介绍了从 Joomla 表单字段插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Joomla 的初学者!开发并创建了一个非常简单的模块.

I am beginner to Joomla! development and have created a very simple module.

如何创建一个包含 3 个文本字段的表单,然后将输入的值保存到数据库表中?

How do I create a form with 3 text fields and then save the entered values into a database table?

推荐答案

试试这个例子:

我们会将用户的名字和姓氏发布到表格中.

We will Post a user's first and last name to a table.

在您的数据库中创建一个表.注意它应该有前缀jos_"

create a table in your database. Note it should have the prefix "jos_"

我们将这种形式称为名称".所以我们将我们的表命名为jos_names"

We will call this form, "names". So we will name our table "jos_names"

在 PHPMyAdmin(或您使用的任何工具..)中的 SQL 行,执行此查询以创建一个新表:

At the SQL line in PHPMyAdmin (or whatever tool you use..), do this query to create a new table:

CREATE TABLE `databasename`.`jos_names` (`id` int(11) NOT NULL auto_increment, `firstname` VARCHAR(100), `lastname` VARCHAR(100), PRIMARY KEY  (`id`) )

为了简化事情,我们将结果发布到同一页面..让我们构建表单:

To simplify things, we will post the results to the same page.. Let's build the form:

<?php

/** post form to db module **/

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );



//--POST YOUR FORM DATA HERE-->
$fname = $_POST['fname'];
$lname = $_POST['lname'];
//--END POST YOUR FORM DATA---|
//--build the form------------>
?>
<form name="names" id="names" action="<?php echo JURI::current(); ?>" method="post">
  <p><input type="text" name="fname" id="fname" value="" /></p>
  <p><input type="text" name="lname" id="lname" value="" /></p>
  <p><input id="submit" name="submit" type="submit" value="Submit Names" /></p>
</form>
//--END BUILD THE FORM--------|
<?
if( (isset($lname)) || (isset($fname)) ) {
   //first name or last name set, continue-->
   $data =new stdClass();
   $data->id = NULL;
   $data->firstname = $fname;
   $data->lastname = $lname;

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

}  else {
  echo '<h4>One Field Is Required!</h4>';
}

?>

应该可以.如果您正在编写传统的 Joomla 模块,这应该是您的 helper.php 文件.

That should do it. If you are writing a traditional Joomla module, this should be your helper.php file.

注意:在 joomla 文档中只包含一次死"脚本..(定义('_JEXEC')..

NOTES: Only include the "die" script once in a joomla document.. (defined( '_JEXEC' )..

JURI::current() 自动读取当前页面的 URL.如果你调用 echo JURI::current();在带有 url http://www.example.com/names 的页面上,它将显示同一个链接.

JURI::current() automatically reads the current page URL. If you call echo JURI::current(); on a page with the url http://www.example.com/names, then it will display the same link.

action="" 指向您将发布此模块的确切 URL,这一点很重要.

It is important that the action="" points to the exact Url where you will publish this module.

此外,将数据发布到SELF"被认为是不好的做法,但是您受到模块的限制,因此除非您构建组件或插件,否则您应该像这样做一样将表单发布到SELF"例子.(JURI::current();)

Furthermore, it is considered bad practice to post data to 'SELF', but you are kindof limited with a module, so unless you build a component or a plugin, you should post your form to 'SELF' as done with this example. (JURI::current();)

当在 Joomla 框架中时,没有必要声明您的数据库名称、用户名或密码,因为 Joomla 已经登录"了..所以而不是查询 databasename.jos__tablename,在 joomla 中,您可以将查询替换为:#__tablename.事实上,这是处理 db 查询和 Joomla 时的最佳实践,因为用户不必使用默认的 jos_ 前缀,joomla 会自动用任何前缀替换#".在我的情况下,#"等于jos"

When in the Joomla framework, it isn't necessary to declare your database name, username or password as Joomla is already "logged in".. So instead of querying databasename.jos__tablename, in joomla you can replace the query with this: #__tablename. In fact this is the best practice when dealing with db queries and Joomla because users do not have to use the default jos_ prefix, joomla automatically replaces "#" with whatever the prefix. In my case "#" equals "jos"

在查询 sql 以创建表时请注意.. 确保将 databasename 替换为数据库的实际名称..

Take note when querying the sql to create the table.. make sure you replace databasename with the actual name of your database..

应该可以.

如果由于某种原因您无法发布数据:1) 确保当您单击提交时表单不会重定向到其他页面.如果是,请将表单操作""更改为发布此页面的绝对网址..然后从那里开始.

If for some reason you are unable to post data: 1) Make sure the form doesn't redirect to a different page when you click submit. If it does, change the form action"" to the absolute url to where this page is published.. then go from there.

2) 有时 $data =new 方法不起作用.这取决于您如何设置模块、函数和类.这是一个替代方案:

2) Sometimes the $data =new method doesn't work. This depends on how you set up your module, functions and classes. Here is an alternative:

$db =& JFactory::getDBO();
$query = "INSERT INTO `#__names` (`fname`, `lname`)
    VALUES ($fname, $lname);";
$db->setQuery( $query );
$db->query();   

这篇关于从 Joomla 表单字段插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆