I've made a simple survey. Successfully filling and put it into a database table. Now the problem is how to prevent the username complete the survey more than once? Username is taken from the session. I've tried my own code here, but it didn't work :
<?php
session_start();
include_once('config.php');
$username = $_SESSION['username'];
$ask1 = $_POST['ask1'];
$ask2 = $_POST['ask2'];
$ask3 = $_POST['ask3'];
$ask4 = $_POST['ask4'];
$query = mysql_query("insert into survey values('', '$username', '$ask1', '$ask2', '$ask3', '$ask4')");
if ($query) {
// success
echo" <script language='JavaScript'> alert ('Thankyou for your participation');</script>";
echo '<script language="JavaScript"> window.location.href ="home.php";</script>';
} else {
echo "SELECT username FROM survey WHERE username='".$_SESSION['username']."'";
$dup = mysql_query("SELECT username FROM survey WHERE username='".$_SESSION['username']."'");
if(mysql_num_rows($dup) >0){
echo"<script language='JavaScript'> alert ('You've filled out the survey!');</script>";
echo '<script language="JavaScript"> window.location.href ="home.php" </script>';
} else {
// fail
echo" <script language= 'JavaScript'> alert ('Failed to complete the survey'); </script>";
echo '<script language="JavaScript"> window.location.href ="survey.php";</script>';
}}
?>
so what is the best solution for the problem like this? Thankyou in advance for you who want to help me. Thankyou once again.