Getting row from DB based on comparison of passed string

Let me try to explain this: I’m again modding my custom invite system ( :smiley: ). I now am creating random 10 char strings that get inserted into the DB on page one (where existing members enter their username and the e-mail of the person being invited). I also capture the entered e-mail and save this to the DB as well. Now, when the invitee clicks the link they receive in their e-mail, it will pass the random code to page two. I want to then “retrieve” the e-mail from the DB that matches the code and stick it in a string for use later. I’ve laid the groundwork down, but am not sure how to compare the passed code to the one in the DB and retrieve the matching e-mail.

Any tips would be appreciated! Thanks!

WHERE code = ?
->execute([$_GET['code']])

?

If that means nothing for you, may you should start with some basic database tutorial

I kinda follow. :slight_smile: I’m using mysql and prepared statements. Can you please elaborate?

I still need help with this please. Here is a snippet related to what I’m doing (this is supposed to enter the e-mail and randomly generated code into the DB):

$stmt = $db2->prepare('INSERT INTO invites (email, code) VALUES (?, ?)');
$stmt->bind_param('ss', $email, $str);
$stmt->execute();
$stmt->close();
$db2->close();

And the script that runs before this to create $str:

function rand_string( $length ) {  
$chars = "[email protected]#$&*{}[]%=+-_~`!()/\|";
$size = strlen( $chars );  
for( $i = 0; $i < $length; $i++ ) {  
	$str= $chars[ rand( 0, $size - 1 ) ];		  
}  
}  
rand_string( 10 );  

It produces a blank page. It must be this particular code (the first example), because when I remove it, the script works. Help! :slight_smile:

@phdr Any thoughts? :smiley:

blank page means you have a look into the web developers console, where you likely would get a 500 error from the server, what means you have to look into the servers error.log

I did “inspect element” on Firefox and looked at everything there. I didn’t see any 500 error or any errors at all. In error.log I didn’t see any reference to it. Just a bunch of warnings from my main site (the invite system is in a sub-folder). :frowning:

you have to look under the network tab in the webdevelopers console (key F12)

Yes, found it, a 500 error. It turns out I wasn’t declaring a statement to open my DB. lol It’s working now, but I think I’m making it too complex. I may just leave it as is since it works and everything is prepared statements…

Thanks!

Sponsor our Newsletter | Privacy Policy | Terms of Service