Multiple form mysql query and pages numbering

Hello everyone,

I’m not not an expert in php and this is why I write on this forum.

I have a page with two search form (one per category, the other by age group) and I display my results from a query with pagination.

All good until I move from one page to another: as soon as I change the page I lose my list of results and my page does not show anything other than my form.

I did a print_r of my sessions and the launch of my page I have:

Array()

Once my form send I got for exemple:

Array ( [categorie] => 4 )

But when I go to another page of my result, I go back to:

Array()

Can you help me or at least tell me how to fix the problem?

Par Age <img src="../images/check_mark.png" width="50" onclick="MM_changeProp('age','','display','block','DIV');MM_changeProp('cat','','display','none','DIV')"/>
Par catégorie <img src="../images/check_mark.png" width="50" onclick="MM_changeProp('age','','display','none','DIV');MM_changeProp('cat','','display','block','DIV')" />
<div id="age" style="display:none"><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validateForm()">
<select name="age" id="age">
<option name="ageid" value="1">1</option>
<option name="ageid" value="2">2</option>
<option name="ageid" value="3">3</option>
<option name="ageid" value="4">4</option>
<option name="ageid" value="5">5</option>
<option name="ageid" value="6">6</option>
</select>
<input name="" type="submit" />

</form></div>
<div id="cat" style="display:none">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validateForm()">
<select name="categorie">
<?php


$fp = fopen($_SERVER["DOCUMENT_ROOT"]."/frimousse/Categorie.csv", 'r');
// Headrow
$head = fgetcsv($fp, 4096, ';', '"');

// Rows
while($column = fgetcsv($fp, 4096, ';', '"'))
{
    $column = array_combine($head, $column);
	
?>

<option name="categorieNom" value="<?php echo $column['IdCategorie']?>"><?php echo $column['Nom']?></option>
<?php
 }

?>
</select>
<input name="" type="submit" />
</form>
</div>
<?php
session_start ();
if (isset($_POST['categorie']) ) { $_SESSION['categorie'] = $_POST['categorie']; }
if (isset($_POST['age']) ) { $_SESSION['age'] = $_POST['age']; }
include('config.php');
    include ('pagination.php');
    $per_page = 6;
if (isset($_SESSION['categorie'])) {
    $result = mysql_query("SELECT * FROM csvdata where IdCategorie={$_SESSION['categorie']}");
	}
if (isset($_SESSION['age'])) {
    $result = mysql_query("SELECT * FROM csvdata where AgeVise={$_SESSION['age']}");
	}

    $total_results = mysql_num_rows($result);
    $total_pages = ceil($total_results / $per_page);
    if (isset($_GET['page'])) {
    $show_page = $_GET['page']; //current page
    if ($show_page > 0 && $show_page <= $total_pages) {
    $start = ($show_page - 0) * $per_page;
    $end = $start + $per_page;
    } else {
    $start = 0;
    $end = $per_page;
    }
    } else {
    $start = 0;
    $end = $per_page;
    }
    // display pagination
    $page = intval($_GET['page']);
    $tpages=$total_pages;
    if ($page <= 0)
    $page = 1;
      $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;


for ($i = $start; $i < $end; $i++) {

if ($i == $total_results) {
break;
}
if (mysql_result($result, $i, 'IdCategorie') == "1") {
    $cat = "Jeu de motricité";
} elseif (mysql_result($result, $i, 'IdCategorie') == "2") {
    $cat = "Jeu d'habileté/rapidité";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "3") {
    $cat = "Jeu sensoriel";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "4") {
    $cat = "Jeu de rôles";
	} elseif (mysql_result($result, $i, 'IdCategorie') =="5") {
    $cat = "Jeu de hasard";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "6") {
    $cat = "Jeu de construction/assemblage";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "7") {
    $cat = "Jeu de créativité/artistique";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "8") {
    $cat = "Jeu de stratégie/réflexion";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "9") {
    $cat = "Jeu de vocabulaire";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "10") {
    $cat = "Jeu mathématique";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "11") {
    $cat = "Jeu d'apprentissage/connaissance";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "12") {
    $cat = "Jeu de mémoire";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "13") {
    $cat = "Jeu de plein air";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "14") {
    $cat = "Livre avec dessins seulement";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "15") {
    $cat = "Livre avec textes et dessins";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "16") {
    $cat = "Livre avec textes seulement";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "17") {
    $cat = "Livre de bricolages";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "18") {
    $cat = "Livre destiné aux parents";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "19") {
    $cat = "Cassette vidéo - Dessins animés";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "20") {
    $cat = "Cassette vidéo - Film";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "21") {
    $cat = "Cassette audio";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "22") {
    $cat = "CD-Rom audio";
	} elseif (mysql_result($result, $i, 'IdCategorie') == "23") {
    $cat = "CD-Rom informatique";
} else {

}
echo "<div style='float:left;width:150px;padding-left:10px;padding-right:10px;height:200px;padding-bottom:50px;vertical-align:top'>";
$IdPhoto = mysql_result($result, $i, 'IdPhoto');

if (empty($IdPhoto))
{
	?>
 <div style="margin-top:5px;height:100px"><img src="<?php $_SERVER['DOCUMENT_ROOT'];?>/frimousse/dataimg/not-found.jpg" width="120px"></div><br />
 <?php
} else {
?>
<div style="margin-top:5px;height:100px"><img src="<?php $_SERVER['DOCUMENT_ROOT'];?>/frimousse/dataimg/i<? echo $IdPhoto ?>.jpg" width="120px" style="padding-bottom:10px"></div><br />

<?php
 }
 $Titre = utf8_encode(mysql_result($result, $i, 'Titre'));
// echo out the contents of each row into a table
echo '<div style="margin-top:5px;height:35px">' . $Titre . '</div><br />';
echo '<div style="margin-top:-15px;height:20px">' . $cat . '</div><br />';
echo '<div style="margin-top:5px">' . mysql_result($result, $i, 'AgeVise') . '</div><br /></div>';
}
// close table>
echo "<div style='clear:both'></div>";
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// pagination

    ?>

Thank you very much.

session_start () should be session_start()

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

session_start(); also needs to be called before ANYTHING is sent to browser.

Really it would be a good idea to do all php logic and queries before any html output, but session_start(); has to be.