homepage Welcome to WebmasterWorld Guest from 54.198.157.6
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
if / elseif to call script
Query MySQL before calling script
richardhc




msg:4437825
 2:50 am on Apr 6, 2012 (gmt 0)

I need to query a MySQL database to see if an activation key has already been issued before calling the activation script to make a key.

This is what I am starting with, I have added notations where I would like help with the code:


<form name="registration" action="<?=$PHP_SELF?>" method="POST">
<p>Please enter your name: <input type="text" name="regname" size="30"></p>
<p> Enter the Registration Code: <input type="text" name="hardware" size="30" /></p>
<p><input type="submit" name="Send" value="OK" id="OK"></p>
</form>

<?php

if( @$Send == "OK" )
{
/* OK-Button pressed - how do I first check to see if a key is already stored in field C3 of MySQL database? */

if(/*how do I query the MySQL database for field C3 if there is nothing in the table ==0 then call the script*/)
{
require_once("activationkey.php");
echo( "<p>Here is your private key:</p>\n" );
echo( "<p>Name: $regname </p>\n" );
echo( "<p>Activation Key: " .MakeKey($1aa, $2bb, $regname, $hardware)."</p>\n" );

/* How do I send the returned key to database field C3 so any future attempt to call the MakeKey script will move to the elseif? */

}

}
elseif(/*a key was found in field C of the database*/ > 0)
{
echo( "<p>This software has already been activated. The activation key issued was: $key </p>\n" );
}

?>

 

enigma1




msg:4437881
 9:49 am on Apr 6, 2012 (gmt 0)

You use the mysql_query function to query if the key is already in something like this:

$check_query = mysql_query("select count(*) as total from table_holding_keys where stored_key='" . $whatever_key_passed . "'")
$check_array = mysql_fetch_array($check_query);
// If total is not zero means the key is already in
if( $check_array['total']) {
// Key in database
} else {
// Key is not in the database verify and activate
}

richardhc




msg:4438192
 12:21 am on Apr 7, 2012 (gmt 0)

Thanks very much enigma1

Dreamweaver shows an error in this line

$check_array = mysql_fetch_array($check_query);

enigma1




msg:4438251
 10:47 am on Apr 7, 2012 (gmt 0)

I forgot a semicolon at the end on the first code line

$check_query = mysql_query("select count(*) as total from table_holding_keys where stored_key='" . $whatever_key_passed . "'");

You need to setup the table name and variable for the key, this is a sample only.

richardhc




msg:4438325
 7:31 pm on Apr 7, 2012 (gmt 0)

Yes, that fixed it. I am not sure how the database will know which user to check. The user has to log in to get to this page using the same database. Will that automatically result in any query looking at that user's information? Or does my query have to identify the user?
Here is what I have now:

<?php
$groupswithaccess="NC1";
require_once("../slpw/sitelokpw.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style5 {font-size: 9px}
-->
</style>
</head>
<?php //connect to the database these includes configure the $dbhost $dbuser $dbpass $dbname and open the connection to the database
include 'config.php';
include 'opendb.php';
?>

<body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
<form name="registration" action="<?=$PHP_SELF?>" method="POST">
<p>Please enter your name: <input type="text" name="regname" size="30"></p>
<p> Enter the Registration Code: <input type="text" name="hardware" size="30" /></p>
<p><input type="submit" name="Send" value="OK" id="OK"></p>
</form>


<?php
if( @$Send == "OK" )
{
/* OK-Button pressed - first check to see if a key is already stored in field C3 of MySQL database */

$check_query = mysql_query("select count(*) as total from $dbname where Custom3='" . $whatever_key_passed . "'"); //How do I make sure this is using the field from the logged in user?
$check_array = mysql_fetch_array($check_query);
// If total is not zero it means the key is already in the database

if( $check_array['total']) {
// Key in database
echo( "<p>This software has already been activated. The activation key issued was: $Custom3 </p>\n" );
} else {
// Key is not in the database verify and activate
require_once("activationkey.php");
echo( "<p>Here is your private key:</p>\n" );
echo( "<p>Name: $regname </p>\n" );
echo( "<p>Activation Key: " .CreateKey(22, "e76", $regname, $hardware)."</p>\n" );
}
}
/* Send the returned key and hardwareID to database fields Custom3 and Custom4 so any future attempt to call the MakeKey script will not call the activation script */
mysql_select_db($dbname);
$query = "INSERT INTO sitelok (custom3,custom4) VALUES ('$returned_key','$hardware')";

mysql_query($query) or die('Error, insert query failed');

}
mysql_close($conn);
?>

enigma1




msg:4438500
 1:07 pm on Apr 8, 2012 (gmt 0)

Is the $returned_key what holds the registration key? Then that's the value you check in the sql query. I cannot see from the form which parameter holds it.

richardhc




msg:4438543
 7:27 pm on Apr 8, 2012 (gmt 0)

The returned_key is what is created by the activationkey.php script and is returned by sending

echo( "<p>Activation Key: " .CreateKey(22, "e76", $regname, $hardware)."</p>\n" );
This is the value that will be stored as Custom3 in the database.

Will MySQL check custom3 - and enter a new key into custom3 for the user who has logged in or do I need to specify the username after the database is opened?

enigma1




msg:4438662
 8:54 am on Apr 9, 2012 (gmt 0)

The values I see from the original form you posted are regname and hardware. This is what the visitor will have to enter in the form. So if it's regname or hardware that is stored in the table then its the field you need to compare with and has to be unique. So if the hardware is the same for multiple clients you need to check the regname and store it in the database after successful submission so you can cross-reference it thereafter.

Mysql will only compare with what the query statement instructs it to compare with.

richardhc




msg:4438883
 8:46 pm on Apr 9, 2012 (gmt 0)

If possible, the initial check should identify the regname and hardware then see if there is an activation key for that combination. The person has already logged in to the same database using their name and a password so I was hoping that when the query was made to see if that person already had an activation key MySQL would somehow know that this is for the logged in person. Can the login process provide $regname without asking for it in the form?

It could happen that two people with the same name might use the software and it is also possible (unlikely) that the hardware number might be repeated somewhere in the table - but it is extremely improbable that the regname/hardware would ever be repeated in the table. Also only one person would have the password to log in.

Sorry, you can see I really don't understand how the MySQL system works.

enigma1




msg:4438918
 9:19 pm on Apr 9, 2012 (gmt 0)

Then you need a unique identifier and store it to the database. It has to be created from the input form since you will have to re-validate it thereafter. Perhaps you need a unique serial number for the s/w and have the clients submit it with the rest of the form. In this case you will need to create a new column in the database eg: unique_id which will hold the serials.

richardhc




msg:4438921
 9:27 pm on Apr 9, 2012 (gmt 0)

The users have to log in using a unique password to have access to this page and that password is already in the database.
<?php
$groupswithaccess="NC1";
require_once("../slpw/sitelokpw.php");
?>
Brings up a login page where they enter their name and password.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved