Jump to content

Help with code. query does not work inside a function?


r2get

Recommended Posts

Hello all,

 

This is my first post. Im trying to learn PHP from scratch. So for myself im making a registration system and some other things but there is something strange happening and i can not figure it out.

 

I have a registration form that makes a email activation link. So now on activation.php im doing something like this


<?phpif (isset($_GET['emailcode']))
{ 
$emailcode = strip_tags(trim($_GET['emailcode']));


//Check if only numbers and letters
if (!preg_match("/^[a-zA-Z0-9]+$/", $emailcode)){


echo $emailcode;
$errors[] = 'code is not ok..';
} 
else {
activateUser($emailcode);
}
}

activateUser goes tot functions.php where i do this

function activateUser($emailcode) {
$sql = "SELECT * FROM users WHERE emailcode='$emailcode' LIMIT 1";
$query = mysqli_query($db_connect, $sql);


$code_check = mysqli_num_rows($query);


if ($code_check == 1) {


echo 'exists';


}


}

the strange thing is, it returns nothing.. but when i attach te code on the activation page itself like this:

<?phpif (isset($_GET['emailcode']))
{ 
$emailcode = strip_tags(trim($_GET['emailcode']));


//Check if only numbers and letters
if (!preg_match("/^[a-zA-Z0-9]+$/", $emailcode)){


echo $emailcode;
$errors[] = 'De door jou opgegeven email code klopt niet';
} 
else {
$sql = "SELECT * FROM users WHERE emailcode='$emailcode' LIMIT 1";
$query = mysqli_query($db_connect, $sql);


//Tellen hoeveel rijen er gevonden zijn 0 of 1 
$code_check = mysqli_num_rows($query);


if ($code_check == 1) {


echo 'exists';


}


}
}
?>
it does work.... what am i doing wrong?
 
on the activation page I have included de db connect and the functions...
Edited by r2get
Link to comment
Share on other sites

Hi, could you please post ALL related code and files, we need to see all of the page contents also;

 

you dont have a closing ?> after

 

else {
activateUser($emailcode);
}
}

 

that maybe why its not working, if not then come back with all the code from the pages please

Link to comment
Share on other sites

here is actiovation .php

<?php include_once('config/db_connect.php');
include_once('config/init.php');
include_once ('functions/functions.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
<title>ASK</title>
<script type="text/javascript">
  WebFontConfig = {
    google: { families: [ 'PT+Sans:400,700:latin' ] }
  };
  (function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
      '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
  })(); </script>
  
<link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700' rel='stylesheet' type='text/css'>


    <!-- MAIN STYLE -->
    <link rel="stylesheet" href="assets/css/style.css">
    
<!-- BOOTSTRAP STYLE http://twitter.github.com/bootstrap/ -->
<link rel="stylesheet" href="assets/css/bootstrap.css" media="screen">
    
    <!-- BOOTSTRAP MODAL EXTEND https://github.com/jschr/bootstrap-modal/ -->
    <link rel="stylesheet" href="assets/css/bootstrap-modal.css" media="screen" />


<!-- JQUERY -->
<script type="text/javascript" src="assets/js/jquery-1.9.1.min.js"></script>


<!-- REGISTER JS -->
<script type="text/javascript" src="assets/js/register.js"></script>
    
    <!-- BOOTSTRAP JS, ADDONS & CUSTOM -->
    <script type="text/javascript" src="assets/js/script.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>


</head>
<body>






<?php include_once('template/topNav.php'); ?>    


<!-- BEGIN MAIN CONTAINER --> 
<div  id="mainContainer" class="container">
<?php include_once('template/mainHeader.php'); ?>      
      <div class="row" id="activateBody">
      <!-- <img src="assets/img/signup-right.png" /> -->




Dit is de activerings pagina.... 




<?php


if (isset($_GET['emailcode']))
{ 
$emailcode = strip_tags(trim($_GET['emailcode']));


//Check if only numbers and letters
if (!preg_match("/^[a-zA-Z0-9]+$/", $emailcode)){


echo $emailcode;
$errors[] = 'De door jou opgegeven email code klopt niet';
} 
else {
$sql = "SELECT * FROM users WHERE emailcode='$emailcode' LIMIT 1";
$query = mysqli_query($db_connect, $sql);


//Tellen hoeveel rijen er gevonden zijn 0 of 1 
$code_check = mysqli_num_rows($query);


if ($code_check == 1) {


echo 'exists';


}


}
}


?>
</div>
</div>
 <?php include_once('template/footer.php'); ?>  
<!-- END MAIN CONTAINER --> 
</body>
</html>

ans here is functions

<?phpfunction activateUser($emailcode) {
$sql = "SELECT * FROM users WHERE emailcode='$emailcode' LIMIT 1";
$query = mysqli_query($db_connect, $sql);


//Tellen hoeveel rijen er gevonden zijn 0 of 1 
$code_check = mysqli_num_rows($query);


if ($code_check == 1) {


echo 'exists';


}


}
?>
but please refer back to origial post because now i have put the code in activation to test.. but i want this in functions..
Link to comment
Share on other sites

I think, that's a logic error b/s of if{}else{} block and the $emailcode has never been assigned a value as an argument to the activateUser function.

 

So, try something like that:

<?php 
if (isset($_GET['emailcode'])) { 
$emailcode = strip_tags(trim($_GET['emailcode']));

//Check if only numbers and letters
if (!preg_match("/^[a-zA-Z0-9]+$/", $emailcode)){
$errors[] = 'De door jou opgegeven email code klopt niet';
 }

 // check for errors
 if (!empty($errors)) {
 // display the errors and return false
    
 } else{
 // call the activateUser function and procced the query statement
 activateUser($emailcode);     
 }
}
Edited by jazzman1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.