Page 2 of 2

Posted: Mon May 08, 2006 7:57 am
by finny
Thanks for all your help... fingers crossed, it sounds 'doable' (is that a word or is that another argument :wink: )

I'll be sure to post up some download codes here if I get it going...

Thanks again.
finny

Posted: Mon May 08, 2006 8:57 am
by a94060
thanks. hopefully we were a help.

Posted: Wed May 31, 2006 6:48 am
by finny
Hi again, I need some more help...
I have created the random codes, populated the DB with the codes (3 cols... code, used, & location). Below is the code I have come up with so far (try not to laugh too hard... remember I'm a newbie) but I'm getting errors and I know its really simple things I am doing wrong... but thats what I get for opening the book in the middle! I think where I am really going wrong is my if statement where I am checking to see if the code exists in the DB. I'm also not sure how to append the file location from the database to start the download.

Anyway, here is the code... any help would be great.

Code: Select all

<?php

/* Connect to the Database */
$host="localhost";
$user="username";
$password="password";

$database = "db_name";

$connection = mysql_connect($host,$user,$password)
	or die ("Couldnt connect to server.");

$db = mysql_select_db("$database", $connection)
     or   die ("Could not connect to database");


/* Pass form input to variable */
$userEntry = $_post

Code: Select all

;


/* See if code exists in Database and pass it to variable $code */
$findCode = "SELECT code from Download
		WHERE code='$userEntry'";
$code = mysql_query($findCode)
     or   die ("Could not execute query.");


/* If Code is 'blank' echo invalid code, else, get the row and check if used = n */
if ($code == "")
{
	echo "You have entered an invalid Code. Please click 'Back' to try again.";
}
else
{
   	$getRow = mysql_fetch_array($code,mysql_assoc);
	if ( $getRow['used'] == "n" )
  	{ echo "Append 'location' to current dir and start download";
      $yesNo = "UPDATE download SET used=y WHERE code='$userEntry'";
      $used = mysql_query($yesNo)
      or die ("Couldnt execute Yes/No query"); }
  	
	else
	{ echo "This code has already been used. If you have received this message in error please contact info@email.com for help.";}
}

?>

Posted: Wed May 31, 2006 8:49 am
by finny
This is the error I get when I run the program...

Code: Select all

Warning: mysql_fetch_array(): The result type should be either MYSQL_NUM, MYSQL_ASSOC or MYSQL_BOTH. in /home/webfolder/public_html/download_folder/download.php on line 35
This code has already been used. If you have received this message in error please contact info@email.com for help.

Posted: Wed May 31, 2006 9:11 am
by feyd
constants, like variables, are case sensitive.

...

Posted: Wed May 31, 2006 9:22 am
by ok
Remember to add a ' in your update query:
UPDATE download SET used='y' WHERE code='$userEntry'!

Posted: Wed May 31, 2006 9:41 am
by finny
Thanks

Now I just get this whether I input a valid or invalid code into the form...

Code: Select all

This code has already been used. If you have received this message in error please contact info@email.com for help.
Any ideas?

Here is the code as it looks now...

Code: Select all

<?php

/* Connect to the Database */
$host="localhost";
$user="db_user";
$password="password";

$database = "db_name";

$connection = mysql_connect($host,$user,$password)
	or die ("Couldnt connect to server.");

$db = mysql_select_db("$database", $connection)
     or   die ("Could not connect to database");


/* Pass form input to variable */
$userEntry = $_post

Code: Select all

;


/* See if code exists in Database and pass it to variable $code */
$findCode = "SELECT code from Download
		WHERE code='$userEntry'";
$code = mysql_query($findCode)
     or   die ("Could not execute query.");


/* If Code is 'blank' echo invalid code, else, get the row and check if used = n */
if ($code == "")
{
	echo "You have entered an invalid Code. Please click 'Back' to try again.";
}
else
{
   	$getRow = mysql_fetch_array($code,MYSQL_ASSOC);
	if ( $getRow['used'] == "n" )
  	{ echo "Append 'location' to current dir and start download";
      $yesNo = "UPDATE Download SET used='y' WHERE code='$userEntry'";
      $used = mysql_query($yesNo)
      or die ("Couldnt execute Yes/No query");
    }
  	
	else
	{ echo "This code has already been used. If you have received this message in error please contact info@email.com for help.";
    }
}

?>

Posted: Wed May 31, 2006 9:47 am
by ok
Replace "$userEntry = $_post

Code: Select all

; " with "$userEntry = $_post[[color=red]'[/color]code[color=red]'[/color]];"

Posted: Wed May 31, 2006 9:54 am
by feyd
*cough* variable case; $getRow

Posted: Wed May 31, 2006 10:03 am
by ok
Pay attention to the little things:

Code: Select all

<?php
//...
/* See if code exists in Database and pass it to variable $code */
$findCode = "SELECT `used`, `code` from Download WHERE `code`='$userEntry'";
$code = mysql_query($findCode) or   die ("Could not execute query.");


/* If Code is 'blank' echo invalid code, else, get the row and check if used = n */
if (!$code)
{
        echo "You have entered an invalid Code. Please click 'Back' to try again.";
}
else
{
        $getRow = mysql_fetch_array($code,MYSQL_ASSOC);
        if ( $getRow['used'] == "n" )
        { 
             echo "Append 'location' to current dir and start download";
             $yesNo = "UPDATE Download SET `used`='y' WHERE `code`='$userEntry'";
             $used = mysql_query($yesNo) or die ("Couldnt execute Yes/No query");
       }
       
        else
        { echo "This code has already been used. If you have received this message in error please contact info@email.com for help.";
    }
}

Posted: Wed May 31, 2006 11:26 am
by finny
Thanks for your help, this seems to work except if a code which does not exist in the database is entered it still returns "this code has already been used" instead of "Invalid code"...

Posted: Wed May 31, 2006 11:46 am
by feyd
"invalid code" will only happen if the query fails. An empty result set is not a failure. mysql_num_rows() may be of interest.

Posted: Wed May 31, 2006 12:19 pm
by finny
Thanks alot guys. Just have to get the download to start and I'm all set. Just in time too... tour starts tomorrow. 8)

Thanks for all your help.

finny