function help - looping but no loop?

They have: 426 posts

Joined: Feb 2005

My insert function to add a new user to the system seems to print out the errors twice? There is no loop and when i append a variable to the end of the error it increments by one twice.

<?php
/*
* Inserts a new user into the database
*
* @param mixed $data an associative array containing the database fields
* @return bool did the query succeed?
*/
   
public function insert($data)
    {
       
        if(
is_array($data)){
       
           
/* the $data array passed to this method will be filtered to only these elements */
           
$data_fields = array('level',
                                
'email',
                                
'email_confirm',
                                
'password',
                                
'registered',
                                
'updated');
                   
           
/* add some more fields (don't forget to add the fields to $data_fields[]) */
           
$data['registered'] = date('Y-m-d H:i:s');
           
$data['updated'] = $data['registered'];
           
$data['email_confirm'] = $this->ags_get_key(10);
           
$data['level'] = 0;
       
           
// first lets see if the email address provided is correct format   
           
if($data['email'] == ""){
               
$this->error .="<li>Please enter an email address</li>";
            }elseif(!
$this->check_email_address($data['email'])){       
               
$this->error .= "<li>Email address invalid</li>";           
            }elseif(
$this->unique_email($data['email'])){
               
$this->error .="<li>The email address supplied is already registered";
            }
           
            if(
$data['pass1'] == ""){
               
$this->error .="<li>Please enter a password</li>";
            }elseif(
$data['pass1'] != "" && $data['pass2']== ""){
               
$this->error .= "<li>You must verify your password</li>";
            }elseif(
$data['pass1'] != $data['pass2']){       
               
$this->error .= "<li>passwords do not match</li>";           
            }
           
            if(
$this->error != ""){           
                return
FALSE;
                exit(
0);           
            }   
                   
$data['pass1'] = md5($data['pass1']);
                   
/* form the SQL query by looping through the $data[] elements */
                   
$fields = $values = '';
                    foreach (
$data as $field => $value) if (in_array($field, $data_fields))
                    {
                       
$fields .= "`$field`,";
                       
$values .= "'" . mysql_real_escape_string($value, $this->dbconnection()) . "',";
                    }
                   
                   
/* remove the last comma from both variables */
                   
$fields = substr($fields, 0, strlen($fields) - 1);
                   
$values = substr($values, 0, strlen($values) - 1);
                   
                   
/* run the query */
                   
$query = "INSERT INTO users ($fields) VALUES ($values)";
                   
$result = $this->query($query);
                   
$this->error_check($query, $result);
                   
                   
/* send out verification email
                    if ($result)
                    {
                        //echo "<b><h2>".$query." Not working</b></h2>";
                        /*$data['user_id'] = mysql_insert_id($this->db_link);
                       
                        $mail = new Mailer();
                        $mail->set_to($data['user_name'] . ' <' . $data['email'] . '>');
                        $mail->set_from('[email protected]');
                        $mail->set_subject('Some Site Registration Confirmation');
                        $mail->set_text_tpl('verify.txt');
                        $mail->set_data($data);
                        $mail->process();
                       
                    }*/   
                    /* suiccessful query = 1 (true), failed query = 0 (false) */
                   
return mysql_affected_rows($this->dbconnection());               
            }               
    }
?>

There is no loop after the errors have been checked? Can anyone help.

Here is where i call the function

<?php
           
if(isset($_POST['register'])){           
               
$data['email'] = $_POST['email'];
               
$data['pass1'] = $_POST['pass1'];
               
$data['pass2'] = $_POST['pass2'];               
               
                if(
$check->insert($data)==1){
                    echo
'Success!';
                    include(
"html/login_form.php");   
                }elseif(
$check->insert($data)==FALSE){
                   
$return = "<ul style=\"margin:1.5em;color:red;font-weight:bold;\">".$check->error."</ul>";
                    include(
"html/registerForm.php");               
                }
               
            } else {   
                echo
"register or login";
            }
?>

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Well you do have a loop in there, the foreach, so if it's a PHP error you are getting, rather than a mysql error, then it could be the foreach with a problem.

What error do you get?

They have: 426 posts

Joined: Feb 2005

Hi thanks for the quick reply.

I dont think i have been clear enough. Not a php error;my own error detection - ie...email address not filled in, passwords do not match.

If you look i check to see if the variable error has anything in it, if it does then i return false and exit().

Trouble is that what ever errors i pick up are printed twice?

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

troubleshoot the flow

add echo "step1 "; and echo "step2 "; etc.

or echo "<script>alert('step1')</script>";

to see where it breaks down - you could also add variables if that helps...

They have: 426 posts

Joined: Feb 2005

Yes this is what i did:

in place of the below

            if($this->error != ""){          
                return FALSE;
                exit(0);          
            }

i changed to this:

            if($this->error != ""){ 
                $this->error .=$i++;        
                return FALSE;
                exit(0);          
            }

i being declared at the top of my script as 0. The result appended 0 then 1 to the end of each set of errors - for example

please enter an email address
please confirm your password
0

please enter an email address
please confirm your password
1

the example above you can see 0 then 1.
I have exited the script so really nothing below it should be executing, and above there is no loop?

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Edited as was incorrect findings

They have: 426 posts

Joined: Feb 2005

Sorry greg, im not sure i understand?

greg's picture

He has: 1,581 posts

Joined: Nov 2005

If you read my last post before I removed what I wrote then my apologies. I re-read your code and realised what the second array you made was doing.

something I noticed unrelated to the error you have an issue with..

<?php
foreach ($data as $field => $value)

if (
in_array($field, $data_fields))
     {
        
$fields .= "`$field`,";
        
$values .= "'" . mysql_real_escape_string($value, $this->dbconnection()) . "',";
                    }
?>

If the value of $field isn't in the array $data_fields, then it wont be sanitised by real_escape.
Perhaps some of the data in the $data array doesn't need filtering, and you have already checked the data on a previous page to ensure it's ok to enter into the db I.E. no apostrophes or quotes etc.
Otherwise it's a potential to NOT sanitise data that might need to be for security.

As for your initial problem, within the code you provided (the function) I see no reason whatsoever why it would echo the error twice.
I have to go out atm, but if you or no-one else has found the error or put you on the right track I'll have another look later.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Are you sure you're not calling the method more than once? I know it sounds too simple, but there is no way the code you posted would run twice unless you called the method twice.

Another possibility. I see that you append $this->error, but I don't see where it is declared. If you are saving/restoring that variable from a session, then you could be building and copying error messages as you test.

They have: 426 posts

Joined: Feb 2005

I build the Error variable and call it on the main page from $this->error if the method returns false - this prints the error messages?

I will have another look then.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

The method you posted never prints anything. It just returns an error message if something goes wrong.

Also, in this code:

<?php
if($this->error != ""){          
return
FALSE;
exit(
0);          
}
?>

exit(0); will never run because the return statement is above it, and once you return, the method is over.

They have: 426 posts

Joined: Feb 2005

OK,
But i am building the error message and putting it in $error if $error has something in it i return false.
On the page that calls the method insert checks to see if it is returning false (has some errors) if it does it prints out $error.

I then dont want the method Insert to run any further past the error so i exit the scipt?

Is this incorrect?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.