0

我一直在尝试创建一个将信息保存在我的数据库中的表单......听起来很容易,但我的文档很乱(或者我不太聪明),我只想在提交时发送一些参数,在另一边捕获他们并在数据库中保存一些信息....

所以,我这样做了:

在我的component/views/myview/tmpl/default.php我写了这个:

<form action="index.php">
<input type="submit" value="test" />
</form>

然后,我转到我的文件view.html.php/mycomponent/view/component/view.html执行此操作:

$this->get('SaveClient');

在我的模型中,我这样做了(仅用于尝试):

public function getSaveClient(){

  $query ="
      Insert into client ( `id_client` ,`test`)
      VALUES 
      (NULL , '1')
  ";
  $db = & JFactory::getDBO();
  $db->Execute($query);
}

但是仍然..不起作用,知道如何使它起作用吗?

谢谢!

4

2 回答 2

2

您没有正确定义表名。应该是#__tablename。一旦你尝试了这个并且它仍然不起作用,请尝试使用下面的代码。

public function getSaveClient($test){ 
    $db = JFactory::getDBO();
    $data = new stdClass();
    $data->id_client = null;
    $data->test = $test;
    $db->insertObject( '#__tablename', $data, 'id_client' );  
}
于 2012-11-06T21:16:54.613 回答
1

你的布局 default.php 形式像

<form action="<?php echo JRoute::_('index.php?option=com_example&view=example&layout=default'); ?>" method="post" name="adminForm">
<input size="60" type="text" name="settings[key]" id="settings[key]" value="<?php echo (isset ($this->settings ['key']) ? htmlspecialchars ($this->settings ['key']) : ''); ?>" />
// More html entites.
<input type="hidden" name="task" value="" />
</form>

你的 view.html.php //你的班级

public function display ($tpl = null)
    {
        $document = &JFactory::getDocument ();
        $document->addStyleSheet ('components/com_example/assets/css/example.css');

        $model = &$this->getModel ();
        $this->settings = $model->getSettings ();
        $this->form = $this->get ('Form');
        $this->addToolbar ();
        parent::display ($tpl);
    }

你的模型你的模型类

public function saveSettings ()
    {
        //Get database handle
        $db = $this->getDbo ();
        $api_settings = array();
        $mainframe = JFactory::getApplication();
        //Read Settings
        $settings = JRequest::getVar ('settings');
                //Insert new settings
          foreach ($settings as $k => $v)
          {

             $sql = "INSERT INTO #__yourtable ( setting, value )" . " VALUES ( " . $db->Quote ($k) . ", " . $db->Quote ($v) . " )";
            $db->setQuery ($sql);
            $db->query ();
          }
              }
于 2012-11-07T04:22:45.087 回答