Jump to content

php/mysql help


sherm

Recommended Posts

I'm fairly new to php/mysql coding and I have a quick question:

 

I'm making a simple survey system.  When the user starts the survey their username is added to my MySQL database.  Also, the text "incomplete" is added to another. 

 

When the user finishes the survey, "incomplete" is updated to "complete".  I have all this working perfectly...

 

However, I cannot figure out how to keep the user from taking the survey again.  If their username has "incomplete" as its status, they should be able to retake the survey... but if it's "complete" the survey should echo back an error message.

 

So how do I code it so the script first checks their username and then the completion status in the same row?  And if the status is complete, it bounces back an error?

 

Thanks in advance

Link to comment
Share on other sites

here's my code so far:

 

<?php

$username="*****";
$password="*****";
$database="l*****";
$db="*****";

$name = $_POST['name'];
$status = $_POST['status'];

$connection = mysql_connect($database,$username,$password);
mysql_select_db($db, $connection) or die( "Unable to select database");


$searchtable = "SELECT * FROM resident_name WHERE name='$name'";

$query = "INSERT INTO resident_name (name, status) VALUES ('".$name."', '".$status."')";


if($cmd=="update")
{
    mysql_query("UPDATE resident_name SET status='complete' WHERE name='$name'", $connection);
}


$result = mysql_query($searchtable) or die(mysql_error());
mysql_query($query) or die( "You have already taken the survey");


mysql_close();


?>

 

So should I do the comparison via the $result variable I am pulling from the mysql_query?  I'm not sure where I need to compare

Link to comment
Share on other sites

here's my code so far:

 

<?php

$username="*****";
$password="*****";
$database="l*****";
$db="*****";

$name = $_POST['name'];
$status = $_POST['status'];

$connection = mysql_connect($database,$username,$password);
mysql_select_db($db, $connection) or die( "Unable to select database");


$searchtable = "SELECT * FROM resident_name WHERE name='$name'";

$query = "INSERT INTO resident_name (name, status) VALUES ('".$name."', '".$status."')";


if($cmd=="update")
{
   mysql_query("UPDATE resident_name SET status='complete' WHERE name='$name'", $connection);
}


$result = mysql_query($searchtable) or die(mysql_error());
mysql_query($query) or die( "You have already taken the survey");


mysql_close();


?>

 

So should I do the comparison via the $result variable I am pulling from the mysql_query? I'm not sure where I need to compare

 

first pull out the status of the select

while row etc.

$status = $row[status];

 

if ($status == "complete" || $status != "")

 

continue else ...

 

like so..

Link to comment
Share on other sites

I changed my code to this:

 

<?php

$username="*******";
$password="**********";
$database="*********";
$db="********_sl";

$name = $_POST['name'];
$status = $_POST['status'];

$connection = mysql_connect($database,$username,$password);
mysql_select_db($db, $connection) or die( "Unable to select database");


$searchtable = "SELECT * FROM resident_name WHERE name='$name'";

$query = "INSERT INTO resident_name (name, status) VALUES ('".$name."', '".$status."')";


if($cmd=="update")
{
    mysql_query("UPDATE resident_name SET status='complete' WHERE name='$name'", $connection);
}


$result = mysql_query($searchtable) or die(mysql_error());
mysql_query($query) or die( "You have already taken the survey");


while($row=mysql_fetch_array($result)) 
  
   { 
      $status = $row["status"];

      if ($status == "complete" || $status != "")
  {
  		echo "survey complete";
  }
    }


mysql_close();


?>

 

 

More specifically, I added this:

while($row=mysql_fetch_array($result)) 
  
   { 
      $status = $row["status"];

      if ($status == "complete" || $status != "")
  {
  		echo "survey complete";
  }
    }

 

But the code is returning the die message of "You have already taken the survey"  instead of the "survey complete" echo.  The "you have already taken this survey" is echoed simply because their username already exists in the database.  I only want the error to show up if they exist AND they have completed the survey.

Link to comment
Share on other sites

i think i would rewrite it like so:

in a nutshell

<?php

$connection = mysql_connect($database,$username,$password);
mysql_select_db($db, $connection) or die( "Unable to select database");

//first check if user exists
$searchtable = "SELECT user, status FROM resident_name WHERE name='$name'";
$result         = mysql_query($searchtable);

if ($result) { //user exists and has probably status
while row etc...

$user = $row[]
$status = $row[]

  if $status == "complete { exit } else {
  update

} else {

insert
}
mysql_close();


?>

 

Link to comment
Share on other sites

  • 2 weeks later...

ok sorry i took so long to respond...

 

i updated my code to the following:

 


<?php
$username="*****";
$password="********";
$database="**********";
$db="*****";

$name = $_POST['name'];
$status = $_POST['status'];

$connection = mysql_connect($database,$username,$password);
mysql_select_db($db, $connection) or die( "Unable to select database");

//first check if user exists
$searchtable = "SELECT name, status FROM resident_name WHERE name='$name'";
$result = mysql_query($searchtable);

if ($result) { //user exists and has probably status

while($row=mysql_fetch_array($result)) 
  
   	{ 
     	$name = $row[]
	$status = $row[]

      if $status == "complete" 
  	{ 
		die( "Exit error") 
	} 
	else 
	{
  			mysql_query("UPDATE resident_name SET status='complete' WHERE name='$name'", $connection);
    	}
} 
}
else {
mysql_query("INSERT INTO resident_name (name, status) VALUES ('".$name."', '".$status."')", $connection);
}
mysql_close();


?>

 

it doesn't seem to work as you suggested.  the user's name is no longer put into the database if it is not already there.  i'm guessing the "if ($result) { }" part is not returning their name...  any reasons why or how this should be fixed?

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.