0

我有一个表格,我想显示两个版本的数据。我知道我可以为此设置两个变量,但我不知道如何让它插入到 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) 并且输入旨在接受对数字的调整,无论是它是积极的或消极的。从那里将其插入数据库表并添加到现有总和中,以便新的总数可以反映在底部的灰色表中。

我知道这可能无法完成,但如果无法完成,我需要能够达到我的数据仍将输入到表格中并在提交后反映的地步。以我现有的代码知识,我似乎无法到达那里。

请帮忙?!

4

2 回答 2

1

I can't comment on a question yet, but I think there are a few issues with your php. as @Anubis points out the insert statement needs work. Also, the first few lines of actual php code look like they are wrapped in a javascript event for the window loading. This is not needed as the code doesn't produce any javascript to run. This code will be run on the server during every page load anyway. There are a few other places you have <script> tags that are not needed and probably cause some errors.

I think what you want is to do is add an identifier to your post fields to help get the correct values. So when you are building your input boxes do it like this:

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

And access the submitted values as follows: $_POST['new_count__' . $pcs]

You are trying to integrate two separate processes which makes your code hard to follow and more error prone. I see what you are trying to do but try putting input processing code at the top and display code towards the bottom. So in rough code/comments:

<?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>
于 2013-01-02T17:49:29.813 回答
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!

于 2013-01-02T17:15:37.847 回答