需要表格才能使用多个提交按钮

时间:2013-01-02 16:49:09

标签: php jquery forms postgresql insert-update

我有一个表单,我希望显示两个版本的数据。我知道我可以为此设置两个变量,但我无法弄清楚如何将其插入到postgres数据库中。我已经填充了数据,并在标签中输入新数字以便插入表中。

这是我到目前为止所拥有的......

<script>
window.onload = function() {
    <?
    $yesterday = date('Y-m-d', strtotime('-1 day'));
    if ($_REQUEST['start'] == '') {
        $_REQUEST['stores'] = array(1,2,3,7,8,9,17,18,19,23,16,11,4,5,6);
        $_REQUEST['start'] = $yesterday;
    } else {
        $_REQUEST['stores'] = array(1,2,3,7,8,9,17,18,19,23,16,11,4,5,6);
    }
    $_REQUEST['submit'] = 'Get Data'; 
    ?>
}

</script>

<?
// various arrays and db connections we're going to need later 
include('/home/...some_data_goes_here.../db_connect.inc');

$dbx = DB::connect('******');
$dbx->setFetchMode(DB_FETCHMODE_ASSOC);

//get the data based on the date.
$start = $_REQUEST['start'];

//$stores = array(1 => '1,177,18', 2 => '2,277,28', 3 => '3,377,38', 7 => '4,477,48', 8 => '5,577,58', 18 => '338', 19 => '50,51', 9 => '6,677,68', 17 => '8,877,818', 16 => '44,45,47', 11 => '7,770,78', 4 => '11,15,17', 5 => '22,25,27', 6 => '33,35,37');

$formstores = $_REQUEST['stores'];

foreach($stores as $sid => $pcs) {
    $store_name = $db->getOne('SELECT store_name FROM stores WHERE store_id = ?', array($sid));
}

foreach($formstores as $k => $sid) {

        if(empty($storeDataMain)){ //array is empty so make one
            $storeDataMain = array();
        }

        //get the store names
        $store_name = $db->getOne('SELECT store_name FROM stores WHERE store_id = ?', array($sid));
        if(DB::isError($store_name)) { echo '<div class="error">Error '.$store_name->getdebuginfo().'</div>'; return; }

        $tempups = $db->getOne('SELECT sum(count) FROM traffic WHERE store_id = ? AND tdate = ?', array($sid,$start));
        //echo $tempups .' | ';
        if(DB::isError($tempups)) { echo '<div class="error">Error '.$tempups->getdebuginfo().'</div>'; return; }
        $tups = $tempups/2;
        //echo $tups;

        //Build array out and return it 
        $storeDataMain[$store_name] = array('trafficGuests' => number_format(floatval($tups),1,'.',',')); // floatval prevents the numbers from rounding to zero
}   
?>

<h3 class="heading">Traffic Updates</h3>
<p>Enter dates in yyyy-mm-dd format.</p>

<script type="text/javascript" src="/css/prototype.js"></script>
<script type="text/javascript" src="/css/scriptaculous.js"></script>
<script type="text/javascript" src="/css/datepicker.js"></script>
<form name="setTraffic" method="get" action="/traffic/updateTraffic.php">

<!-- Build the table where they can input the date to be changed -->
<table>
    <tr>
        <th>Date</th>
    </tr>
    <tr>
        <td>
            <input type="text" name="start" id="start" value="<?=$_REQUEST['start']?>" alt="mm/dd/yyyy" onblur="if(this.value.length){valid=check_date(this.value);if(!valid){this.value='';this.focus();alert('Invalid date input');}else{this.value=valid;}}" onchange="if(this.value.length){valid=check_date(this.value);if(!valid){this.value='';this.focus();alert('Invalid date input');}else{this.value=valid;}}" onfocus="if(this.value == this.defaultValue) this.value = ''" /></td>
            <script type="text/javascript">
                                var start_picker = new DatePicker({
                                    relative    : 'start',
                                    language    : 'en',
                                    dateFormat  : 'yyyy-mm-dd',
                                    showDuration: 0.1,
                                    closeEffectDuraction: 0.1,
                                    disableFutureDate: true,
                                    disablePastDate: false,
                                    keepFieldEmpty : true
                                });
            </script>
        </td>
    </tr>
</table>
<input class="button" type="submit" name="submit" value="Get Data" id="submit" />
</form>
<form method="post" action="">
    <table>
        <tr>
            <th style="height: 20px; width: 165px; vertical-align: bottom;">Stores</th>
                <? //build the list of stores for the headers.
                    foreach($storeDataMain as $k => $val){
                        echo '<th style="text-align: center; vertical-align:   bottom;">'.$k.'</th>';
                    }
                ?>
        </tr>
        <tr>
            <th style="height: 20px; vertical-align: bottom;">Guests for <?=$start?>    </th>
                <? //format and print out the traffic for each store
                    foreach($storeDataMain as $k => $val){
                    echo '<td style="text-align: center; vertical-align: bottom; font-size: 14px;">'.$val["trafficGuests"].'</td>';
                } 
            ?>
    </tr>  


    <tr>
        <th style="height: 20px; vertical-align: bottom;">Adjustment for <?=$start?></th>
        <? //build the input fields identified based on store id.
                foreach($formstores as $pcs) {
                    echo '<td><input class="newTraffic" type="number" style="width: 65px; text-align: right;" name="new_count" id="'.$pcs.'" value="'.$count.'" />'.$count.'</td>';
                    <script>// get the data ready for inserting into the database
                        if(isset($count)){
                            $count = $_POST['']
                        } else {
                            $count = '';
                        }}

                        if( $_POST ){
                            $count = $_POST["count"];
                            $store_id = $sid

                        $insertCounts = $db->query('INSERT INTO traffic (store_id, thour, tdate, count, door) VALUES ('$sid', )');
                    }
                }
                </script>

                }
            ?>
        </tr>      
    </table>
    <input class="button" type="submit" name="submit2" value="Submit" id="submit" />
</form>


<h5 style="color: #707070;">New Traffic Counts</h5>
<table style="color: #707070;">
        <tr>
            <td style="height: 20px; width: 165px; font-weight: bold; vertical-align: bottom;">Stores</td>
            <? //display the list of stores for the output of the new counts.
                    foreach($storeDataMain as $k => $val){
                        echo '<td style="text-align: center; width: 69px; padding: 4px; font-weight: bold; vertical-align: bottom;">'.$k.'</td>';
                    }
                ?>
        </tr>
        <tr>
            <td style="height: 20px; vertical-align: bottom; font-weight: bold;">Guests for <?=$start?></td>
                <? //format and display the NEW traffic for each store
                    foreach($storeDataMain as $k => $val){
                        $newCount = $val["trafficGuests"] + $count[$pcs];
                        echo '<td style="text-align: center; vertical-align: bottom; font-size: 14px;">'.$newCount.'</td>';
                    } 
                ?>
        </tr> 
</table>
</form>

如果你想看看页面是什么样的,你可以去[这里](https://picasaweb.google.com/105736785937866961668/January22013#5828892865441021874)并且输入设计用于调整数字,无论是正面的还是负面的。从那里它被插入到DB表中并添加到现有的总和中,以便新的总计可以反映在底部的灰色表中。

我知道这可能无法完成,但如果不能,我需要能够达到我的数据仍然会输入到表中然后在提交后反映出来的程度。我似乎无法用我现有的代码知识实现目标。

请帮帮忙?!

2 个答案:

答案 0 :(得分:1)

我还不能评论一个问题,但我认为你的php存在一些问题。正如@Anubis所指出的,insert语句需要工作。此外,实际php代码的前几行看起来像是在javascript事件中包含窗口加载。这不是必需的,因为代码不会产生任何运行的JavaScript。无论如何,此代码将在每个页面加载期间在服务器上运行。还有一些其他地方你不需要<script>标签,可能会导致一些错误。

我认为您想要做的是在帖子字段中添加标识符以帮助获取正确的值。因此,当您构建输入框时,请执行以下操作:

echo '<td><input class="newTraffic" type="number" style="width: 65px; text-align: right;" name="new_count__'.$pcs.'" id="'.$pcs.'" value="'.$count.'" />'.$count.'</td>';

并按如下方式访问提交的值:$_POST['new_count__' . $pcs]

您正在尝试集成两个单独的进程,这会使您的代码难以遵循并且更容易出错。我看到你要做的是尝试将输入处理代码放在顶部并将代码显示在底部。所以粗略的代码/评论:

<?php //check for and process the new data
    //if posted values
    //process $_POST for values to add/subtract
    //if(isset($_POST)) {
    //if(!empty($_POST) {
    switch ($_POST['submit']) {
        case 'submit':
            //new date selected
            //get new display data
            break;
        case 'submit2':
            //new traffic counts submitted
            //for each input submitted
            //add/subtract value to current database traffic value
            //get new display data
            break;
        default:
            //stuff if no submit
            //get default display data
    }

?>

<html>
    display html data here
</html>

答案 1 :(得分:0)

您的插入语句错误:

//, thour, tdate, count, door <- are there default values defined for theses? if not set the values
$db->query('INSERT INTO traffic (store_id) VALUES ('.$sid.')');

编程时应使用此错误设置:

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

这会在出现问题时为您提供大量错误和描述,并使调试更容易。

在生产环境中“display_errors”应为0!

相关问题