Avatar of peter Ojeda
peter Ojeda
 asked on

Inserting results of While loop to sql server

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
PHPSQL

Avatar of undefined
Last Comment
Peos John

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Peos John

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

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.
peter Ojeda

ASKER
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

peter Ojeda

ASKER
Thank you Peos after further research and testing you were right and this is what I needed to do.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Peos John

You are welcome!