iT邦幫忙

0

php 表單上傳,數量太多會出現錯誤嗎?

https://ithelp.ithome.com.tw/upload/images/20190917/20076058ShX1Dl08wp.jpg
我用 php 做了一個類似圖片中的表單,
按下加入產品按鈕就可以加入新的產品,
按下確定儲存就會存到 Mysql 數據庫。

我遇到的問題是:
在我自己本地端伺服器時執行都沒問題,
但上傳到虛擬主機商 (戰國策) 的伺服器後,
如果加入的產品數量是 77 個以上時,
按下確定儲存就會出現類似以下的錯誤訊息:
Notice: Undefined index: price in /xxx.php on line 102
Notice: Undefined index: cost in /xxx.php on line 103
Notice: Undefined index: quantity in /xxx.php on line 104

請問:

  1. 如果未定義的索引會引發錯誤,
    那應該數量 76 個以下時也會錯誤啊,
    但為何數量 76 個以下時都沒問題,
    而 77 個以上時才會出現錯誤訊息呢?
  2. 為何在本地伺服器都沒問題,
    但上傳到虛擬主機商的伺服器後才會有問題?
  3. 我該如何修正呢?

謝謝!!!

看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2019-09-17 16:06:56 檢舉
先看你 browser network 那頁

送出去什麼東西
ccutmis iT邦高手 2 級 ‧ 2019-09-17 19:07:55 檢舉
寫個功能判斷超過76個時就不能按[儲存]鍵,選我正解....咯咯咯~~~~
samjam iT邦新手 3 級 ‧ 2019-09-18 11:45:03 檢舉
dragonH 大大:
我看了 browser network 發現 Content-Length 的部分有點可疑,
先測試這地方看看
samjam iT邦新手 3 級 ‧ 2019-09-18 14:26:54 檢舉
原本以為可能是 php 的 memory_limit 設置問題,
改為下方的設定後,還是不行:
ini_set('memory_limit', '800M');
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
firecold
iT邦新手 1 級 ‧ 2019-09-17 18:12:45
最佳解答

如果是上傳檔案

php.ini相關
upload_max_filesize
post_max_size
max_file_uploads
max_input_time
max_input_vars
memory_limit

mysql相關
https://stackoverflow.com/questions/3536103/mysql-how-many-rows-can-i-insert-in-one-single-insert-statement

蠻多都有關係的

但是看你的情況應該是code沒寫好而已
看你要不要貼code吧

看更多先前的回應...收起先前的回應...
samjam iT邦新手 3 級 ‧ 2019-09-18 14:37:10 檢舉

firecold 大大:
查看虛擬主機商的 php 設置後,
發現 max_input_vars 最大為 1000 個,
測試了一下,確定問題是表單送出的數量超過 1000 個。
我設置了 .htaccess 文件,
下面的寫法無效,還是會錯誤:

php_value max_input_vars 5000

請問還有什麼方式可以更改 max_input_vars 的設定嗎?
是否必須要請虛擬主機商幫我改呢?

firecold iT邦新手 1 級 ‧ 2019-09-18 14:59:03 檢舉

我推測試code的問題,方便提供code嘛
77筆你範例的資料不太可能是ini的問題

samjam iT邦新手 3 級 ‧ 2019-09-18 15:39:11 檢舉

firecold 大大:
抱歉沒有說明清楚,
我圖片的範例是簡化過的,
實際上的表單是如下:

<form action='' method='POST'>
	<table class='formTable_01'>
		<tr>
			<th>採購單日期</th>
			<td><input type='text' name='datepicker' value='<?php echo $formItems['datepicker']; ?>' id='datepicker' /></td>
		</tr>
		<tr>
			<th>供應商</th>
			<td><?php echo $supplierSelectOption; ?></td>
		</tr>
		<tr>
			<th>匯率</th>
			<td><input type='text' name='exchange_rate' value='<?php echo $exchange_rate; ?>' id='exchange_rate' class='isNumber' /></td>
		</tr>
	</table>

	<table class='formTable_02'>
		<tr>
			<th>採購單編號</th>
			<td>
				<?php
				echo "
					{$formItems['purchase_number']}
					<input type='hidden' name='purchase_number' value='{$formItems['purchase_number']}' />
				";
				?>
			</td>
		</tr>
		<tr>
			<th>總標準成本</th>
			<td class='bgBlue'>
				<?php
				echo "
					{$cost_standard_total}
					<input type='hidden' name='cost_standard_total' value='{$cost_standard_total}' id='cost_standard_total' />
				";
				?>
			</td>
		</tr>
		<tr>
			<th>總採購成本</th>
			<td class='bgYellow'>
				<?php
				echo "
					{$cost_original_total}
					<input type='hidden' name='cost_original_total' value='{$cost_original_total}' id='cost_original_total' />
				";
				?>
			</td>
		</tr>
	</table>

	<div class='spacing'></div>

	<table class='formTable_03'>
		<tr>
			<td colspan='20'>
				<?php echo $viewer->getAddProductsSubmit(); ?>
			</td>
		</tr>
		<tr>
			<th>產品代碼</th>
			<th>產品名稱</th>
			<th>零售價</th>
			<th class='w7em'>標準外幣</th>
			<th class='w7em'>標準成本</th>
			<th class='w7em'>採購外幣</th>
			<th class='w7em'>採購成本</th>
			<th class='w8em'>折數</th>
			<th class='w9em'>採購量</th>
			<th class='w9em'>標準成本小計</th>
			<th class='w9em'>採購成本小計</th>
			<th>移除</th>
		</tr>

		<?php
		if (isset($formItems['products'])) {
			$products = $formItems['products'];
			foreach ($products as $x=>$y) {
				// 格式化小數點
				$price 						= $formatDecimal->setPrice($y['price']);
				$foreign_standard 			= $formatDecimal->setPurchaseMoney($y['foreign_standard']);
				$cost_standard 				= $formatDecimal->setPurchaseMoney($y['cost_standard']);
				$foreign_original 			= $formatDecimal->setPurchaseMoney($y['foreign_original']);
				$cost_original 				= $formatDecimal->setPurchaseMoney($y['cost_original']);
				$discount 					= $formatDecimal->setDiscount($y['discount']);
				$purchase_quantity 			= $formatDecimal->setQuantity($y['purchase_quantity']);
				$item_total_cost_standard 	= $formatDecimal->setPurchaseMoney($y['item_total_cost_standard']);
				$item_total_cost_original 	= $formatDecimal->setPurchaseMoney($y['item_total_cost_original']);

				$theBoolean = $model->canRemove($y['purchase_item_id']);
				$remove = $viewer->getRemove($y['purchase_item_id'], $theBoolean);

				echo "
					<tr>
						<td>
							{$y['brand_product_number']}
							<input type='hidden' name='products[{$x}][purchase_item_id]' value='{$y['purchase_item_id']}' />
							<input type='hidden' name='products[{$x}][product_id]' value='{$y['product_id']}' />
							<input type='hidden' name='products[{$x}][brand_product_number]' value='{$y['brand_product_number']}' />
						</td>
						<td>
							{$y['full_product_name']}
							<input type='hidden' name='products[{$x}][full_product_name]' value='{$y['full_product_name']}' />
						</td>
						<td class='alignRight'>
							{$price}
							<input type='hidden' name='products[{$x}][price]' value='{$price}' />
						</td>
						<td class='bgBlue'>
							<input type='text' name='products[{$x}][foreign_standard]' value='{$foreign_standard}' class='alignRight isNumber foreign_standard' />
						</td>
						<td class='alignRight bgBlue'>
							{$cost_standard}
							<input type='hidden' name='products[{$x}][cost_standard]' value='{$cost_standard}' class='cost_standard' />
						</td>
						<td class='bgYellow'>
							<input type='text' name='products[{$x}][foreign_original]' value='{$foreign_original}' class='alignRight isNumber foreign_original' />
						</td>
						<td class='alignRight bgYellow'>
							{$cost_original}
							<input type='hidden' name='products[{$x}][cost_original]' value='{$cost_original}' class='cost_original' />
						</td>
						<td class='alignRight'>
							{$discount}
							%
							<input type='hidden' name='products[{$x}][discount]' value='{$discount}' class='discount' />
						</td>
						<td>
							<input type='text' name='products[{$x}][purchase_quantity]' value='{$purchase_quantity}' class='alignRight isNumber purchase_quantity' />
						</td>
						<td class='alignRight bgBlue'>
							{$item_total_cost_standard}
							<input type='hidden' name='products[{$x}][item_total_cost_standard]' value='{$item_total_cost_standard}' class='item_total_cost_standard' />
						</td>
						<td class='alignRight bgYellow'>
							{$item_total_cost_original}
							<input type='hidden' name='products[{$x}][item_total_cost_original]' value='{$item_total_cost_original}' class='item_total_cost_original' />
						</td>
						<td>
							{$remove}
						</td>
					</tr>
				";
			}
		}
		?>

		<tr>
			<td colspan='20'>
				備註:
				<textarea name='remark'><?php echo $formItems['remark']; ?></textarea>
			</td>
		</tr>
	</table>

	<?php
	echo $viewer->getRemoveList($formItems);
	echo $viewer->getConfirmSubmitInBox();
	?>
	
</form>

每一個產品會有 13 個 input,
76 個產品總共會送出 988 個,
加上其他單獨的幾個 input,
送出的數量差不多是 1000 個。

然後我測試把每個產品刪除二個欄位,變成 11 個 input,
結果可以送出 90 個產品,
所以我想應該就是 max_input_vars 的問題了。

只是我沒辦法自己更改設置 ...

samjam iT邦新手 3 級 ‧ 2019-09-19 09:59:27 檢舉

firecold 大大:
已請虛擬主機商幫我改好了,
max_input_vars 的設置加大之後就正常了,
謝謝您提供的資訊。

firecold iT邦新手 1 級 ‧ 2019-09-19 10:26:21 檢舉

恭喜解決問題
.htaccess無效的主機商 基本上都拒絕使用
超多坑

samjam iT邦新手 3 級 ‧ 2019-09-19 11:38:10 檢舉

還好這家還蠻幫忙的,
運氣不錯,
哈哈!

0
永往直前
iT邦新手 4 級 ‧ 2019-09-18 16:54:47

PHP Notice 而已,

在程式碼起始點加上這行就好!

error_reporting(E_ALL ^ E_NOTICE);

讓 Notice 不要印…

samjam iT邦新手 3 級 ‧ 2019-09-19 10:03:07 檢舉

Chiyung579 大大:
是因為 php 5.3.9 版本後有設置 max_input_vars 1000 的限制,
造成表單送出超過 1000 個之後的數據不會被接收到而造成錯誤,
max_input_vars 加大之後就正常了,
謝謝您。

我要發表回答

立即登入回答