Jump to content

Recover data from a form and insert them into a mysql table


schoumi26

Recommended Posts

Hello, 

 

I'm in prestashop 1.5.6.2 

 

Sorry for my english

 

I created mypage.php to the site root 

I created MypageController.php in controllers / front folder 

I created mypage.tpl in the folder of my theme where I put a form (contest). 

 

How can I retrieve the values ​​of my form to insert in my table contest? 

What should I put in my controller? 

 

thank you

Link to comment
Share on other sites

you ahve to use DB object to insert these datas to database.

it looks very similar to simple php script.

 

for example, you've got form with button named submit_form, and with input field named "testfield"

in php file you can use:

if (Tools::isSubmit('submit_form')){
Db ::getInstance()->Execute('INSERT INTO MY_TABLE ('testfield') VALUE (tools::getValue('testfield'))");
}
Link to comment
Share on other sites

Hello, 
 
Thank you, I can insert the form data in the DB. 
 
Now I can not view the data on a new page I created.
 
ContestController.php

 

<?php

class ContestControllerCore extends FrontController
{
public $php_self = 'contest';
 
    public          $id_contest;
    public          $id_customer;
    public          $date;
    public          $question;
    public          $response;
    public          $statut;
 
public function initContent()
{
  parent::initContent();
 
  $this->setTemplate(_PS_THEME_DIR_.'contest.tpl');
}
 
public static $definition = array(
  'table' => 'contest',
  'primary' => 'id_contest',
  'fields' => array(
    'id_customer' => array(
                'type' => ObjectModel::TYPE_INT,
                'validate' => 'isUnsignedInt',
                'required' => true,
            ),
            'date' => array(
                'type' => ObjectModel::TYPE_STRING,
            ),
'question' => array(
                'type' => ObjectModel::TYPE_STRING,
            ),
'response' => array(
                'type' => ObjectModel::TYPE_STRING,
            ),
            'statut' => array(
                'type' => ObjectModel::TYPE_STRING,
                'required' => true,
            )
  ),
);
 
public static function findAll() {
$sql = 'select * from ' . _DB_PREFIX_ . 'contest where id_customer = $this->context->cookie->id_customer';
if ($rows = Db :: getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql)) {
return ObjectModel :: hydrateCollection(__CLASS__, $rows);
}
return array();
$contests = findAll();
$this->context->smarty->assign('contests', $contests);
$this->setTemplate('contest.tpl');
}
 
 
}

 

contest.tpl 

 

{capture name=path}<a href="{$link->getPageLink('my-account', true)|escape:'html'}">{l s='My account'}</a><span class="navigation-pipe">{$navigationPipe}</span>{l s='Contest'}{/capture}

{include file="$tpl_dir./breadcrumb.tpl"}
 
<h1>{l s='Contest'}</h1>
 
<div class="block-center" id="block-history">
    {if $contests && count($contests)}
    <table id="order-list" class="std">
        <thead>
            <tr>
                <th class="first_item">{l s='ID'}</th>
<th class="columnDate">{l s='Date'}</th>
<th class="item">{l s='Question'}</th>
<th class="item">{l s='Response'}</th>
                <th class="item">{l s='Statut'}</th>            
            </tr>
        </thead>
        <tbody>
        {foreach from=$contests item=cont name=myLoop}
            <tr>
                <td class="history_method">{$cont->id_contest}</td>
<td class="history_date bold">{$cont->date}</td>
<td class="history_link bold">{$cont->question}</td>
                <td class="history_method">{$cont->response}</td>
                <td class="history_state">{$cont->statut}</td>      
            </tr>
        {/foreach}
        </tbody>
    </table>
    <div id="block-contest-detail" class="hidden"> </div>
    {else}
        <p class="warning">{l s='you have not yet participated in a contest'}</p>
    {/if}
</div>
 
<ul class="footer_links clearfix">
    <li><a href="{$link->getPageLink('my-account', true)}"><img src="{$img_dir}icon/my-account.gif" alt="" class="icon" /> {l s='Back to Your Account' mod='prepayment'}</a></li>
    <li class="f_right"><a href="{$base_dir}"><img src="{$img_dir}icon/home.gif" alt="" class="icon" /> {l s='Home' mod='prepayment'}</a></li>
</ul>

 

The contest page returns no result. I am not a developer and I can not find my error.

 

Thank you

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...