It won't insert values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

It won't insert values

Post 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>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: It won't insert values

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: It won't insert values

Post by Theramore »

thanks a lot
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: It won't insert values

Post by Theramore »

nope it does not works
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: It won't insert values

Post by Jade »

Show us your updated code
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: It won't insert values

Post 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.';
                }        
        }

?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: It won't insert values

Post 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>";
        }
    }
}

?>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: It won't insert values

Post 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());
    }
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: It won't insert values

Post 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
Idri
Forum Newbie
Posts: 19
Joined: Sun May 29, 2011 9:21 am

Re: It won't insert values

Post 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.
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: It won't insert values

Post 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();
        }
    }
Post Reply