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

本文是小编为大家收集整理的关于从Joomla表格字段中插入数据库的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

推荐答案

尝试此示例:

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

在数据库中创建一个表.请注意,它应该具有前缀" jos _"

我们将此表格称为"名称".因此,我们将命名我们的表" jos_names"

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

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

要简化事物,我们将结果将结果发布到同一页面.让我们构建表格:

<?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模块,则应该是您的帮助文件.

注意: 仅在Joomla文档中包含一次" DIE"脚本..(定义('_jexec')..

Juri :: Current()自动读取当前页面URL.如果您致电Echo Juri :: current();在带有URL http://www.example.com/names

Action =""指向确切的URL很重要,您将在其中发布此模块.

此外,将数据发布到"自我"中被认为是不好的做法,但是您对模块有限,因此,除非您构建组件或插件,否则您应该将您的表格发布到"自我"中,例子. (Juri :: current();)

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

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

应该做的.

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

2)有时$ data =新方法不起作用.这取决于您如何设置模块,功能和类. 这是一个替代方法:

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

其他推荐答案

尝试类似 http://www.chronoengine.com/ - chronoforms

其他推荐答案

您可以运行自定义查询以在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>

本文地址:https://www.itbaoku.cn/post/597708.html

问题描述

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

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

推荐答案

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_"

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

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>';
}

?>

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

NOTES: Only include the "die" script once in a joomla document.. (defined( '_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.

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

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();)

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"

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

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.

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();   

其他推荐答案

Try something like http://www.chronoengine.com/ - Chronoforms

其他推荐答案

You may run a custom query to get a result on tmpl file which is simple

<?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>