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>";
?>
[changed 'code' tags to 'php' tags -- MOD]