Form loses data on re-submit

I have a form where the user chooses an item from the dropdown(Term) which then displays data(Rate) from the database. The user should be able to choose different terms to get the results however when you hit submit it works the first two times and then loses the data the third time. Also, when you first enter the page it has a specific Term(cid=1) that should be displayed. Does anyone know why it is losing the data?


<?php
$calledforthefirsttime = true;
?>
<?php
if ($calledforthefirsttime == true)
{
	
$id = $_GET[id'];
$sql = "SELECT * FROM table1 ORDER BY Display";
$result = mysql_query($sql, $db)
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="cid" id="cid">

<?php
          while ($row=mysql_fetch_array($result))
{

if ($cid == $row[cid])
{
echo "<option value=\\"$row[cid]\\" selected>$row[Term]\
";
}
else
{
echo "<option value=\\"$row[cid]\\">$row[Term]";
}
}
echo "</select>
				
               </select> <input type=hidden name=id value=$id> <input type='submit' name='button' id='button' value='Submit'> ";
			
     ?>


</form>

 <?php
$cid = 1;

 $sql3 = "SELECT * FROM table2 WHERE cid = '$cid' AND id='$id'";
$result3 = mysql_query($sql3, $db);
$row3 = mysql_fetch_assoc($result3);
$cid = $_POST['cid'];
$id = $_POST['id'];

 echo "Pricing 1: $row3[rate]";
}

 ?>
<?php

if ((isset($_POST['cid'])) && ($cid!=""))
{
$cid = $_POST['cid'];

$calledforthefirsttime = false;
$id = $_POST['id'];
	
$sqlp="SELECT * FROM table2 WHERE cid = '$cid' AND id='$id'";
$resultp = mysql_query($sqlp, $db);

while ($rowp = mysql_fetch_array ($resultp))
{

echo "Pricing: $rowp[rate]";
}
		
}

?>

Firstly there are several syntax errors in your script that you need to address

Why are you setting $calledforthefirsttime then checking it for the first routine, it will always be true and therefore always run !

Im not 100% sure how this is supposed to run, I cant work out when pricing 1 should show and when pricing 2 should show