It won't insert values
Posted: Mon Jun 27, 2011 7:15 am
I got no errors on this code it just doesn't inserts the values into mysql anyone knows why?
and here's the register.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;
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>