[SOLVED] _POST returns wrong number

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
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

[SOLVED] _POST returns wrong number

Post by LuaMadman »

Okey, first of I have a php code that generates
a table with the correct info, the last cell cotains the key in the array, witch is correct

Code: Select all

<?php // <<added to easy read on forum...
	if($showmodels) {
		if($_POST['manufacturerselect']) {
			$man_name = $_POST['manufacturerselect']; //set var to selected manufacturer
		} elseif ($_POST['selman']) {
			$man_name = $_POST['selman']; //set var to manufacturer that was previsoly stored
		}
		$sql = "SELECT * FROM bytin_manufacturer WHERE manufacturer_name='$man_name'";
		$query = mysql_query($sql);
		$rows = mysql_fetch_array($query);
		$models = $rows['manufacturer_models'];
		$file = fopen($modelfile,'w');
		fwrite($file,$models);
		fclose($file);
		$array = unserialize($models);
	?>
		<form name="models" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
			<input type="hidden" name="selman" value="<?php echo($man_name) ?>"/> <!--  Store our manufacturer -->
			<table border="1">
				<tr>
					<td width="100px;">Modell</td>
					<td width="100px;">Pris</td>
					<td width="150px;">Avdrag utan kartong</td>
					<td width="50px;">Edit</td>
					<td width="50px">Delete</td>
				</tr>
				<?php
					$c = count($array);
					for($i = 0;$i < $c;$i++) { // Create table with model info
					?>
					<tr>
						<td width="100px;"><?php echo($array[$i][0]); ?></td>
						<td width="100px;"><?php echo($array[$i][1]); ?></td>
						<td width="150px;"><?php echo($array[$i][2]); ?></td>
						<td width="50px;">
							<input type="hidden" name="editindex" value="<?php echo($i); ?>">
							<input type="submit" name="modelindexedit" value="Edit"/>
						</td>
						<td width="50px">
							<input type="hidden" name="delindex" value="<?php echo($i); ?>">
							<input type="submit" name="modelindexdel" value="Delete"/>
						</td>
						<td>
							<?php echo($i); ?>
						</tf>
					</tr>
					<?php
					}
				?>
			</table>
		</form>
		<?php echo($_POST['editindex']) ?>
		<form name="modeledit" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
			<input type="hidden" name="selman" value="<?php echo($man_name) ?>"/>
			<input type="hidden" name="index" value="<?php echo($_POST['editindex']); ?>"><!-- Get the index of our model to edit -->
			<input type="text" name="modelname" value="<?php echo($array[ $_POST['editindex'] ][0]); ?>" /> Modell<br/>
			<input type="text" name="modelprice" value="<?php echo($array[ $_POST['editindex'] ][1])?>" /> Pris<br/>
			<input type="text" name="modelnocarbon" value="<?php echo($array[ $_POST['editindex'] ][2])?>" /> Utan kartong<br/>
			<input type="submit" name="addmodel" value="Edit" />
			<!-- ? Add a clear button, so I can add a model after clickng on a Edit field -->
		</form>
	<?php
	}
// vv added to easy read on forum...
?>
At first render it dosent have the editindex in post.
So the 2nd form is empty, as it should be.
Now if i click a edit button, it will do this function

Code: Select all

if($_POST['modelindexedit']) {
	$showmodels = true;
	$man_name = $_POST['selman']; // set var to selected manufacturer stored in form
}
Witch is just the same function but with the selman, to get my manufacturer, and also the editindex.
By my understandning, the editindex, should be the $i value of each Edit button, but for some reason, it returns the last $i, so all i can do is edit the last key in the array
Last edited by LuaMadman on Fri Feb 11, 2011 8:47 pm, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: _POST returns wrong number

Post by social_experiment »

Are you refering to this section of code?

Code: Select all

$c = count($array);
                                        for($i = 0;$i < $c;$i++) { // Create table with model info
                                        ?>
                                        <tr>
                                                <td width="100px;"><?php echo($array[$i][0]); ?></td>
                                                <td width="100px;"><?php echo($array[$i][1]); ?></td>
                                                <td width="150px;"><?php echo($array[$i][2]); ?></td>
                                                <td width="50px;">
                                                        <input type="hidden" name="editindex" value="<?php echo($i); ?>">
                                                        <input type="submit" name="modelindexedit" value="Edit"/>
                                                </td>
                                                <td width="50px">
                                                        <input type="hidden" name="delindex" value="<?php echo($i); ?>">
                                                        <input type="submit" name="modelindexdel" value="Delete"/>
                                                </td>
                                                <td>
                                                        <?php echo($i); ?>
                                                </tf>
                                        </tr>
                                        <?php
                                        }

“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
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: _POST returns wrong number

Post by LuaMadman »

Yes, in that code something goes wrong.
all td's appears fine on the page.
but when i click on one of the edit's button, editindex is always the last value of $i
How do i get the editindex to actually return the value I have set?

btw, checking source code, all editindex has the correct values $i
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: _POST returns wrong number

Post by LuaMadman »

Figuerd it out!
The code previsoly posted didn't work, beacuse the entiere table was in the same form.
There for when hitting Edit button, all editindex was sent, but only the last set was given to the text field, thus always editing the last entry.

What I did to solve it was to put the php generated cells in to it own table and form.
Code now looks like this.

Code: Select all

<?php
	if($showmodels) {
		if($_POST['manufacturerselect']) {
			$man_name = $_POST['manufacturerselect']; //set var to selected manufacturer
		} elseif ($_POST['selman']) {
			$man_name = $_POST['selman']; //set var to manufacturer that was previsoly stored
		}
		$sql = "SELECT * FROM bytin_manufacturer WHERE manufacturer_name='$man_name'";
		$query = mysql_query($sql);
		$rows = mysql_fetch_array($query);
		$models = $rows['manufacturer_models'];
		$file = fopen($modelfile,'w');
		fwrite($file,$models);
		fclose($file);
		$array = unserialize($models);
		print_r($array);
	?>
			<table>
				<tr>
					<td width="105px;"><strong>Modell</strong></td>
					<td width="105px;"><strong>Pris</strong></td>
					<td width="150px;"><strong>Avdrag utan kartong</strong></td>
					<td width="55px;"><strong>Edit</strong></td>
					<td width="60px"><strong>Delete</strong></td>
				</tr>
			</table>
				<?php
					$c = count($array);
					for($i = 0;$i < $c;$i++) { // Create table with model info
					?>
					<form name="models" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
						<table border="1">
						<input type="hidden" name="selman" value="<?php echo($man_name) ?>"/> <!--  Store our manufacturer-->
						<tr>
							<td width="100px;"><?php echo($array[$i][0]); ?></td>
							<td width="100px;"><?php echo($array[$i][1]); ?></td>
							<td width="150px;"><?php echo($array[$i][2]); ?></td>
							<td width="50px;">
								<input type="hidden" name="editindex" value="<?php echo($i); ?>">
								<input type="submit" name="modelindexedit" value="Edit"/>
							</td>
							<td width="60px">
								<input type="hidden" name="delindex" value="<?php echo($i); ?>">
								<input type="submit" name="modelindexdel" value="Delete"/>
							</td>
						</tr>
						</table>
					</form>
					<?php
					}
				?>
		<form name="modeledit" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
			<input type="hidden" name="selman" value="<?php echo($man_name) ?>"/>
			<input type="hidden" name="index" value="<?php echo($_POST['editindex']); ?>"> <!-- Get the index of our model to edit -->
			<input type="text" name="modelname" value="<?php echo($array[ $_POST['editindex'] ][0]); ?>" /> Modell<br/>
			<input type="text" name="modelprice" value="<?php echo($array[ $_POST['editindex'] ][1])?>" /> Pris<br/>
			<input type="text" name="modelnocarbon" value="<?php echo($array[ $_POST['editindex'] ][2])?>" /> Utan kartong<br/>
			<input type="submit" name="addmodel" value="Edit" />
			<!-- ? Add a clear button, so I can add a model after clickng on a Edit field -->
		</form>
	<?php
	}
?>
And the coresponding post functions

Code: Select all

<?php
if($_POST['modelindexdel']) {
	unset($_POST['editindex']); // Unset this so we don't add that to edit fields
	$file = fopen($modelfile,'r');
	$arraystring = fread($file,filesize($modelfile));
	fclose($file);
	$array = unserialize($arraystring); // Get back the array
	$showmodels = true;
	$man_name = $_POST['selman'];
	unset($array[$_POST['delindex']]);
	//unset removes the key, but does not re-number the keys
	// lines below will do this
	$oldarray = $array;
	$array = array();
	foreach($oldarray as $key => $item) {
		$array[] = $item;
	}
	$arraystring = serialize($array);
	$sql = "UPDATE bytin_manufacturer SET manufacturer_models='$arraystring' WHERE manufacturer_name='$_POST[selman]'";
	mysql_query($sql);
}

if($_POST['modelindexedit']) {
	$file = fopen($modelfile,'r');
	$arraystring = fread($file,filesize($modelfile));
	fclose($file);
	$array = unserialize($arraystring); // Get back the array
	$showmodels = true;
	$man_name = $_POST['selman']; // set var to selected manufacturer stored in form
}
?>
Also, it's posibole to create to forms, one for each td that has the submit buttons, but all that does for my current code, it to save me the need to unset($_POST['editindex']) when deleting.
Post Reply