I am working on an inventory program (web based) for my local gameshop. Trying to create an update page. Values are not carrying over

huangapple go评论86阅读模式
英文:

I am working on an inventory program (web based) for my local gameshop. Trying to create an update page. Values are not carrying over

问题

问题是单选按钮的id属性在每个单元格中都是相同的,这导致只有最后一个单选按钮能够正常工作,因为浏览器只能识别到最后一个具有相同id的元素。为了解决这个问题,你需要确保每个单选按钮的id属性是唯一的。

在你的代码中,这个问题出现在以下部分:

<td style="text-align:center"><input type="radio" id="id" name="id" value="<?php echo ($row['id']); ?>"></input></td>

你可以使用循环为每个单选按钮分配唯一的id,例如:

<td style="text-align:center"><input type="radio" id="id_<?php echo ($row['id']); ?>" name="id" value="<?php echo ($row['id']); ?>"></input></td>

这将为每个单选按钮生成一个不同的id,以确保它们能够正常工作。同样的原理也适用于复选框,确保它们具有唯一的id属性。这样,你就可以选择多个项目并将它们传递到下一页。

英文:

I have the first page that displays current inventory. Works fine.
I have the second page that displays items that match the SKU entered on the first page.
If I select the first one, values for dateSold, retail (actual Retail), and tax don't carry to the update page. The SKU and the ID# do. If I select the last one for that SKU, everything works fine.
If there is only one item matching the SKU, it works fine. I am at a loss as to why?

I realized why the last or if only one item works. How can it make it so if I use radio buttons it take the one marked, or even better if I use checkboxes have it post all checked?

&lt;fieldset&gt;
					&lt;h2&gt;Update Inventory&lt;/h2&gt;
					&lt;h5&gt;&lt;span style=&quot;color:black&quot;&gt;Please complete the form below. All fields are Required&lt;/span&gt; * &lt;/span&gt;&lt;/h5&gt;
			 
					&lt;form method=&quot;POST&quot; class=&quot;updateInventory&quot; name=&quot;updateInventory&quot; action=&quot;../updates/changeUpdateInventory.php&quot; id=&quot;updateInventory&quot; &gt;
						&lt;?php
							 $SKUErr = &quot;&quot;;
						
						?&gt;	
							&lt;table&gt;			
						&lt;fieldset id=&quot;Inventory&quot; &gt;	
							&lt;tr&gt;&lt;td&gt;SKU Code: &lt;/td&gt;&lt;td&gt;&lt;input type=&quot;textbox&quot; id=&quot;SKU&quot; name=&quot;SKU&quot; value=&quot;&quot; size=&quot;50&quot; autofocus&gt;&lt;/input&gt;&lt;/td&gt;&lt;/tr&gt;				
							&lt;/table&gt;
							
							&lt;!-- &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt; --&gt;
							 &lt;button type=&quot;submit&quot; value=&quot;Submit&quot; class=&quot;submit&quot;  onClick=&quot;return checkForm(this.parentNode)&quot; &gt;Submit&lt;/button&gt; 
							&lt;br&gt;
					&lt;/form&gt;
					&lt;br&gt;
					&lt;/fieldset&gt;
				&lt;/fieldset&gt;
				
				&lt;div&gt;
				&lt;?php
				require_once &#39;../includes/main2.php&#39;;
				
				try {
					$pdo = new PDO(&#39;mysql:host=&#39; . db_host . &#39;;dbname=&#39; . db_name . &#39;;charset-&#39; . db_charset, db_user, db_pass, [PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION]);
					$skillquery = &quot;SELECT * FROM `inventory` WHERE `dateSold` &lt;= &#39;0000-00-00&#39; ORDER BY `date`, `category`, `itemName`&quot;;
					$q = $pdo-&gt;query($skillquery);
					$q-&gt;setFetchMode(PDO::FETCH_ASSOC);
				} catch (PDOException $e) {
						die (&quot;Could not connect to the database $db_name :&quot; . $e-&gt;getMessage());
				}
		?&gt;
				
				&lt;table style=&quot;width:100%&quot;&gt; 
				&lt;tbody&gt;
					&lt;tr&gt;
						&lt;th style=&quot;width:15%&quot;&gt;SKU: &lt;/th&gt;
						&lt;th style=&quot;text-align:center; width:10%&quot;&gt;Consignee&lt;/th&gt;
						&lt;th style=&quot;text-align:center; width:20%&quot;&gt;Item Name&lt;/th&gt;
						&lt;th style=&quot;text-align:center; width:10%&quot;&gt;Category&lt;/th&gt;
						&lt;th style=&quot;text-align:center; width:20%&quot;&gt;Description&lt;/th&gt;
						&lt;th style=&quot;text-align:center; width:5%&quot;&gt;Retail&lt;/th&gt;
						&lt;th style=&quot;text-align:center; width:5%&quot;&gt;Wholesale&lt;/th&gt;
						&lt;th style=&quot;text-align:center; width:15%&quot;&gt;Date&lt;/th&gt;
					&lt;/tr&gt;
					&lt;?php
						$bg=1;
						while ($row = $q-&gt;fetch()):
						if($bg == 1){
							echo &quot;&lt;tr class=&#39;odd&#39;&gt;&quot;;
							$bg=2;
						}else{
							echo &quot;&lt;tr class=&#39;even&#39;&gt;&quot;;
							$bg=1;
						}
					?&gt;	
					
					
						&lt;td&gt;&lt;?php echo ($row[&#39;SKU&#39;]); ?&gt;&lt;/td&gt;
						&lt;td&gt;&lt;?php echo ($row[&#39;consignee&#39;]); ?&gt;&lt;/td&gt;
						&lt;td style=&quot;text-align:center&quot;&gt;&lt;?php echo ($row[&#39;itemName&#39;]); ?&gt;&lt;/td&gt;
						&lt;td style=&quot;text-align:center&quot;&gt;&lt;?php echo ($row[&#39;category&#39;]); ?&gt;&lt;/td&gt;
						&lt;td style=&quot;text-align:center&quot;&gt;&lt;?php echo ($row[&#39;description&#39;]); ?&gt;&lt;/td&gt;
						&lt;td style=&quot;text-align:center&quot;&gt;&lt;?php echo ($row[&#39;retail&#39;]); ?&gt;&lt;/td&gt;
						&lt;td style=&quot;text-align:center&quot;&gt;&lt;?php echo ($row[&#39;wholesale&#39;]); ?&gt;&lt;/td&gt;
						&lt;td style=&quot;text-align:center&quot;&gt;&lt;?php echo ($row[&#39;date&#39;]); ?&gt;&lt;/td&gt;
					&lt;/tr&gt;
					
					&lt;?php endwhile; ?&gt;
					&lt;/tbody&gt;
				&lt;/table&gt;
					
				&lt;/div&gt;

Page two where the problem starts:

		
				&lt;fieldset&gt;
					&lt;h2&gt;Add Inventory&lt;/h2&gt;
					&lt;h5&gt;&lt;span style=&quot;color:black&quot;&gt;Please complete the form below. All fields are Required&lt;/span&gt; * &lt;/span&gt;&lt;/h5&gt;
			 
		&lt;form method=&quot;POST&quot; class=&quot;changeUpdateInventory&quot; name=&quot;changeUpdateInventory&quot; action=&quot;../updates/processUpdateInventory.php&quot; id=&quot;changeUpdateInventory&quot; &gt;
			&lt;?php
				 $idErr = $SKUErr = $dateSoldErr = $consigneeErr = $itemNameErr = $categoryErr = $descriptionErr = $retailErr = $wholesaleErr = $taxErr = $dateErr = &quot;&quot;;			
			?&gt;	
				&lt;table&gt;			
			&lt;fieldset id=&quot;Inventory&quot; &gt;	
				&lt;tr&gt;				
				&lt;?php
				if (empty($_POST[&#39;SKU&#39;]) )
				{
				echo &quot;&lt;p&gt;You must enter all requested information!&lt;a href=&#39;../updates/updateInventory.php&#39;&gt;&lt;button type=&#39;submit&#39; name=&#39;return&#39;&gt;Return to Form&lt;/button&gt;&lt;/a&gt;&lt;/p&gt;&quot;;
				}
				else 
				{
					$TableName = &#39;Inventory&#39;;
					$SKU = stripslashes($_POST[&#39;SKU&#39;]);
					/*
					var_dump ($SKU);
					echo $SKU;*/
		
				//Get information from db where SKU
				require_once &#39;../includes/main2.php&#39;;
				
				try {
					$pdo = new PDO(&#39;mysql:host=&#39; . db_host . &#39;;dbname=&#39; . db_name . &#39;;charset-&#39; . db_charset, db_user, db_pass, [PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION]);
					$skillquery = &quot;SELECT * FROM `inventory` WHERE `SKU` = &#39;$SKU&#39; AND `dateSold` &lt;= &#39;0000-00-00&#39; ORDER BY `date`, `category`, `itemName`&quot;;
					$q = $pdo-&gt;query($skillquery);
					$q-&gt;setFetchMode(PDO::FETCH_ASSOC);
				} catch (PDOException $e) {
						die (&quot;Could not connect to the database $db_name :&quot; . $e-&gt;getMessage());
				}
				?&gt;
				
				&lt;table style=&quot;width:100%&quot;&gt; 
				&lt;tbody&gt;
					&lt;tr&gt;					
						&lt;th style=&quot;width:5%&quot;&gt;Db ID: &lt;/th&gt;
						&lt;th style=&quot;width:10%&quot;&gt;SKU: &lt;/th&gt;
						&lt;th style=&quot;width:10%&quot;&gt;Selling Date: &lt;/th&gt;
						&lt;th style=&quot;width:10%&quot;&gt;Consignee&lt;/th&gt;
						&lt;th style=&quot;width:10%&quot;&gt;Item Name&lt;/th&gt;
						&lt;th style=&quot;width:10%&quot;&gt;Category&lt;/th&gt;
						&lt;th style=&quot;width:5%&quot;&gt;Retail&lt;/th&gt;
						&lt;th style=&quot;width:10%&quot;&gt;Actual Retail&lt;/th&gt;
						&lt;th style=&quot; width:5%&quot;&gt;Wholesale&lt;/th&gt;
						&lt;th style=&quot;text-align:center; width:5%&quot;&gt;Tax&lt;/th&gt;
						&lt;th style=&quot;width:10%&quot;&gt;Date&lt;/th&gt;
					&lt;/tr&gt;
					&lt;?php
						$bg=1;
						while ($row = $q-&gt;fetch()):
						if($bg == 1){
							echo &quot;&lt;tr class=&#39;odd&#39;&gt;&quot;;
							$bg=2;
						}else{
							echo &quot;&lt;tr class=&#39;even&#39;&gt;&quot;;
							$bg=1;
						}
					?&gt;	
					
					&lt;!-- Check the radio to update this item --&gt;	
						&lt;td style=&quot;text-align:center&quot;&gt;&lt;input type=&quot;radio&quot; id=&quot;id&quot; name=&quot;id&quot; value=&quot;&lt;?php echo ($row[&#39;id&#39;]); ?&gt;&quot;&gt;&lt;/input&gt;&lt;/td&gt;
					&lt;!-- displays SKU left as input to ensure that it carries over in the Submit--&gt;	
						&lt;td&gt;&lt;input type=&quot;text&quot; size=&quot;10&quot; id=&quot;SKU&quot; name=&quot;SKU&quot; value=&quot;&lt;?php echo ($row[&#39;SKU&#39;]); ?&gt;&quot;&gt;&lt;/input&gt;&lt;/td&gt;
					&lt;!-- Enter date product was sold --&gt;	
						&lt;td&gt;&lt;input type=&quot;text&quot; size=&quot;10&quot; id=&quot;dateSold&quot; name=&quot;dateSold&quot; placeholder=&quot;Date sold&quot; value=&quot;&quot;&gt;&lt;/input&gt;&lt;/td&gt;
					&lt;!-- displays Consignee&#39;s name, Item Name, and Category, has Item Description hidden only there for tracking and future needs --&gt;	
						&lt;td&gt;&lt;?php echo ($row[&#39;consignee&#39;]); ?&gt;&lt;/td&gt;
						&lt;td&gt;&lt;?php echo ($row[&#39;itemName&#39;]); ?&gt;&lt;/td&gt;
						&lt;td&gt;&lt;?php echo ($row[&#39;category&#39;]); ?&gt;&lt;/td&gt;
						&lt;h2 class=&quot;hidden&quot;&gt;&lt;?php echo ($row[&#39;description&#39;]); ?&gt;&lt;/h2&gt;
					&lt;!-- displays the current price of the item in the inventory --&gt;	
						&lt;td&gt;$&lt;?php echo ($row[&#39;retail&#39;]); ?&gt;&lt;/td&gt;
					&lt;!-- Should allow the retail price to be entered and updated when submitted --&gt;	
						&lt;td&gt;&lt;input type=&quot;text&quot; id=&quot;retail&quot; name=&quot;retail&quot; value=&quot;&quot; size=&quot;10&quot; placeholder=&quot;Actual Price&quot;&gt; &lt;/input&gt;&lt;/td&gt;
					&lt;!-- displays wholesale price of item --&gt;	
						&lt;td style=&quot;text-align:center;&quot;&gt;$&lt;?php echo ($row[&#39;wholesale&#39;]); ?&gt;&lt;/td&gt;
					&lt;!-- should allow the amount of tax collected to be entered and submitted --&gt;	
						&lt;td&gt;&lt;input type=&quot;text&quot; size=&quot;5&quot; id=&quot;tax&quot; name=&quot;tax&quot; value=&quot;&quot;placeholder=&quot;Taxes&quot;&gt; &lt;/input&gt;&lt;/td&gt;
					&lt;!-- displays the date that the item was added to inventory --&gt;	
						&lt;td&gt;&lt;?php echo ($row[&#39;date&#39;]); ?&gt;&lt;/td&gt;
						
					&lt;/tr&gt;
					
				&lt;?php endwhile; }?&gt;
					&lt;/tbody&gt;
				&lt;/table&gt;
				
				&lt;!-- Submit --&gt;
				 &lt;button type=&quot;submit&quot; value=&quot;Submit&quot; class=&quot;submit&quot; onClick=&quot;return checkForm(this.parentNode)&quot; &gt;Submit&lt;/button&gt; 
				&lt;br&gt;
			&lt;/form&gt;
			&lt;br&gt;
			&lt;/fieldset&gt;
	&lt;/fieldset&gt;

What I get: [enter image description here](https://i.stack.imgur.com/ED8t4.png)

Code from Update Page:

&lt;div&gt;
			&lt;?php
			if (empty($_POST[&#39;SKU&#39;]) )
				{
					echo &quot;&lt;p&gt;You must enter all requested information!&lt;a href=&#39;../updates/changeUpdateInventory.php&#39;&gt;&lt;button type=&#39;submit&#39; name=&#39;return&#39;&gt;Return to Form&lt;/button&gt;&lt;/a&gt;&lt;/p&gt;&quot;;
				}
					else 
				{
			$TableName = &#39;Inventory&#39;;			
			$id = stripslashes($_POST[&#39;id&#39;]);			
			$SKU = stripslashes($_POST[&#39;SKU&#39;]);
			$dateSold = stripslashes($_POST[&#39;dateSold&#39;]);			
			$retail = stripslashes($_POST[&#39;retail&#39;]);			
			$tax = stripslashes($_POST[&#39;tax&#39;]);
			

			/**/
			var_dump ($id);
			var_dump ($SKU);
			var_dump ($dateSold);			
			var_dump ($retail);			
			var_dump ($tax);
			
			
			
	 // Insert the new event into the database  
			require_once &#39;../includes/main2.php&#39;;
		try {
			$conn = new PDO(&#39;mysql:host=&#39; . db_host . &#39;;dbname=&#39; . db_name . &#39;;charset-&#39; . db_charset, db_user, db_pass);
			$conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			// consultation:	
			
			$statement = &quot;UPDATE `Inventory` SET `dateSold`=&#39;$dateSold&#39;, `retail`=&#39;$retail&#39;, `tax`= &#39;$tax&#39; WHERE `id` = &#39;$id&#39; AND `SKU` = &#39;$SKU&#39;&quot;; 

			
			$conn-&gt;exec($statement);
			
			echo &quot;New Inventory Update Entered.&quot;;
			
				} catch (PDOException $e) {
					echo $statement . &quot;&lt;br&gt;&quot; . $e-&gt;getMessage ();
				}
       
        } //header(&#39;Location: ./register_success.php&#39;);
	
?&gt;

&lt;/div&gt;

I don't understand why checking the radio button for the first item (or any other except the last) doesn't carry over, while checking the last item does. Or if it is only one item it works fine. Is there a check that I need to do to get only the Radio button that is checked to carry over? or should it be a checkbox (which would be preferred so I could select multiple items.)

答案1

得分: 0

在你的第二页,用户可以进行更新,你有一个表单。在这个表单中,你动态生成了每个记录的单选按钮的表格行。问题是,当一个SKU有多个记录时,每个行都会生成自己的表单元素,但都包含在同一个表单内。如果用户选择最后一条记录的单选按钮并提交,那么之前记录的所有表单元素(其中的值为空)将覆盖所选记录的值。

为了解决这个问题,你可以分开表单,而不是为所有记录创建一个大表单,你可以为每个记录创建单独的表单,或者你可以使用JavaScript来确保只发送所选的数据。

英文:

In your second page, where users can make updates you have form. Within this form, you are dynamically generating table rows with radio buttons for each record. The problem is when you have multiple records for a single SKU, as each of those rows is generated with it's own form elements, but all are enclosed within the same form. If the user selects the radio button for the last record and submits, then all the form elements of the previous record (which have empty values) will overwrite the values of the selected records.

To fix this you could separate forms, instead of having one big form for all records, you could create separate form for each record OR you can use Javascript to ensure that only the selecetd data is sent.

huangapple
  • 本文由 发表于 2023年7月24日 00:48:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定