Jump to content

Recommended Posts

Hello, I having a problem with losing data in an array variable. The problem is that after I submit a query to a DB I fill an array with some values. This works fine, but after I do another submit with a different button, the values in the array seem to disappear. Any help will be appreciated. I placed comments in the code. I running php 5.2.5. Thanks.

 

<?php
/*****************8 Connect to database ******************/
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("pluto_media") or die(mysql_error());

/****************** Set Title ****************************/
Echo "<html>";
Echo "<title>HTML with PHP</title>";

/***************** Variables ****************************/
$i = 0;
$querStr = $_POST['querStr'];
$mediaSubTypeEnum = array (1 => "TV Shows",2 => "Movies",3 => "Home Videos", 4 => "Sports Events", 5 => "Music Videos", 6 => "Alternative", 7 => "Popular Music", 8 => "Classical Music");
$fileFormatEnum = array (1 => "Low Res",2 => "DVD",3 => "Standard Def", 4 => "HD 720", 5 => "HD 1080", 6 => "Low Quality", 7 => "MP3", 8 => "CD Quality", 9 => "High-def audio");


/******************* Create main body of html page **********************************************/
Echo  "
<body bgcolor='Green'>
<form action=".$_SERVER['PHP_SELF']." method='post'>
Search For:<input name='querStr' type='text' size='10'/>
<input type='submit' name='submit' value='submit'>";



/**************** perform action after button is clicked ***************************************/
if(isset($_POST['submit'])) {	//If the initial button displayed is clicked
$result = mysql_query("SELECT * FROM File Where Filename LIKE '%".$querStr."%'");	
Echo "	<input type='submit' name='submit2' value='submit2'> // output results of query in html table - this works fine
<table border='1'>
<tr>
	<th>Primary File</th>
	<th>Update File</th>
	<th>Filename</th>
	<th>Path</th>
	<th>MediaSubType</th>
	<th>FileFormat</th>
	<th>PK_File</th>
</tr>";		    
while($row = mysql_fetch_array($result)) {
	echo "<tr>";
		echo "<td><input type='radio' name='primary[]' value=$i/></td>";
		echo "<td><input type='checkbox' name='CopyFile[]' value='$i'/></td>";
		echo "<td>".$row['Filename']."</td>";
		echo "<td>".$row['Path']."</td>";			
		echo "<td>".$mediaSubTypeEnum[$row['FK_MediaSubType']]."</td>";
		echo "<td>".$fileFormatEnum[$row['FK_FileFormat']]."</td>";                               
		$myArray[$i++] = $row['PK_File'];
		echo "<td>".$myArray[$i-1]." -".$i."</td>";
	echo "</tr>";
}
echo "</table>";	
}


/**************** perform action after second button is clicked ***************************************/
if(isset($_POST['submit2'])) {
foreach($_POST['primary'] as $value) { // determine which radio button is selected
	$value = $value + 1;
	echo "the primary is".$value."\n";
}
               
for ($i = 0; $i < 10; $i++) { 
/* simple test to see if anything is in myArray. the array appears to be empty. This is the problem area. Before the second button was clicked
the array had verified values in it.*/
	echo $_REQUEST["myArray[$i]"];
	echo " in loop".$i;
}

$result = mysql_query("SELECT * FROM Picture_File Where FK_File = '".$myArray[$value]."'");		
$row = mysql_fetch_array($result);
$FK_Picture = $row['FK_Picture'];
$priVal = $value;	
echo "prival and fk_pic = ".$priVal. " - ".$FK_Picture."\n";
foreach($_POST['CopyFile'] as $value) {
	$value = $value + 1;
	mysql_query("INSERT INTO `Picture_File` VALUES ($FK_Picture,$myArray[$value],NULL,NULL,NULL,0,NULL,NULL)");
	mysql_query("UPDATE File SET mediaSubTypeEnum='.mediaSubTypeEnum[$priVal].' fileFormatEnum='.$fileFormatEnum[$priVal].' WHERE FK_File='.$myArray[$value].'");
}
}

Echo "	</form>
   </body>
</html>";
?>

Link to comment
https://forums.phpfreaks.com/topic/86832-losing-data-in-array-after-button-submit/
Share on other sites

* Save the array in a session.

* Save the array in a cookie

* Save the array itself in a db (seperate table) and retrieve it with the ip.

 

The problem is when you submit it completely erases all memory that was on the page.  If you submit something ti's submittted.  If you submit something else it submits something else (not naturally maintaining what was originally submitted).

Thus another problem that requires saving state.  As explained above.

Thanks for the reply businessman332211:

 

I tried to save the array as a session variable and I also tried to serialize it - separate attempts.

 

I attempted to create the session variable by adding session_start(); to top of page. Then I did $_SESSION["myArray[$i++]] = $val;

 

I'm sure that I just don't understand the syntax. Can you tell me how to do a session variable for an array?

 

Thanks

 

lon22

$_SESSION = array(whatever in the array);

then you can access it the same as any array.

$_SESSION['arraykeyhere'];

So basically save the entire array into an array.

Then merge that array into session like.

$myarray = array();

then fill my array with whatever you want and then

$_SESSION = $myarray;

That is one method.  The other is to use $_SESSION['subkey'] as your array.  (makes it cleaner).

$_SESSION['my_array'] will be your top level array.  Then put your other keys and values underneath the

my_array key. This is so if you needed more than 1 seperate array in session you can do

$_SESSION['my_array']

$_SESSION['user_data']

$_SESSION['system_data']

and each one can be a seperate array or a multi array.

Thanks for the replies. I decided to go with the hidden field.

 

To save data:

$saveStr = implode(",",$myArray);

echo $saveStr;

echo "<input type='hidden' id='arry' name='myArray' value='".$saveStr."'/>";

 

To retrieve data:

$saveStr  = $_POST['myArray'];

echo $saveStr;

$myArray = explode(",",$saveStr);

 

Worked like a charm resago.

 

Glad you got it working. When working with more complex data (ie binary data or non-standard characters), serialize the array and make it a hidden form element. Then unserialize it and you'll have the original array in its perfect form.

 

PS You may want to base64 the serialized data to make sure nothing is lost in the POST operation.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.