2016-11-18 53 views
-2

我有以下Mysql表和PHP代碼。我的php循環沒有使用MYsql填充我的數據庫

MySQL:我從Store表中選擇商店的數量。哪個返回我的商店列表。

我也有一個cashup表信息

PHP: 環路會發生這樣的NUMBEROFDAYS被假設是在一個月的日子。

在while循環中,我做了一個SQl select,它根據日期和商店從cashup表中返回我的值。

在循環中我增加了我的日子以允許在我所有的商店中循環31天以返回cashup總和值。

//loop days 
$numberofdays = 31; 
$monthyear = NOV_2016; 
$x = 1; 
     //loop days 
     do { 
      //string days info 
      $thedate = $x . '_' . $monthyear; 

      if (strlen($thedate) == 10){ 
        $thedate = '0' . $thedate; 
        } 
       //Do select 
       mysql_select_db($database_localhost, $localhost); 
       $query_showr = "select sum(cashup_cash) from cashiercashdata where cashup_date LIKE '$thedate' and cashup_store = '$store'"; 
       $showr = mysql_query($query_showr, $localhost) or die(mysql_error()); 
       while ($row_showr = mysql_fetch_assoc($showr)) 
         { 
          //loop through record rows 
          foreach($row_showr as $row) 
           { 
           ///////$insertday = "recon_d" . $x . "_received"; 
           echo " Store : " . $store . " Counter : " . $x . ' : ' . gettype($x) . ' : ' . $row . ' : ' . gettype($row) . '<br>'; 
          mysql_query(" 
          update bankrecon 
          set recon_d10_received = '$row' 
          WHERE recon_store = '$store' 
          and recon_month = '$monthyear' 
          "); 
        } 
         } 
$x++; 
     } while ($x <= $numberofdays); 

PHP: 環路會發生這樣的NUMBEROFDAYS(整數)假設是在一個月的日子。

在while循環中,我做了一個SQl select,它根據日期和商店從cashup表中返回我的值。

在循環中我增加了我的日子以允許在我所有的商店中循環31天以返回cashup總和值。

此回顯正確顯示我的信息,但我的循環更新只在numberofdays設置爲靜態10時才插入信息;

Store : centralpark Counter : 10 : integer : 56277.40 string 
Store : ficksburg Counter : 10 : integer : 42698.14 string 
Store : kroonstad Counter : 10 : integer : 28486.70 string 
Store : maitland Counter : 10 : integer : 55988.40 string 
Store: od Counter : 10: integer 41412.10 string 

它在顯示上正常工作,但不更新目標表。 任何人都可以請向我解釋爲什麼numberofdays信息的循環不會觸發列的更新,但如果numberofdays是靜態更新。現在

enter image description here

+0

打印SQL查詢,你會得到錯誤。 –

+0

不幸的是,打印sql和回顯值給出了相同的結果。信息與這些值正確對應。意思是我的查詢是正確的。內循環中的更新會正確更新目標表,但前提是numberofdays var將設置爲static 10 int。使用while循環或for循環的動態更改值的迭代會導致update語句不能正確響應 –

回答

0

,很難確切地知道不知道你的目標表是什麼樣子,但我猜它是與您的UPDATE語句。

update bankrecon 
    set recon_d10_received = '$row' 
    WHERE recon_store = '$store' 
    and recon_month = '$monthyear' 

具體該位:set **recon_d10_received** = '$row'

是,也許說的是更新表只有10一天?

+0

當前我正在測試更新到recon_d10_received。這些選擇的目的是爲每個月的每一天的30個商店填充提供信息的網格。 $ x增量將在循環的每次迭代中動態改變。更新工作正常時,爲10的靜態整數給予天數。但是,當循環迭代循環中的$ x = 10時,信息仍然正確回顯,但更新忽略值ti插入到目標表中的norlan varchar列。 –

+0

請在服務器上查找表格插入和文件以便更清楚地理解。要插入的數據庫稱爲cashup。 https://www.dropbox.com/s/0rlsm73uqy7p42o/bankrecon.sql?dl=0 https://www.dropbox.com/s/bh6srw7wic4y89k/cashiercashdata.sql?dl=0 https://開頭www.dropbox.com/s/55nyzuu9i2vhlv1/store.sql?dl=0 https://www.dropbox.com/s/9ly02qoyw88f51r/gridtest.php?dl=0 –

0

兩個示例表的SQL:

CREATE TABLE `bankrecon` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `recon_d10_received` varchar(45) DEFAULT NULL, 
    `recon_store` varchar(45) DEFAULT NULL, 
    `recon_month` varchar(45) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `data and store` (`recon_store`,`recon_month`,`recon_d10_received`) 
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8; 


CREATE TABLE `cashiercashdata` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `cashup_cash` int(11) DEFAULT NULL, 
    `cashup_date` varchar(45) DEFAULT NULL, 
    `cashup_store` varchar(45) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; 

和PHP本身:

session_start(); 

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

$mysqli = new mysqli("localhost", "someuser", "somepass", "somedb"); 

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

    $store = 'somestrome'; 
    $numberofdays = 31; 
    $monthyear = 'NOV_2016'; 
    $x   = 1; 
    //loop days 
    do { 
     //string days info 
     $thedate = $x.'_'.$monthyear; 

     if (strlen($thedate) == 10) { 
      $thedate = '0'.$thedate; 
     } 
     $showr = $mysqli->query("select sum(cashup_cash) as cashup_cash from cashiercashdata where cashup_date LIKE '$thedate' and cashup_store = '$store'")->fetch_assoc(); 
     if(!is_null($showr['cashup_cash'])){ 
      $sql = " INSERT INTO bankrecon (recon_d10_received,recon_store,recon_month) VALUES ('".$showr['cashup_cash']."','$store','$monthyear') ON DUPLICATE KEY UPDATE recon_d10_received='".$showr['cashup_cash']."'"; 
      $mysqli->query($sql); 
      echo " Store : ".$store." Counter : ".$x.' : '.gettype($x).' : '.$showr['cashup_cash'].' : '.gettype($showr['cashup_cash']).'<br>'; 
     } 


     $x++; 
    } while ($x <= $numberofdays); 

請記住:

  • 更改$店VAR
  • 在使用insert-on-duplicate的列recon_store,recon_month,recon_d10_received上保留唯一密鑰。
  • 更改數據庫連接代碼

還可以使用此PHP代碼,看到更多的錯誤:

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);