Jump to content

How to avoid redundant data


Amit20

Recommended Posts

Hello Everyone,

 

I m making an attendance application.

 

I have an issue with following code,

 

<?php 
session_start();
require_once("conf.php");
mysql_select_db('site');
$subjects=$_GET['sublen'];
$username=$_SESSION['user'];
for($i=1;$i<=$subjects;$i++)
{
$subject=$_GET[',Sub'.$i];
$query="INSERT INTO Subjects(id,Subjects,User,Classs) VALUES($i,'$subject','$username','F.Y.B.Sc')";
$true=mysql_query($query) or die("Cannot Store subjects".mysql_error());

}

?>

<html>
<head>
	<link href="../css/style.css" type="text/css"  rel="stylesheet" />
	<script type="text/javascript" src="../js/jquery.js"></script>
</head>
	<body>
		<div id="main">
				<div id="logo">
					KiMi...
				</div>
					<div id="header">
						<ul>
							<li><a href="index.html">Home</a></li>
							<li><a href="php/subjectsTable.php">Create Subjects</a></li>
							<li><a href="html/form.html">Contact Us</a></li>
							<li><a href="html/register.php">Register!</a></li>
						</ul>
					</div>
					<div id="nav">

					</div>
						<div id="content">
							<?php 
								if($true)
								{
								echo "<div id='dynamicDiv'>";
								echo "<div> Subjects stored Successfully!</div></br></br>";
								$query="SELECT * FROM Subjects Where User='$username'";
								$result=mysql_query($query) or die("Cannot Fetch Data");
								echo "<table id='table'>";
								echo "<tr><td> </td><td>Subjects</td><td>Class</td></tr>";
								while($row=mysql_fetch_array($result))
									{
										echo "<tr><td><input type='checkbox' name='box[]'/> </td>";
										echo "<td>";
										echo $row['Subjects'];
										echo "</td>";
										echo "<td>";
										echo $row['Classs'];
										echo "</td>";
										echo "</tr>";
									}
									echo "</table>";
									echo "</div>";
								}
							?>

					</div>
					<div id="footer">
						&#169; Copyright Amit K. Goda 2011-2012
					</div>
		</div>
	</body>
</html> 

 

In the above code i m storing data into database which has been passed into $_GET array.

The Code is this

 

<?php

session_start();

require_once("conf.php");

mysql_select_db('site');

$subjects=$_GET['sublen'];

$username=$_SESSION['user'];

for($i=1;$i<=$subjects;$i++)

{

$subject=$_GET[',Sub'.$i];

$query="INSERT INTO Subjects(id,Subjects,User,Classs) VALUES($i,'$subject','$username','F.Y.B.Sc')";

$true=mysql_query($query) or die("Cannot Store subjects".mysql_error());

 

}

 

?>

the data is stored

but the issue is, since i have data coming in $_GET array when i reload my page the same data is stored again inmy database.

 

So any suggesstions how can i avoid that???

 

Any Help will be highly appreciated :D

Link to comment
Share on other sites

Using POST will still repost the data. You need to create a random key that will be assigned to each form and is rendered invalid when the form's data has been processed.

 

You can create a random key simply from

hash_hmac('sha1', time(), $_SERVER['REMOTE_ADDR'])

 

The value returned from hash_hmac would be stored in a session variable and also placed in the hidden input element within the form. When the form is submitted check the value of the hidden input element against that of the session variable. If they match then you'll process the form and then unset the session variable. Unsetting it will cause the hidden input element's value to be invalid.

N.B. Don't process the form is the hidden input element's value does not exist.

Link to comment
Share on other sites

Thank You codeprada for your reply.

 

As i said in my last reply that the data i m sending is a JS array.

 

Here is the code,

 

<script type="text/javascript">

	function getNoOfSub()
	{	
		var subj=document.getElementById('subjects').value;
		return subj;
	}

	function getSubjects()
	{
		var totalSubjects=getNoOfSub();
		var subjectArray = new Array();
		for(var i=1;i<=totalSubjects;i++)
		{
			var s=document.getElementById('subject'+i).value;
			subjectArray.push("Sub"+i+"="+s+"&");
		}

		window.location="storeSubjects.php?&,"+subjectArray+"&sublen="+subjectArray.length;
	}
</script>

 

So any suggestions how can i manage this????

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.