database 从 Joomla 表单字段插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2842860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert into database from Joomla form fields
提问by jehan
I am beginner to Joomla! development and have created a very simple module.
我是 Joomla 的初学者!开发并创建了一个非常简单的模块。
How do I create a form with 3 text fields and then save the entered values into a database table?
如何创建一个包含 3 个文本字段的表单,然后将输入的值保存到数据库表中?
回答by Kevin Florida
Try this example:
试试这个例子:
We will Post a user's first and last name to a table.
我们将把用户的名字和姓氏张贴到表中。
create a table in your database. Note it should have the prefix "jos_"
在您的数据库中创建一个表。注意它应该有前缀“jos_”
We will call this form, "names". So we will name our table "jos_names"
我们将这种形式称为“名称”。所以我们将我们的表命名为“jos_names”
At the SQL line in PHPMyAdmin (or whatever tool you use..), do this query to create a new table:
在 PHPMyAdmin(或您使用的任何工具..)中的 SQL 行,执行此查询以创建一个新表:
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>';
}
?>
That should do it. If you are writing a traditional Joomla module, this should be your helper.php file.
那应该这样做。如果您正在编写传统的 Joomla 模块,这应该是您的 helper.php 文件。
NOTES: Only include the "die" script once in a joomla document.. (defined( '_JEXEC' )..
注意:在 joomla 文档中只包含一次“死”脚本..(定义('_JEXEC')..
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.
JURI::current() 自动读取当前页面 URL。如果你调用 echo JURI::current(); 在带有 url http://www.example.com/names的页面上,它将显示相同的链接。
It is important that the action="" points to the exact Url where you will publish this module.
重要的是 action="" 指向您将发布此模块的确切 URL。
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();)
此外,将数据发布到“SELF”被认为是不好的做法,但是您受到模块的限制,因此除非您构建组件或插件,否则您应该像本示例一样将表单发布到“SELF”。(JURI::current();)
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"
当在 Joomla 框架中时,没有必要声明您的数据库名称、用户名或密码,因为 Joomla 已经“登录”了..所以而不是查询databasename
. jos__tablename
在你的Joomla可以替换本查询#__tablename
。事实上,这是处理 db 查询和 Joomla 时的最佳实践,因为用户不必使用默认的 jos_ 前缀,joomla 会自动用任何前缀替换“# ”。在我的情况下“#”等于“jos”
Take note when querying the sql to create the table.. make sure you replace databasename
with the actual name of your database..
查询 sql 以创建表时请注意.. 确保替换databasename
为数据库的实际名称..
That should do it.
那应该这样做。
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.
如果由于某种原因您无法发布数据: 1) 确保当您单击提交时表单不会重定向到其他页面。如果是这样,请将表单操作“”更改为发布此页面的绝对网址..然后从那里开始。
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:
2) 有时 $data =new 方法不起作用。这取决于您如何设置模块、函数和类。这是一个替代方案:
$db =& JFactory::getDBO();
$query = "INSERT INTO `#__names` (`fname`, `lname`)
VALUES ($fname, $lname);";
$db->setQuery( $query );
$db->query();
回答by Tousif
You may run a custom query to get a result on tmpl file which is simple
您可以运行自定义查询以获取 tmpl 文件的结果,这很简单
<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
$category = $_REQUEST['category'];
if(isset($category))
{
$db = JFactory::getDbo();
$db->setQuery("SELECT machine_id FROM j25_machinefinder_products WHERE category = '$category'");
// Load the row.
$result = $db->loadRowList();
//your result will return here
print_r($result);
}
?>
<form action="" method="get" name="usedequipment">
<select name="category">
<?php foreach($hello as $category)
{
?><option value="<?php echo $category[0]; ?>"> <?php echo $category[0]; ?></option><?php
} ?>
</select>
<input type="submit" />
</form>
回答by Jeepstone
Try something like http://www.chronoengine.com/- Chronoforms
尝试类似http://www.chronoengine.com/- Chronoforms