Jump to content

Can't find my error in this code...


Seaholme

Recommended Posts

Hey all,

Basically I have this code which technically should work, but I have put an error in the logic somewhere and am really struggling to find it, I've been going over it for about an hour now :/

 

The error is that in the second part (// Enter them into the activities database if they're not already there), it won't enter them, and it won't show the word 'Randomevent1' either, so clearly somehow I have put in something to prevent it performing that step.

 

Can anybody help me out? :( I'd be amazingly grateful, I have no clue how I've screwed it up.

 

if(isset($_POST['submit'])){

$ownerid = $_SESSION['id'];

// If completed = Y give an error
$completeyn = "SELECT completed FROM activities WHERE playerno='$ownerid' AND activityno = '1'";
$completecheck=mysql_query($completeyn) or die(mysql_error());
while($row = mysql_fetch_array( $completecheck )) {
if($row['completed'] == 'Y'){ echo 'Oops, you\'ve already done this twice today!';} else {

// Enter them into the activities database if they're not already there
$stepno2 = "SELECT playerno, timesdone FROM activities WHERE playerno='$ownerid' AND activityno = '1'";
$stepnoanswer2=mysql_query($stepno2) or die(mysql_error());
$num_rows2 = mysql_num_rows($stepnoanswer2);
echo $num_rows2;
if($num_rows2 == '0'){ $putintodb2 = mysql_query("INSERT INTO activities (playerno, activityno, timesdone) VALUES ('$ownerid', '1', '1')") or die("Error: ".mysql_error());
echo 'Randomevent1';
}else{

// If they are already there update their stepcount
$updatestepcount2=("UPDATE activities SET timesdone=timesdone+'1' WHERE playerno='$ownerid' AND activityno = '1'");
$newstepcount2=mysql_query($updatestepcount2);
echo 'Randomevent2';}

// If this new stepcount is equal to 2, set completed to Y
$checkstep = "SELECT timesdone FROM activities WHERE playerno='$ownerid' AND activityno = '1'";
$checkstepresult=mysql_query($checkstep) or die(mysql_error());
while($row = mysql_fetch_array( $checkstepresult )) {
if($row['timesdone'] == '2'){
	echo $row['timesdone'];
	$updatestepcount22=("UPDATE activities SET completed = 'Y' WHERE playerno='$ownerid' AND activityno = '1'");
$newstepcount22=mysql_query($updatestepcount22);
}


}}}}

Link to comment
Share on other sites

Sorry to say, but your code is seriously all over the shop.

 

To start with, can you indent it consistently so the rest of us can read it? Then we can probably start finding the issues.

 

I'm sure you don't actually need those while loops for starters, and numbers should not be surrounded by quotes in php (or any programming language really), quotes are for strings.

Link to comment
Share on other sites

Sure, I'll try to indent it a bit if that helps. Out of interest, what would you use instead of while loops to get rows from a table? I only know the one way which is why I use it :X

 

I don't know if this is what you meant, but it now has some indentations in it, hopefully in the right places!

if(isset($_POST['submit'])){

$ownerid = $_SESSION['id'];

// If completed = Y give forth the NO WAY sign
$completeyn = "SELECT completed FROM activities WHERE playerno='$ownerid' AND activityno = '1'";
$completecheck=mysql_query($completeyn) or die(mysql_error());
while($row = mysql_fetch_array( $completecheck )) {
	if($row['completed'] == 'Y'){ echo 'Oops, you\'ve already done this twice today!';} 

	else {

// Enter them into the activities database if they're not already there
$stepno2 = "SELECT playerno, timesdone FROM activities WHERE playerno='$ownerid' AND activityno = '1'";
$stepnoanswer2=mysql_query($stepno2) or die(mysql_error());
$num_rows2 = mysql_num_rows($stepnoanswer2);
	echo $num_rows2;
		if($num_rows2 == '0'){ $putintodb2 = mysql_query("INSERT INTO activities (playerno, activityno, timesdone) VALUES ('$ownerid', '1', '1')") or die("Error: ".mysql_error());
			echo 'Randomevent1';

		}else{

// If they are already there update their stepcount
$updatestepcount2=("UPDATE activities SET timesdone=timesdone+'1' WHERE playerno='$ownerid' AND activityno = '1'");
$newstepcount2=mysql_query($updatestepcount2);
echo 'Randomevent2';}

// If this new stepcount is equal to 2, set completed to Y
$checkstep = "SELECT timesdone FROM activities WHERE playerno='$ownerid' AND activityno = '1'";
$checkstepresult=mysql_query($checkstep) or die(mysql_error());
while($row = mysql_fetch_array( $checkstepresult )) {
	if($row['timesdone'] == '2'){
		echo $row['timesdone'];
			$updatestepcount22=("UPDATE activities SET completed = 'Y' WHERE playerno='$ownerid' AND activityno = '1'");
$newstepcount22=mysql_query($updatestepcount22);
}


}}}}

 

Thanks!

 

EDIT: also thanks for that thing about the numbers, I've changed them all (although not above). Is it just bad practice or does it actually slow the script down to have ' marks around numbers?

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.