MySql:Last record inserted twice

Hello All,

I am using here PHP and Smarty Template.
I have written a script where evrything is fine but last inserted records inserted twice.
Where data is coming from TPL correctly and loop is also moving correctly.

For eg. here 2 question is there the last question’s answer for respective user is entering twice.
My Mysql table structure


CREATE TABLE `tbl_question_answer` (
  `ans_id` bigint(20) NOT NULL auto_increment,
  `Lq_id` bigint(20) NOT NULL,
  `lecture_id` bigint(20) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `answer` varchar(255) NOT NULL,
  `reg_date` datetime NOT NULL,
  PRIMARY KEY  (`ans_id`)
)

my TPL Code is as folows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>{$Title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="./css/style.css" type="text/css" rel="stylesheet">
{literal}
<script language="javascript">
function validateAllFields(objForm)
	{		
		      var count=document.frm.hdnCount.value;   
        	  var data="";
			  var checkStatus;
			   for(var i=1; i<=count; i++)     
    				{
					checkStatus=false;
					var pos1='txtAnswer_'+i;					
					var pos='hdnQuestionID_'+i;
					var strQuest=document.getElementById(pos);
					
					if(eval('document.frm.'+pos1+'.value')!="")
						{
						data=data+"~"+strQuest.value+" $"+eval('document.frm.'+pos1+'.value');
						checkStatus=true;
						}
					
						if(!checkStatus)
						{
						alert("Please choose anyone from this question");
						document.getElementById(pos1).focus();
						return false;
                        }
					}					
					if(data!="")
					{
					data=data.substr(1);
					}
					alert("Data are :"+data);			
		 document.frm.hdnQuestionAnswerData.value=data;      
         document.frm.mode.value='WARMUP';     
           return true;
		
		}
function limitlength(obj, length)
{
var maxlength=length
if (obj.value.length>maxlength)
obj.value=obj.value.substring(0, maxlength);
return true;
}

</script>
			 {/literal}
</head>
<body>
<form action="{$PHP_SELF}" method="post" name="frm" enctype="multipart/form-data" onSubmit="return validateAllFields(this);">
<input type="hidden" name="mode" value="">
<input type="hidden" name="hdnLectureID" value="{$lectureID}">
<input type="hidden" name="hdnQuestionAnswerData" value="">
<table cellpadding="3" cellspacing="5" width="100%" align="left" bordercolor="#003333" border="1">
<tr><td align="center" class="header_ermsg">{$msg}</td></tr>
<tr>
      <td align="left" class="heading1">
	    <table cellpadding="2" cellspacing="2" id="tblWarmUp">
{assign var="id"  value="0"}
{section name=client loop=$data}
{assign var="id" value=$id+1}
		      <tr>
                <td valign="top">{$id}</td>
                <td valign="top">{$data[client].Question}</td>
                <td valign="top">
				    <table width="100%" >
		<tr><td>
		<input type="hidden" id="hdnQuestionID_{$id}" name="hdnQuestionID_{$id}" value="{$data[client].LQ_id}" />
		<textarea id="txtAnswer_{$id}" name="txtAnswer_{$id}"  rows='5' cols='50' onkeyup="return limitlength(this, 500);" ></textarea></td><td></td></tr>
		                    </table>
                </td>
            </tr>
{/section}
		</table>
		<input type="hidden" id="hdnCount" name="hdnCount" value="{$id}"></td>
		</tr>
		<tr><td align="center" >{if $tag eq 0}<input class="Butt"  type="submit" name="sub" value=" Add ">{/if}</td></tr>
</table>
</form>
</body></html>

From where data is coming perfectly.

PHP code is as follows:


if(isset($_POST['mode']) && $_POST['mode']=="WARMUP")
{
$dataArray = explode('~', $_POST['hdnQuestionAnswerData']); 
 
    for($cnt=0;$cnt<count($dataArray);$cnt++)
   {  
   if($dataArray[$cnt]!="")
	   {
		$smallarray=explode('$', $dataArray[$cnt]); 
		$sqlC="select count(1) from  tbl_question_answer where LQ_id='".$smallarray[0]."' and lecture_id='".$_POST['hdnLectureID']."' and user_id='".$smallarray[1]."'";
		$QU=$db->select_data($sqlC);
		$res=$db->get_row($QU);
		echo "Result Set :".$res[0];
		if($res[0]=="0")
		   {	
		[I][B]$sql="insert into tbl_question_answer (LQ_id,lecture_id,user_id,answer,reg_date)";
		$sql.=" values('".$smallarray[0]."','".$_POST['hdnLectureID']."','".$_SESSION['UID']."','".$smallarray[1]."',NOW())";[/B][/I]
			  		
		  [B] $db->insert_sql($sql);[/B]
		 	echo 'Sql '.$cnt.' is :'.$sql."</BR>";
			}
	   }
   }
   $smarty->assign("tag","1");
   $smarty->assign("msg","Data Saved successfully");
}

Printing data aslo shown that data looping is right but in tables last record is twice.