Hello,
I tried without luck and my code well I scraped it. It was more of a mess then anything.
If someone can point me in the right direction, please.....
What I want to do is I do not want anyone to register on my site but I would like to send out an INVITE with a ID.
Email would be something like this.
Please use this code 1234567890 to register with our site etc.....
I have a database with the fields
invite,date,tries,active
The part I am having trouble with is the user entering the ID and updating the DB
I am able to connect to the data base and enter the code but updating the db is the issue. I need to check the DB to make sure the ID is still active return ID has been already used. If active then continue with script or go to register.php
invite - the id code
Tries just to see how many people have tried that code
date to when it went from active to not active.
active- active or not active
I only want the code to be only used once. As for entering the code into theDB from the admin I can do this by the backend and someday will write the code.
This sounds simple but just can not get it to work and like I said I would post the code but it would take more time to figure out what I am doing wrong then it is worth.
Thanks for any HELP, I tried looking for php code around the web and if I found something close it had more features but then I tried to take it out just lead to other issues.
8 replies to this topic
#1
Posted 02 April 2011 - 11:33 AM
|
|
|
#2
Posted 02 April 2011 - 01:42 PM
I've written really the base logic for what your script is wanting to do, I am unsure if it works as is, although it does show you how to update time, iterate tries and deactivate it once activated.
You could even generate invites in batches with your own makeshift hashing algorithm (such as half of a salted sha1 hashes of some known numbers beforehand, otherwise it is tedious)
$usercode = mysql_real_escape_string( $_POST['invite'] );
$usercode_query = mysql_query( "SELECT id, active FROM table WHERE invite = '$usercode'" );
//if exists
if( mysql_num_rows( $usercode_query ) ) {
$usercode_row = mysql_fetch_row( $usercode_query );
//if active
if( $usercode[1] == '1' ) {
echo "Your invite has been accepted";
mysql_query( "UPDATE table SET active = 0 AND date = NOW() WHERE invite = '$usercode'" );
} else {
//if not active
echo "The invite you have entered had already been used.";
mysql_query( "UPDATE table SET tries = tries + 1 WHERE invite = '$usercode'" );
}
} else {
//if not exists
echo "The invite you have entered was invalid";
}You'll need to read up on datetime columns and also defaults (tries must be not null, and default to zero if you want it to iterate for example)You could even generate invites in batches with your own makeshift hashing algorithm (such as half of a salted sha1 hashes of some known numbers beforehand, otherwise it is tedious)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 02 April 2011 - 02:04 PM
Thanks, That looks better then mine :) I used to many if statements and believe that is the place I was lost.
#4
Posted 03 April 2011 - 04:00 AM
Thanks for the code, have an issue with the line $usercode_row = mysql_fetch_row( $usercode_query );
The invite code does get passed on, so that is not the problem, the connecting to database has no errors. Not sure what to look at next. Thanks!
Get the error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/public_html/register1.php on line 16
The invite code does get passed on, so that is not the problem, the connecting to database has no errors. Not sure what to look at next. Thanks!
Get the error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/public_html/register1.php on line 16
<?php
$dbHost = "localhost"; //Location Of Database usually its localhost
$dbUser = "XXXX"; //Database User Name
$dbPass = "XXXX"; //Database Password
$dbDatabase = "XXXX"; //Database Name
$db = mysql_connect($dbHost,$dbUser,$dbPass)or die("Error connecting to database.");
mysql_select_db($dbDatabase, $db)or die("Couldn't select the database.");
$usercode = mysql_real_escape_string( $_POST['invite'] );
$usercode_query = mysql_query( "SELECT id, active FROM table WHERE invite = '$usercode'" );
if( mysql_num_rows( $usercode_query ) ) {
$usercode_row = mysql_fetch_row( $usercode_query );
//if active
if( $usercode[1] == '1' ) {
echo "Your invite has been accepted";
mysql_query( "UPDATE table SET active = 0 AND date = NOW() WHERE invite = '$usercode'" );
} else {
//if not active
echo "The invite you have entered had already been used.";
mysql_query( "UPDATE table SET tries = tries + 1 WHERE invite = '$usercode'" );
}
} else {
//if not exists
echo "The invite you have entered was invalid";
}
?>
Edited by Orjan, 03 April 2011 - 03:55 PM.
use php or code tags around your code
#5
Posted 03 April 2011 - 06:42 AM
Try using mysqli instead of mysql. I think I remember having trouble with this a while back.
<?php
$dbHost = "localhost"; //Location Of Database usually its localhost
$dbUser = "XXXX"; //Database User Name
$dbPass = "XXXX"; //Database Password
$dbDatabase = "XXXX"; //Database Name
$db = mysqli_connect($dbHost,$dbUser,$dbPass)or die("Error connecting to database.");
mysqli_select_db($dbDatabase, $db)or die("Couldn't select the database.");
$usercode = mysqli_real_escape_string( $_POST['invite'] );
$usercode_query = mysqli_query( "SELECT id, active FROM table WHERE invite = '$usercode'" );
if( mysqli_num_rows( $usercode_query ) ) {
$usercode_row = mysqli_fetch_row( $usercode_query );
//if active
if( $usercode[1] == '1' ) {
echo "Your invite has been accepted";
mysqli_query( "UPDATE table SET active = 0 AND date = NOW() WHERE invite = '$usercode'" );
} else {
//if not active
echo "The invite you have entered had already been used.";
mysqli_query( "UPDATE table SET tries = tries + 1 WHERE invite = '$usercode'" );
}
} else {
//if not exists
echo "The invite you have entered was invalid";
}
?>
#6
Posted 03 April 2011 - 12:54 PM
Now I get the error
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home2/public_html/jobs/register1.php on line 8
Couldn't select the database.
That is mysqli_select_db($dbDatabase, $db)or die("Couldn't select the database.");
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home2/public_html/jobs/register1.php on line 8
Couldn't select the database.
That is mysqli_select_db($dbDatabase, $db)or die("Couldn't select the database.");
#7
Posted 03 April 2011 - 03:58 PM
The PHP Manual is your best friend. Look the function up at PHP: Hypertext Preprocessor and you'll see that your parameters isn't in the right order...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#8
Posted 05 April 2011 - 03:58 AM
Well looked at PHP Manual and many other examples that are close but I can not figure out what function is wrong. Like I say I do not know much about mysql or php but I do get things figure out after awhile except this small script. Just can not figure it. I tried so many options but always get different errors.
Thanks,
Thanks,
#9
Posted 05 April 2011 - 04:40 AM
Go and have a look at the PHP website link below.
PHP: mysqli::query - Manual
Use this code and reword it to do what you need:
Use MySQL Improve Extention and not the old one.
PHP: mysqli::query - Manual
Use this code and reword it to do what you need:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
printf("Table myCity successfully created.\n");
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", $result->num_rows);
/* free result set */
$result->close();
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {
/* Note, that we can't execute any functions which interact with the
server until result set was closed. All calls will return an
'out of sync' error */
if (!$mysqli->query("SET @a:='this will not work'")) {
printf("Error: %s\n", $mysqli->error);
}
$result->close();
}
$mysqli->close();
?>
Use MySQL Improve Extention and not the old one.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









