Amit20 Posted September 25, 2011 Share Posted September 25, 2011 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"> © 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 Quote Link to comment https://forums.phpfreaks.com/topic/247843-how-to-avoid-redundant-data/ Share on other sites More sharing options...
Xpluration Posted September 25, 2011 Share Posted September 25, 2011 You should convert your form into a POST instead of GET. Quote Link to comment https://forums.phpfreaks.com/topic/247843-how-to-avoid-redundant-data/#findComment-1272728 Share on other sites More sharing options...
jcbones Posted September 26, 2011 Share Posted September 26, 2011 After form processing, re-direct to a clean URL. Quote Link to comment https://forums.phpfreaks.com/topic/247843-how-to-avoid-redundant-data/#findComment-1272739 Share on other sites More sharing options...
codeprada Posted September 26, 2011 Share Posted September 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247843-how-to-avoid-redundant-data/#findComment-1272779 Share on other sites More sharing options...
Amit20 Posted September 26, 2011 Author Share Posted September 26, 2011 Thank you Xpluration for your reply, but the thing is i m not sending form data, instead; i m sending a javascript array. And i guess i can't send a JS array through post because that array is created by JS function. Quote Link to comment https://forums.phpfreaks.com/topic/247843-how-to-avoid-redundant-data/#findComment-1272799 Share on other sites More sharing options...
Amit20 Posted September 26, 2011 Author Share Posted September 26, 2011 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???? Quote Link to comment https://forums.phpfreaks.com/topic/247843-how-to-avoid-redundant-data/#findComment-1272946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.