Page 1 of 1

It won't insert values

Posted: Mon Jun 27, 2011 7:15 am
by Theramore
I got no errors on this code it just doesn't inserts the values into mysql anyone knows why?

Code: Select all

<?php


error_reporting(E_ALL);

class Register
{

	private $_register;
	private $_errors;
	private $_username;
	private $_password;
	private $_password2;
	private $_passmd5;
	private $_mail;
	private $_token;
	
	
	public function __construct()
	{
		
		$this->_register = isset($_POST['register'])? 1 : 0;
		
		if(isset($_POST['register']))
		{
			
			$this->_username = $this->filter($_POST['username']);
			$this->_password = $this->filter($_POST['password']);
			$this->_password2 = $this->filter($_POST['password2']);
			$this->_mail = $this->filter($_POST['mail']);
			$this->_token = $_POST['token'];
			$this->_passmd5 = md5($this->_password);
			$this->_ip = $_SERVER['REMOTE_ADDR'];
			$ths->_data = date('Y-m-D');
			
		}
		
		$this->_errors = array();
		
	}
	
	public function filter($var)
	{
		
		return preg_replace('/[^a-zA-Z0-9]/','',$var);
		
	}
	
	public function insert()
	{
		mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
		mysql_select_db("romsilva") or die(mysql_error());
		$query = mysql_query("INSERT INTO users VALUES('', '0', '0', '0', '0', '0')") or die(mysql_error());
	}
	
	
	

	
	public function register()
	{
		
		if($this->messages())
		{
		$this->insert();
		}
		else
			$this->showerrors();
		
	}
	

	public function passleng()
	{
		
		return(strlen($this->_password) > 6 && strlen($this->_password) < 25)? 1 : 0;
		
	}
	
	public function messages()
	{
		
		try{
			
			if(!$this->allfields())
				throw new Exception("Te rog completeaza toate campurile!");
			
			if($this->istokenvalid())
				throw new Exception("Formular nevalidat! Te rog completeaza din nou formularul fara a da refresh paginii!");
			
			if(!$this->database())
				throw new Exception("Utilizatorul exista deja!");
			
			if(!$this->chkpass())
				throw new Exception("Cele doua parole nu se potrivesc");
			
			if(!$this->passleng())
				throw new Exception("Parola trebuie sa fie intre 6 si 25 de caractere!");
			
			
			echo "Succes!";
			
		}
		catch(Exception $e)
		{
		$this->_errors[] = $e->getMessage();
		}
		
		
		
	}
	

	
	public function chkpass()
	{
		
		return($this->_password==$this->_password2)? 1 : 0;
		
	}
	
	
	public function allfields()
	{
		
		return($this->_username&&$this->_password&&$this->_password2&&$this->_mail)? 1 : 0;
		
	}
	
	
	public function database()
	{
		
		mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
		mysql_select_db("romsilva") or die(mysql_error());
		
		$data = mysql_query("SELECT * FROM users WHERE username = '{$this->_username}' ");
		
		if(mysql_num_rows($data)==0)
		{
		
			return true;
			
		}
		else
			return false;
		
		
	}
	
	public function istokenvalid()
	{
		
		return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
		
	}
	
	
	public function showerrors()
	{
		
		foreach($this->_errors as $key=>$value)
		{
			echo $value."<br>";
		}
			
	}
	
	
	
	
}
	
	
	
	
	


?>

and here's the register.php:

Code: Select all

<?php

mysql_connect("localhost", "ascent", "ppaass");
mysql_select_db("romsilva");
		
include("functions.php");
$register = new Register();
if(isset($_POST['register']))
{
	
	$register->register();
	

	
}
	
$token = $_SESSION['token'] = md5(uniqid(mt_rand(), true));
?>


<form action='<?php $_SERVER['PHP_SELF']; ?>' method='POST'>
<table>
<tr>
<td>
Username:
</td>
<td>
<input type='text' name='username'>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type='password' name='password'>
</td>
</tr>
<tr>
<td>
Rescrie parola:
</td>
<td>
<input type='password' name='password2'>
</td>
</tr>
<tr>
<td>
E-Mail:
</td>
<td>
<input type='text' name='mail'>
</td>
</tr>
</table>
<input type='reset' value='Reset'>
<input type='submit' name='register' value='Register'>
<input type='hidden' name='token' value='<?php echo $token; ?>'>
</form>

Re: It won't insert values

Posted: Mon Jun 27, 2011 11:53 am
by social_experiment

Code: Select all

<?php
$query = mysql_query("INSERT INTO users VALUES('', '0', '0', '0', '0', '0')") or die(mysql_error());
// try
$query = mysql_query("INSERT INTO users (field1, field2, field3, field4, field5, field6) 
VALUES ('', '0', '0', '0', '0', '0')" ) or die(mysql_error());
?>
Try adding the fieldnames where the data is supposed to go.

Re: It won't insert values

Posted: Mon Jun 27, 2011 12:00 pm
by Theramore
thanks a lot

Re: It won't insert values

Posted: Mon Jun 27, 2011 12:48 pm
by Theramore
nope it does not works

Re: It won't insert values

Posted: Mon Jun 27, 2011 1:10 pm
by Jade
Show us your updated code

Re: It won't insert values

Posted: Mon Jun 27, 2011 1:17 pm
by social_experiment
Try throwing the exceptions inside the functions. The code where you throw the exceptions looks a bit weird.

Code: Select all

<?php
 function trySomething($i)
 {
   if ($i != 0)
   {
     throw new Exception('i is not 0');
   }
   else
   {
    echo $i;
   }
 }

 //
 $x = 1;
 
 try 
 {
  trySomething($x);
 }
 catch (Exception $e)
 {
   echo $e->getMessage();
 }
?>
In the function showerrors() try testing the $this->_errors variable to see if it is an array, this will help with troubleshooting where the problem is.

Code: Select all

<?php

        public function showerrors()
        {
                if (is_array($this->_errors))
                {
                 foreach($this->_errors as $key=>$value)
                 {
                         echo $value."<br>";
                 }
                }
                else
                {
                         echo 'No error messages present.';
                }        
        }

?>

Re: It won't insert values

Posted: Mon Jun 27, 2011 2:20 pm
by Theramore
Doesn't works...

My updated code is here
for register.php:

Code: Select all

<?php

mysql_connect("localhost", "ascent", "ppaass");
mysql_select_db("romsilva");

include("functions.php");
$register = new Register();
$captcha = rand();
if(isset($_POST['register']))
{

	
		$register->register();





}


$token = $_SESSION['token'] = md5(uniqid(mt_rand(), true));

?>


<form action='<?php $_SERVER['PHP_SELF']; ?>' method='POST'>
<table>
<tr>
<td>
Username:
</td>
<td>
<input type='text' name='username'>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type='password' name='password'>
</td>
</tr>
<tr>
<td>
Rescrie parola:
</td>
<td>
<input type='password' name='password2'>
</td>
</tr>
<tr>
<td>
E-Mail:
</td>
<td>
<input type='text' name='mail'>
</td>
</tr>
<tr>
<td>
Captcha(<?php echo $captcha; ?>):
</td>
<td>
<input type='text' name='insertcaptcha'>
<input type='hidden' name='cap' value='<?php echo $captcha; ?>'>
</td>
</tr>
</table>
<input type='reset' value='Reset'>
<input type='submit' name='register' value='Register'>
<input type='hidden' name='token' value='<?php echo $token; ?>'>
</form>

and for functions.php:

Code: Select all

<?php

error_reporting(E_ALL);

class Register {
    private $_register;
    private $_errors;
    private $_username;
    private $_password;
    private $_password2;
    private $_passmd5;
    private $_mail;
    private $_token;
    private $_insertcaptcha;
    private $_cap;

    public function __construct()
    {
        $this->_register = isset($_POST['register'])? 1 : 0;

        if (isset($_POST['register'])) {
            $this->_username = $this->filter($_POST['username']);
            $this->_password = $this->filter($_POST['password']);
            $this->_password2 = $this->filter($_POST['password2']);
            $this->_mail = $this->filter($_POST['mail']);
            $this->_token = $_POST['token'];
            $this->_passmd5 = md5($this->_password);
            $this->_ip = $_SERVER['REMOTE_ADDR'];
            $this->_data = date('Y-m-D');
            $this->_insertcaptcha = $_POST['insertcaptcha'];
            $this->_cap = $_POST['cap'];
        }

        $this->_errors = array();
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9]/', '', $var);
    }

    public function insert()
    {
        mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
        mysql_select_db("romsilva") or die(mysql_error());
        $query = mysql_query("INSERT INTO users (id, username, password, admin, lastip, lastlogin) VALUES('', '0', '0', '0', '0', '0')") or die(mysql_error());
    }

    public function register()
    {
        if ($this->messages()) {
            $this->insert();
        } else
            $this->showerrors();
    }

    public function passleng()
    {
        return(strlen($this->_password) > 6 && strlen($this->_password) < 25)? 1 : 0;
    }

    public function messages()
    {
        try {
            if (!$this->allfields())
                throw new Exception("Te rog completeaza toate campurile!");

            if ($this->istokenvalid())
                throw new Exception("Formular nevalidat! Te rog completeaza din nou formularul fara a da refresh paginii!");

            if (!$this->database())
                throw new Exception("Utilizatorul exista deja!");

            if (!$this->chkpass())
                throw new Exception("Cele doua parole nu se potrivesc");

            if (!$this->passleng())
                throw new Exception("Parola trebuie sa fie intre 6 si 25 de caractere!");

            if (!$this->captcha())
                throw new Exception("Cod captcha incorect!");

            echo "Succes!";
        }
        catch(Exception $e) {
            $this->_errors[] = $e->getMessage();
        }
    }

    public function chkpass()
    {
        return($this->_password == $this->_password2)? 1 : 0;
    }

    public function captcha()
    {
        return($this->_cap == $this->_insertcaptcha)? 1 : 0;
    }

    public function allfields()
    {
        return($this->_username && $this->_password && $this->_password2 && $this->_mail)? 1 : 0;
    }

    public function database()
    {
        mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
        mysql_select_db("romsilva") or die(mysql_error());

        $data = mysql_query("SELECT * FROM users WHERE username = '{$this->_username}' ");

        if (mysql_num_rows($data) == 0) {
            return true;
        } else
            return false;
    }

    public function istokenvalid()
    {
        return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
    }

    public function showerrors()
    {
        foreach($this->_errors as $key => $value) {
            echo $value . "<br>";
        }
    }
}

?>

Re: It won't insert values

Posted: Mon Jun 27, 2011 2:25 pm
by Jade
Your problem is that you're not inserting the values into the table in your insert() function. It should be

Code: Select all

public function insert()
    {
        mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
        mysql_select_db("romsilva") or die(mysql_error());
        $query = mysql_query("INSERT INTO users (username, password, admin, lastip, lastlogin) VALUES( '{$this->_username}', '{$this->_password}', '{$this->_admin}', '{$this->_ip}', '{$this->_data'}')") or die(mysql_error());
    }

Re: It won't insert values

Posted: Tue Jun 28, 2011 2:55 am
by Theramore
no thats not the problem...even with 0 value it should insert into the db value 0 but it does not insert even that value

Re: It won't insert values

Posted: Tue Jun 28, 2011 3:47 am
by Idri
What you should do is run through all functions being called and either echo something random (so you know that the code's going to right way), or output various data that you need at points to check if it's actually there.

In your code you use this if-statement

Code: Select all

if($this->messages())
However, take another look at your messages function. It does not return a value, thus it will always be false, so rather than calling the insert function it'll call the showerrors function instead. You'll have to either return true or false depending on the conditions the validation is in.

Also, the way your current exceptions are set up won't work, PHP will leave the try catch block once the an exception has been thrown, you'll have to either try catch them individually or reconsider whether you want to use exceptions or just normal string type error messages.

Re: It won't insert values

Posted: Tue Jun 28, 2011 4:59 am
by Theramore
I did this and now works, thanks Idri and others :

Code: Select all

    public function messages()
    {
        try {
            if (!$this->allfields())
                throw new Exception("Te rog completeaza toate campurile!");

            if ($this->istokenvalid())
                throw new Exception("Formular nevalidat! Te rog completeaza din nou formularul fara a da refresh paginii!");

            if (!$this->database())
                throw new Exception("Utilizatorul exista deja!");

            if (!$this->chkpass())
                throw new Exception("Cele doua parole nu se potrivesc");

            if (!$this->passleng())
                throw new Exception("Parola trebuie sa fie intre 6 si 25 de caractere!");

            if (!$this->captcha())
                throw new Exception("Cod captcha incorect!");

            mysql_connect("localhost", "ascent", "ppaass") or die(mysql_error());
            mysql_select_db("romsilva") or die(mysql_error());
            $query = mysql_query("INSERT INTO users (id, username, password, admin, lastip, lastlogin) VALUES('', '0', '0', '0', '0', '0')") or die(mysql_error());

            echo "Succes!";
        }
        catch(Exception $e) {
            $this->_errors[] = $e->getMessage();
        }
    }