Russia Posted May 20, 2010 Share Posted May 20, 2010 I currently have this code: <?php mysql_connect("localhost", "brian_keys", "******") or die(mysql_error()); mysql_select_db("brian_keys") or die(mysql_error()) if (isset ($_POST['submit'])) // if the form was submitted, display their name { $key = addslashes(htmlentities($_POST['key'])); if ($key == "") { die("No key found"); } $query = "SELECT * FROM keys WHERE key='".$key."'"; $result = mysql_query($query) or die("Could not execute query"); if (mysql_num_rows($result) > 0) { echo "Key valid"; // Setting the key activated $query = "UPDATE keys SET activation='yes' WHERE key='".$key."'"; $result = mysql_query($query); } else { echo "Key invalid"; } } else // form hasn't been submitted, so display the form { echo '<form method="POST" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> <input type="text" name="key"> <input type="submit" value="Submit!"> </form>'; } ?> Its a form to check if the licence key someone provided during an installation of a script is valid or not, you enter the key in the form and it tells you if its valid, used or invalid. For some reason the form wont show up at all and there seems to be no errors in the coding, does anyone spot the problem? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 20, 2010 Share Posted May 20, 2010 AFAIR it's $_SERVER['PHP_SELF'], but even with action="" it should use current url Quote Link to comment Share on other sites More sharing options...
Russia Posted May 20, 2010 Author Share Posted May 20, 2010 <?php mysql_connect("localhost", "brian_keys", "123321") or die(mysql_error()); mysql_select_db("brian_keys") or die(mysql_error()) if (isset ($_POST['submit'])) // if the form was submitted, display their name { $key = addslashes(htmlentities($_POST['key'])); if ($key == "") { die("No key found"); } $query = "SELECT * FROM keys WHERE key='".$key."'"; $result = mysql_query($query) or die("Could not execute query"); if (mysql_num_rows($result) > 0) { echo "Key valid"; // Setting the key activated $query = "UPDATE keys SET activation='yes' WHERE key='".$key."'"; $result = mysql_query($query); } else { echo "Key invalid"; } } else // form hasn't been submitted, so display the form { ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="key"> <input type="submit" value="Submit!"> </form> <?php } ?> ALright I fixed it, but it still wont show up... Whats the problem in the coding??? Quote Link to comment Share on other sites More sharing options...
Russia Posted May 20, 2010 Author Share Posted May 20, 2010 BUmp. Quote Link to comment Share on other sites More sharing options...
Uhkis Posted May 20, 2010 Share Posted May 20, 2010 Try: $_SERVER['SCRIPT_NAME']; Oops, completely wrong answer. Try changing the if-else order, if (!isset($_POST['submit'])) .... Also, are you coming to this page from a form? Quote Link to comment Share on other sites More sharing options...
Russia Posted May 20, 2010 Author Share Posted May 20, 2010 ALright, I found and fixed the error, but now the form shows up and doesnt work, it goes to pho self but doesnt give an error when there is no key entered, invalid or valid, it just reshows the form, what am I doing wrong??? <?php if (isset ($_POST['submit'])) // if the form was submitted, display their name { mysql_connect("localhost", "brian_keys", "******") or die(mysql_error()); mysql_select_db("brian_keys") or die(mysql_error()); $key = addslashes(htmlentities($_POST['key'])); if ($key == "") { echo "no key found"; } $query = "SELECT * FROM keys WHERE key='".$key."'"; $result = mysql_query($query) or die("Could not execute query"); if (mysql_num_rows($result) > 0) { echo "Key valid"; // Setting the key activated $query = "UPDATE keys SET activation='yes' WHERE key='".$key."'"; $result = mysql_query($query); } else { echo "Key invalid"; } } else // form hasn't been submitted, so display the form { ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" > <input type="text" name="key"> <input type="submit" value="Submit!"> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 20, 2010 Share Posted May 20, 2010 Only form fields that have name="..." attributes are submitted to the server. Your submit form field does not have a name and the php code testing if (isset ($_POST['submit'])) is always false. Quote Link to comment Share on other sites More sharing options...
Russia Posted May 20, 2010 Author Share Posted May 20, 2010 Oh, got it to work, now it gives me a new error when I proccess the form, it says: Could not execute query Its part of the OR DIE thing. Is it because it cannot find the table or why? <?php if (isset ($_POST['submit'])) // if the form was submitted, display their name { mysql_connect("localhost", "brian_keys", "123321") or die(mysql_error()); mysql_select_db("brian_keys") or die(mysql_error()); $key = addslashes(htmlentities($_POST['key'])); if ($key == "") { echo "no key found"; } $query = "SELECT * FROM keys WHERE key='".$key."'"; $result = mysql_query($query) or die("Could not execute query"); if (mysql_num_rows($result) > 0) { echo "Key valid"; // Setting the key activated $query = "UPDATE keys SET activation='yes' WHERE key='".$key."'"; $result = mysql_query($query); } else { echo "Key invalid"; } } else // form hasn't been submitted, so display the form { ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="key"> <input type="submit" name="submit" value="Submit!"> </form> <?php } ?> Is it unable to get the table or the column? Quote Link to comment Share on other sites More sharing options...
Uhkis Posted May 20, 2010 Share Posted May 20, 2010 Only form fields that have name="..." attributes are submitted to the server. Your submit form field does not have a name and the php code testing if (isset ($_POST['submit'])) is always false. In my experience submit is SET, but it's empty, so that works. ALright, I found and fixed the error, but now the form shows up and doesnt work, it goes to pho self but doesnt give an error when there is no key entered, invalid or valid, it just reshows the form, what am I doing wrong??? if ($key == "") { echo "no key found"; } You don't do anything if the key is empty.. return false or die() it. Quote Link to comment Share on other sites More sharing options...
Uhkis Posted May 20, 2010 Share Posted May 20, 2010 Oh, got it to work, now it gives me a new error when I proccess the form, it says: Could not execute query Its part of the OR DIE thing. Is it because it cannot find the table or why? Is it unable to get the table or the column? Print out mysql_error() after the simple error message, so you can see what's wrong. mysql_query() or die("Could not execute query. ".mysql_error()) Quote Link to comment Share on other sites More sharing options...
Russia Posted May 20, 2010 Author Share Posted May 20, 2010 Alright, I was able to fix that too. Now I have another problem, some kind of syntax error: no key found You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys WHERE key=''' at line 1 This is the line its talking about $query = "SELECT * FROM keys WHERE key='".$key."'"; I think its that last part, were its using the $key variable. Are the single quotes, periods, or quotes around it messing it up? from this whole script: <?php if (isset ($_POST['submit'])) // if the form was submitted, display their name { mysql_connect("localhost", "brian_keys", "*******") or die(mysql_error()); mysql_select_db("brian_keys") or die(mysql_error()); $key = addslashes(htmlentities($_POST['key'])); if ($key == "") { echo "no key found"; } $query = "SELECT * FROM keys WHERE key='".$key."'"; $result = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($result) > 0) { echo "Key valid"; // Setting the key activated $query = "UPDATE keys SET activation='yes' WHERE key='".$key."'"; $result = mysql_query($query); } else { echo "Key invalid"; } } else // form hasn't been submitted, so display the form { ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="key"> <input type="submit" name="submit" value="Submit!"> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
Uhkis Posted May 20, 2010 Share Posted May 20, 2010 Now I have another problem, some kind of syntax error: no key found You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys WHERE key=''' at line 1 This is the line its talking about $query = "SELECT * FROM keys WHERE key='".$key."'"; What kind of data is that key? If it's INT you don't need quotes. Quote Link to comment Share on other sites More sharing options...
Russia Posted May 20, 2010 Author Share Posted May 20, 2010 key is the data from the form... its the column were the keys are stored... Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 20, 2010 Share Posted May 20, 2010 http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html Quote Link to comment Share on other sites More sharing options...
Russia Posted May 20, 2010 Author Share Posted May 20, 2010 ALright, I changed the name of the key instead to key_name, but it still gives me the same error. I dont get why it does that? ALso, only the column name cant be key or keys correct? Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 20, 2010 Share Posted May 20, 2010 keys is also a reserved word. You can still use it as column name, but you have to put it in backticks $query = "SELECT * FROM `keys` WHERE `key`='".$key."'"; Quote Link to comment Share on other sites More sharing options...
Russia Posted May 20, 2010 Author Share Posted May 20, 2010 WOO!! that simple.... crap i feel like an idiot, also, can I ask for one more thing, I would also like to add a message if the key has already been used, that means that if activation column is already set to yes it gives an error message saying like License key already used. How am I able to do that? Quote Link to comment 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.