I'm new to php and sqlite and I'm trying to modify the registration form of a website. I did what I thought were all the necessary changes but I can't make it work now since when I use a email to register it tells me it already exists. The form used a username before and checked if the username already existed. If it did then it displays a message. Now I don't want to use a username but the email to register and, even though the database is empty, it always tells me that the email already exists. I left only the email for registration to make it simpler to understand. But even then the problem persists.
I think there's a problem while checking the database but I don't know where the problem lies. I don't understand very well the flow between the php pages so if someone could help me clarify what is exactly happening I would appreciate it. Here is the code of the registration page.
Participate.php:
<hr class="structure" />
<h1 class="out">Main content</h1>
<div class="eb1">
<!--TYPO3SEARCH_begin-->
<h2 class="out">Topinformationen</h2>
<!--TYPO3SEARCH_end-->
</div>
<div class="eb2">
<!--TYPO3SEARCH_begin-->
<h2>Participate</h2>
<p>Please register.</p>
<!-- ###LOGIN_FORM### -->
<div class="box big form">
<form id="participateForm" action="#" target="_top" onSubmit="return checkform();" method="post" name="participate">
<fieldset class="nolegend">
<legend>Particpate</legend>
<div>
<label for="email">
<span> Email:* <?php echo isset($_POST['email']) ? '<strong>The email “' .$_POST['email']. '” already exists. </strong>' : ''; ?></span><input type="email" name="email" required/>
</label>
</div>
</fieldset>
<div class="morelink">
<p><input type="submit" name="submit" value="Participate" onclick="show('form'); return FALSE;"/></p>
</div>
</form>
</div><!-- box big form -->
</div>
So if I understand correctly when you click the Participate button the page calls itself (action="#") with the email as a post variable. The thing is that there's nothing on this particular page that uses that variable so I guess this code is reloading the index.php page which starts like this.
index.php:
<?PHP
//Session wird gestartet
session_start();
//Includ PHP functions
@include("./data/php/functions.php");
//Funktionen werden ausgeführt
if(isset($_GET['do']))
{
if($_GET["do"]=="logout"){
logout();
}
}
else if (!empty($_GET["email"])&& !session()){
login($_GET["email"]);
}
if(postvar("submit")=="login" && !session()){
login(0,0);
}
else if (postvar("submit")=="participate" && !session()){
register();
}
else if (postvar("submit")=="emote" && session()){
saveMood();
}
?>
Within this functions.php code that is included in the index.php page are the functions to access the database. So two of those functions are these.
functions.php:
function postvar($var)
{
if (isset($_POST[$var])){
$erg = $_POST["$var"];
$erg = trim($erg);
}
else
$erg=NULL;
return $erg;
}
function register()
{
global $open, $settings, $db_usr;
$email = postvar("email");
//if($privacy && $terms)
if(TRUE)
{
$query = "SELECT * FROM usr_data WHERE email='".$email."'";
$res_usr = $db_usr->prepare($query);
$res_usr->execute();
$count=$res_usr->fetchColumn();
if($count!=1){
$query ="INSERT INTO usr_data (email, regtime, status, sessionID) VALUES ('".$email."', '".time()."','0', 'new')";
$db_usr->query($query);
header("Location: ".$settings["Main"]."?open=Login&info=1");
}
else
{
//$open="Login_Failed";
}
}
}
So what I think it's happening is this and I will appreciate if someone tells me if I'm completely lost or if I need to put some other part of the code to clarify. So the participate.php calls itself with the email send on this post variable. Somehow, and I don't know exactly how, the functions.php code is called and this register function is used to check if the email exists and, if not, to add it to the database. The thing is that it's not working. This code worked before when instead of email it used a username. The registration actually worked then.
By the way the checkform() function that is called in the Participate.php code just does this.
function checkform()
{
if (document.participate.email.value == "")
{
alert('Please enter an email.');
document.participate.email.focus();
return false;
}
}
Aucun commentaire:
Enregistrer un commentaire