写信给Joomla数据库

时间:2015-05-08 11:19:29

标签: php mysql json joomla

我有一个很酷的joomla组件,帮助我通过创建URL来注册用户(请参阅链接)。 joomla extention

我想做的是能够使用相同的方法在不同的表上写: 这是帮助用户注册的代码:

À0 0ˆÏ0€ÿ0Â0_A°Á0ðBˆB°<AÐ8A

为了更明确,我希望能够在名为belvw_zoo_comment的表中写评论 有没有办法通过修改上面的代码来做到这一点?我在考虑这样的事情:

2AàŒ0ÐÀA…0

当然上面的代码无效。

2 个答案:

答案 0 :(得分:0)

在代码的开头添加:

$comment = JRequest::getVar('comment');

然后在success子句中添加以下块

$db     = JFactory::getDbo();
$query  = $db->getQuery(true);

$query->insert('#__zoo_comment');
$query->set('name_of_field = '.$db->quote($comment));

$db->setQuery( $query );
$db->execute();

您必须将“name_of_field”的名称更改为字段名称,并假定评论位于提交的表单中名为“评论”的字段中。

所以你会得到:

public function registration()
{
    $name = JRequest::getVar('name');
    $username = JRequest::getVar('username');
    $passwd = JRequest::getString('pass');
    $email = JRequest::getVar('email');
    $comment = JRequest::getVar('comment');

    $data = array(
          "name"=>$name,
          "username"=>$username,
          "password"=>$passwd,
          "password2"=>$passwd,
          "email"=>$email,
          "block"=>1,
          "groups"=>array("2"),
          "sendEmail"=>("1"),
        );

    $user = new JUser;
    //Write to database
    if(!$user->bind($data)) {
        $status = "Could not bind data. Error: " . $user->getError();
    }
    if (!$user->save()) {
      $status = "Could not save user. Error: " . $user->getError();
    }
    else {
      $status = "Success";

        //Save comment
        $db     = JFactory::getDbo();
        $query  = $db->getQuery(true);

        $query->insert('#__zoo_comment');
        $query->set('name_of_field = '.$db->quote($comment));

        $db->setQuery( $query );
        $db->execute();


    }

    $message = array(
        'message' => $status
        );

    header('Content-Type: application/json');
    echo json_encode ($message);
    jexit();
}

答案 1 :(得分:0)

这是最终对我有用的代码:

public function event()
{
    //$id = JRequest::getVar('id');
    $ide = JRequest::getVar('ide');
    $name = JRequest::getVar('name');
    $email = JRequest::getVar('email');
    $confirmed = JRequest::getVar('confirmed');
    $quantity = JRequest::getVar('quantity');



    $ip = $_SERVER['REMOTE_ADDR'];
    $date = date('Y-m-d H:i:s');


    //Custom Joomla code for inseting to comment table starts

    $db = JFactory::getDbo();
    // Create a new query object.
    $query = $db->getQuery(true);

    // Insert columns.
    $columns = array('ide', 'name','email','confirmed', 'ip','date'); // table column names

    // Insert values.
    $values = array($db->quote($ide), $db->quote($name), $db->quote($email), $db->quote($confirmed), $db->quote($ip) , $db->quote($date)); // values 

    // Prepare the insert query.
    $query
        ->insert($db->quoteName('#__rseventspro_users'))
        ->columns($db->quoteName($columns))
        ->values(implode(',', $values));

    // Set the query using our newly populated query object and execute it.
    $db->setQuery($query);
    $message=$db->execute();
    $last_id = $db->insertid();


    $query1 = $db->getQuery(true);

    // Insert columns.
    $columns1 = array('ids', 'quantity'); // table column names

    // Insert values.
    $values1 = array($db->quote($last_id), $db->quote($quantity)); // values 



    // Prepare the insert query.
    $query1
        ->insert($db->quoteName('#__rseventspro_user_tickets'))
        ->columns($db->quoteName($columns1))
        ->values(implode(',', $values1));

    // Set the query using our newly populated query object and execute it.
    $db->setQuery($query1);
    $message=$db->execute();



    header('Content-Type: application/json');
    echo json_encode ($message);
    jexit();
    //Custom Joomla code for inseting to comment table starts
}