update inside array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

update inside array

Post by jayson.ph »

Hi all,

any idea? if possible to update in array? ex.

i have a records displaying as array with 5 columns and 10 rows. now i want to update a single row and any idea on how since it is an array? and it is a single page and no need to link to get the id.

Code: Select all

<tr>
<td><input type = "text" name = "1" value = "<?php echo $rows['1st']; ?>"></td> 
<td><input type = "text" name = "2" value = "<?php echo $rows['2nd']; ?>"></td>
<td><input type = "text" name = "3" value = "<?php echo $rows['3rd']; ?>"></td>
<td><input type = "text" name = "4" value = "<?php echo $rows['4th']; ?>"></td>
<td><input type = "submit" name = "submit" value = "update"></td>
</tr>
Last edited by jayson.ph on Fri Aug 10, 2012 2:07 am, edited 2 times in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: update inside array

Post by social_experiment »

if you have the key of the element in the array that you want to update it is possible

Code: Select all

<?php
 $i = 1;    // this is the key of the element.

 // assume the current value is 10 for the specific element
 $array[1] = 10;
 
 // now the value of this element will be 5 because $i has 
 // a value of 1
 $array[$i] = 5;
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: update inside array

Post by jayson.ph »

can you elaborate more?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: update inside array

Post by requinix »

I think maybe social missed the part about how you're dealing with forms.

Name the textboxes

Code: Select all

name="text[1st]"
then $_POST["text"] will be an array with keys like "1st" and "2nd" and their values will be whatever was in the textboxes.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: update inside array

Post by social_experiment »

Indeed, :) i misread the question

@jayson.ph - the html code you pasted; do you wish to update one array at a time?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: update inside array

Post by jayson.ph »

@jayson.ph - the html code you pasted; do you wish to update one array at a time?
and yes exactly what i mean.. since when i update the single row he did not read my update code because the he read the entire rows of array. and i have error array mssge something like this if im not mistaken:

Code: Select all

      {$ERRMSG[] = '1 '\0'\ value';} else {$_POST['1'] = 'stripslashes_real_escape_string(slashes($_POST['1']))';}


since i have this he read all the '0' zero value of whole array from the 1 value in the table.. i cant imagine how work you posted earlier. i nee more specific if how it work or it is possible.

something like below: i need to update a single row, i need to change the the value of column balance, but however i can't change due to an array value of each column and rows.

Image
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: update inside array

Post by social_experiment »

The table in your previous post, does the array to create it look like the one below

Code: Select all

 $array = array(BranchNameVal, ItemNameVal, ItemVal, ItemDescriptionVal, ReceiveVal, ReturnVal, SoldVal, BalanceVal, QreqVal, DateVal, DateRegVal);
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: update inside array

Post by jayson.ph »

yes, exactly and what was the answer from your reply?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: update inside array[SOLVE]

Post by social_experiment »

I see you have added [SOLVE] to the topic title so this might be too little too late but here is my input;

You add square brackets end of the value inside the name attribute (as pointed out by requinix in another post), thus creating an array which resides in $_POST['holder'].

Code: Select all

<input type="text" value="<?php echo $value; ?>" name="holder[]" />
You don't mention what happens to the array after it's updated so the script below will create a new array with all the values from $_POST['holder']

Code: Select all

<?php
 foreach ($_POST['holder'] as $value) {
    $newArray[] = $value;
 }
?>
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: update inside array[SOLVE]

Post by jayson.ph »

so how about to my variable $rows, i need it to change everything?

and more details, we are really same problem here: http://stackoverflow.com/questions/9899 ... ng-one-row but i dont get it there answer. can you add more your code above to make me clear, please thanks and advance
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: update inside array

Post by social_experiment »

To update a record in the database you will need a unique identifier for the record; a primary key is often good enough for this purpose.
jayson.ph wrote:and it is a single page and no need to link to get the id.
You will have to pass along some sort of unique identifier for the reason explained above. Can you explain how you generate the table with the details and the update button / link ?

Some sample code on how you can update a single record;

Code: Select all

<?php
 // database connection, etc
 
 $id = $_GET['id'];

 // assume $_POST['holder'] contains all the data
 // create an array to hold all the data from the post variable
 foreach ($_POST['holder'] as $value) {
      $dataAry[] = $value; 
 }

 $sql = "UPDATE `table` SET `branch_name` = '" . $dataAry[0] . "', `item_name` = '" . $dataAry[1] . "', 
`item_val` = '" . $dataAry[2] . "', `item_desc` = '" . $dataAry[3] . "' WHERE `uniqueField` = '" . $uniqueValue . "' ";

?>
A note about the order of the fields in the update query : They have to be in the same order as the data in your form because if they aren't you will be updating the wrong fields with the wrong data.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: update inside array

Post by jayson.ph »

here, are my full sourse code:

adm_branch_information.php

Code: Select all

$pingsql = "SELECT tbl_user.uid,
					tbl_user.tbl_brnch_name,
					tbl_brn_requst.id,
					tbl_brn_requst.cat_id,
					tbl_brn_requst.product_name,
					tbl_brn_requst.product_desc,
					tbl_brn_requst.rec_on,
					tbl_brn_requst.return_on,
					tbl_brn_requst.balancs,
					tbl_brn_requst.sold,
					tbl_brn_requst.re_qty,
					tbl_brn_requst.dt_nwo,
					tbl_brn_requst.commnt,
					tbl_category.cat_id,
					tbl_category.cat_name
					FROM tbl_user INNER JOIN tbl_brn_requst ON tbl_user.uid = tbl_brn_requst.uid
					INNER JOIN tbl_category ON tbl_brn_requst.cat_id = tbl_category.cat_id WHERE tempf <> '1988'";
	$pingresult = mysql_query($pingsql);
	
	?>
	<div class = "main_adm_branch_tb"><table>
	<form method = "POST" action = "return_information.php">
	<th>Branch</th>
	<th>Category</th> 
	<th>Item Name</th>  
	<th>Item Description</th> 
	<th>Remark</th> 
	<th>Return</th> 
	<th>Sold</th> 
	<th>Balance</th> 
	<th>Request</th> 
	<th>Date Request</th>
	<th>User Log</th>
	<th>UPDATE</th>
	<?php
	$count = mysql_num_rows($pingresult);
	while($rows = mysql_fetch_array($pingresult))
	{
	?>
		
		<?php $id[] = $rows['id'];?><input type = "hidden" name = "id" value = "<?php echo $rows['id']; ?>">
		<input type = "hidden" name = "uid" value = "<?php echo $rows['uid']; ?>">
		<input type = "hidden" name = "tempf[]" value = "1988" id = "tempf">
		<input type = "hidden" name = "dt_response" value = "<?php echo date("F j, Y"); ?>">
	<tr>
	
	<td>
	<input type = "text" name = "tbl_brnch_name" value = "<?php echo $rows['tbl_brnch_name']; ?>" style = "border:none; width:65px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"/></td>
	<td><input type = "text" name = "cat_name" value = "<?php echo $rows['cat_name']; ?>" style = "border:none; width:80px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"/></td>
	<td><input type = "text" name = "product_name" value = "<?php echo $rows['product_name']; ?>" style = "border:none; width:50px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"/></td>
	<td><input type = "text" name = "product_desc" value = "<?php echo $rows['product_desc']; ?>" style = "border:none; width:160px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"/></td>
	<td><input type = "text" name = "rec_on" value = "<?php echo $rows['rec_on']; ?>" style = "border:none; width:30px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;font-weight:bold;"></td>
	<td><input type = "text" name = "return_on" value = "<?php echo $rows['return_on']; ?>" style = "border:none; width:30px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"></td>
	<td><input type = "text" name = "sold" value = "<?php echo $rows['sold']; ?>" style = "border:none; width:30px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"></td>
	<td><input type = "text" name = "balancs" value = "<?php echo $rows['balancs']; ?>" style = "border:none; width:30px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"></td>
	<td><input type = "text" name = "re_qty" value = "<?php echo $rows['re_qty']; ?>" style = "border:none; width:30px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"></td>
	<td><input type = "text" name = "dt_nwo" value = "<?php echo $rows['dt_nwo']; ?>" style = "border:none; width:80px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"></td>
	<td><input type = "text" name = "commnt" value = "<?php echo $rows['commnt']; ?>" style = "border:none; width:70px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;" readonly = "readonly"></td>
	<td><input type = "submit" name = "UPDATE" value = "UPDATE" style = "border:none;color:red; background:none; width:55px; height:15px;font-family:Tahoma, Geneva, sans-serif;font-size:11px;font-weight:bold;" ></td>
	</tr>
	<?php
	}
	?>
	</form>
	</table>
return_information.php

Code: Select all

		$pingsqla = "UPDATE tbl_brn_requst SET tempf = '$tempf' WHERE id = '$id'";
		$pingresulta = mysql_query($pingsqla) or die ("database error: $pingsqla" .mysql_error());
     header('Location:adm_branch_information.php');
it is not updated, since i am currently gating more information. and only tempf field that i want to edit from the list. and i hope you can give this a little time to understand and to help me. thnanks
Post Reply