recurring delete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yesugneva
Forum Newbie
Posts: 1
Joined: Tue Sep 02, 2008 3:55 pm

recurring delete

Post by yesugneva »

have a real problem.
i have a php page that deletes all records in a table if the page is loading for the first time-mysql_query("delete from table") and inserts a new record if not. the problem i have is that whenever the page reloads, the records of the table are deleted again. i know the 'if' block that i use to check if the page was loading for the first time is not running again cos theres a print statement in d if block that tells me if the block is executed. i know that a new record is being inserted cos immediately after insertion, i count the number of records in d table and its increased by one but whenever the page reloads, th number of records returns to zero. can anyone help. heres wat d code looks like:

<?php
include('incs/dbcon.inc');


if(!empty($_POST['first'])) $first=$_POST['first'];

print "hasbeen inserted and number in table now is ".mysql_num_rows(mysql_query("select * from real_time"));

//check if this is beginning of test. if it is it gets d subject and test year from the previous page(past-questions-subjects-govt.html)
if($first!="no") {
print "gimmie something to work on man ";
$dq=mysql_query("delete from real_time") or die(mysql_error());


}

print "hasbeen inserted and number in table now is ".mysql_num_rows(mysql_query("select * from real_time"));

mysql_query("insert into real_time values('','$qtn_id','$ans','$model_ans','$qtn_num')") or die(mysql_error());
print "hasbeen inserted and number in table now is ".mysql_num_rows(mysql_query("select * from real_time"));
++$qtn_num;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JAMB Virtual Tutorial College | Test Center</title>

<style type="text/css">
<!--
body {
background-image: url(file:///X|/Projects/Go Virtual Solutions/OCEANIC VLA/images/bg.jpg);
background-repeat: repeat-x;
background-image: url(file:///X|/Projects/Go Virtual Solutions/OCEANIC VLA/images/bg.jpg);
background-image: url(file:///X|/Projects/Go Virtual Solutions/OCEANIC VLA/images/bg.jpg);
background-color: #0C400F;
background-image: url();
}
.style2 {
font-family: Calibri;
font-size: 12px;
}
-->
</style>
<style type="text/css">
<!--
a:link {
color: #3280A4;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #3280A4;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: none;
}
.style49 {color: #FFFFFF}
.style50 {font-size: 13px}
.style51 {color: #A4812F}
.style52 {
font-size: 24px;
font-family: Calibri;
font-weight: bold;
color: #1B143D;
}
.style74 { font-family:Calibri; font-size: 12px; color: #000000; }
-->
</style>

<script language="javascript" type="text/javascript">
var d=new Date();
var monthName= new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var today= monthName[d.getMonth()]+" "+d.getDate()+" "+d.getFullYear();
</script>

<script language="javascript">
function goBack(real_time_id,qtn_num,dept_id,ans)
{
location.assign("testcenter-qtns.php?prev_real_time_id="+real_time_id+"&qtn_num="+qtn_num+"&dept_id="+dept_id+"&ans="+ans+"&rtid=yep");
}
</script>
</head>

<body topmargin="0">
<table width="751" border="0" align="center" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td height="567" colspan="3" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<!--DWLayoutTable-->
<tr>
<td height="134" colspan="3" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="751" height="134" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" background="images/testcenter-banner.jpg">
<!--DWLayoutTable-->
<tr>
<td width="233" height="64">&nbsp;</td>
<td width="114">&nbsp;</td>
<td width="404" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
</tr>
<tr>
<td height="43">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="27">&nbsp;</td>
<td colspan="2" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="91" height="2"></td>
<td width="427"></td>
</tr>
<tr>
<td height="14"></td>
<td valign="top" class="style2"></td>
</tr>
<tr>
<td height="11"></td>
<td></td>
</tr>

</table> </td>
</tr>
</table></td>
</tr>

</table> </td>
</tr>
<tr>
<td height="53" colspan="3" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F0F0F0">
<!--DWLayoutTable-->
<tr>
<td width="542" height="53" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="75"></td>
<td width="170" height="14"></td>
<td width="44"></td>
<td colspan="2" valign="middle"><strong class="style2">Name of Subject:&nbsp;</strong> <strong class="style2 style51"><?php echo $subject;?></strong></td>
<td width="49"></td>
</tr>
<tr>
<td></td>
<td height="7"></td>
<td></td>
<td width="160"></td>
<td width="44"></td>
<td></td>
</tr>


<tr>
<td></td>
<td height="4"></td>
<td></td>
<td rowspan="2" valign="top"><strong class="style2">Duration of Test:</strong> <strong class="style2 style51">20mins</strong></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td height="15" valign="top"><span class="style2 style50"><strong>Date:&nbsp;</strong><strong class="style2 style51"><script language="javascript">document.write(today);</script>
</strong></span></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td height="13"></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>

</table></td>
<td width="208" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="208" height="15" valign="top"><strong class="style2">No. of Questions:</strong> <strong class="style2 style51"><?php echo $numQ;?> </strong></td>
</tr>
<tr>
<td height="6"></td>
</tr>
<tr>
<td height="14" valign="top"><strong class="style2">Total Score:</strong> <strong class="style2 style51"><?php echo $numQ;?> </strong></td>
</tr>
<tr>
<td height="5"></td>
</tr>


</table></td>
</tr>



</table></td>
</tr>
<tr>
<td width="64" height="5"></td>
<td width="629"></td>
<td width="58"></td>
</tr>
<tr>
<td height="29" colspan="3" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="60" height="29" valign="top" class="style52"><div align="center"></div></td>
<td width="689" valign="top" class="style52"><div align="left"><?php echo $subject;?> | <?php echo $test_year;?></div></td>
</tr>


</table></td>
</tr>
<tr>
<td height="29">&nbsp;</td>
<td></td>
<td></td>
</tr>



<tr>
<td height="305">&nbsp;</td>
<td valign="top" class="style74">


</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="17"></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>


</table></td>
</tr>
<tr>
<td width="95" height="14"></td>
<td width="585" valign="top"><div align="center"><span class="style2 style49"> <a href="index.html">Home</a> | <a href="Library.html">Library </a>| <a href="past-questions.html">Past Questions</a> | <a href="past-questions-subjects-govt.html">Past Questions in govt</a> | <a href="tutorial center.html">Tutorial Center </a>| Help </span></div></td>
<td width="71"></td>
</tr>
</table>
</body>
</html>
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: recurring delete

Post by WebbieDave »

Can you repost your code with php tags? It makes the code more readable. Check out the posting guidelines for more info.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: recurring delete

Post by califdon »

Just go back to your post, Edit it and enclose your code between [syntax=php]and[/syntax] tags.
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: recurring delete

Post by starram »

Try storing your count variable in session and implement the check using that. This will give you the desired results.
Post Reply