Inserting results of While loop to sql server

peter Ojeda
peter Ojeda used Ask the Experts™
on
Hello experts, I am using the while loop below to populate a table with results from my sql server database. The while loop does its purpose of populating the table, but what I am attempting to do is pull the data from this php page to another php page where an update query is located.
I believe a better format for my question would be How to assign values to text inputs generated in a while loop
Below I am posting the code I am using to pull the data from php to populate the table, You can see I am populated text inputs(which is working) , but do not know how to assign a name to the text boxes after the first record is echo'd. I will provide an image to try to clear what I am saying up.

<table width="500" border="1" id="myTable">
  <tr>

    <th width="91" height="50"> <div align="center">DATE</div></th>
    <th width="91"> <div align="center">CREW</div></th>
    <th width="91"> <div align="center">SHIFT</div></th>
    <th width="91"> <div align="center">LINE</div></th>
    <th width="20PX"> <div align="center">#</div></th>
    <th width="91"> <div align="center">Light Station</div></th>
    <th width="91"> <div align="center">Packer</div></th>
    <th width="91"> <div align="center">Packer</div></th>
    <th width="91"> <div align="center">Packer</div></th>
    <th width="91"> <div align="center">Packer</div></th>
    <th width="91"> <div align="center">Palletizer</div></th>
    <th width="91"> <div align="center">Reliever</div></th>



  </tr>

<?php
while($result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
?>
  <tr>
	<td align="center"  height="50" width="120PX" nowrap> <?php if ($result["DATE"]!= NULL ){echo $result["DATE"] ->format("Y-m-d") ;}?></td>
	<td align="center" width="120px" height="50"  nowrap> <?php echo $result["CREW"];?></td>
	<td align="center" width="10px" height="50"  nowrap> <?php echo $result["SHIFT"];?></td>
	<td align="center" width="50px" height="50"  nowrap><INPUT TYPE="TEXT" NAME="LINE" VALUE=" <?php echo $result["LINE"];?>"></td>
	<td align="center" width="20px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="LINE_NUM" VALUE="<?php echo $result["LINE_NUM"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="PERSON_1" VALUE="<?php echo $result["PERSON_1"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="PERSON_2" VALUE="<?php echo $result["PERSON_2"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="PERSON_3" VALUE="<?php echo $result["PERSON_3"];?>"></td>
	<td align="center" width="140px"height="50"  nowrap> <INPUT TYPE="TEXT" NAME="PERSON_4" VALUE="<?php echo $result["PERSON_4"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="PERSON_5" VALUE="<?php echo $result["PERSON_5"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="PERSON_6" VALUE="<?php echo $result["PERSON_6"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="PERSON_7" VALUE="<?php echo $result["PERSON_7"];?>"></td>





  </tr>

<?php
}
?>

Open in new window

EXPERTEXCHEANGE.PNG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can set the name for the text boxes are array like below, so it will be easy post the value with same name

	<td align="center" width="50px" height="50"  nowrap><INPUT TYPE="TEXT" NAME="LINE[]" VALUE=" <?php echo $result["LINE"];?>"></td>
	<td align="center" width="20px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="LINE_NUM[]" VALUE="<?php echo $result["LINE_NUM"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="ligt_station[]" VALUE="<?php echo $result["PERSON_1"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="paker1[]" VALUE="<?php echo $result["PERSON_2"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="packer2[]" VALUE="<?php echo $result["PERSON_3"];?>"></td>
	<td align="center" width="140px"height="50"  nowrap> <INPUT TYPE="TEXT" NAME="packer3[]" VALUE="<?php echo $result["PERSON_4"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="packer4[]" VALUE="<?php echo $result["PERSON_5"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="palletizer[]" VALUE="<?php echo $result["PERSON_6"];?>"></td>
	<td align="center" width="140px" height="50"  nowrap> <INPUT TYPE="TEXT" NAME="reliever[]" VALUE="<?php echo $result["PERSON_7"];?>"></td>

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
If the app is designed correctly, you might have column names that match the name= attributes in the corresponding input controls.  Since $result is an associative array, the keys can be lined up with the name= attributes, and the values would line up, too.

Author

Commented:
Hi Peos I do not fully understand what you are trying to say. Below is the code on the 2nd php page that I planned to use for the update. The values in the Params array are being posted based on text input Name attributes. That is why i need to have a name for the text inputs that are being created in the while loop.

	if( $conn === false ) {
		die( print_r( sqlsrv_errors(), true));
	}

	$sql = "UPDATE CE_MANNING SET 
				DATE= ? ,
				SHIFT= ? ,
                               LINE= ? ,
				#= ? ,
				LIGHT_STATION= ? ,
                               PERSON_1= ? ,
				PERSON_2= ? ,
				PERSON_3= ? ,
                               PERSON_4= ? ,
				PERSON_5= ? ,
				PERSON_6= ? ,
                               PERSON_7= ? 
				WHERE NAME = ? ";
				
	$params = array($_POST["DATE"], $_POST["SHIFT"], $_POST["LINE"],$_POST["#"], $_POST["LIGHT_STATION"], $_POST["PERSON_1"], $_POST["PERSON_2"], $_POST["PERSON_3"], $_POST["PERSON_4"], $_POST["PERSON_5"], $_POST["PERSON_6"], $_POST["PERSON_7"]);
	

	$stmt = sqlsrv_query( $conn, $sql, $params);
	if( $stmt === false ) {
		 die( print_r( sqlsrv_errors(), true));
	}
	else
	{
		echo "Record update successfully";
	}

	sqlsrv_close($conn);
?>

Open in new window

Author

Commented:
Thank you Peos after further research and testing you were right and this is what I needed to do.
You are welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial