Jump to content

Add account class question


EdwardJ

Recommended Posts

I am trying to build an account system, here are the files:

new_user.php

<form action="process/new_user_process.php" method="post">
<tr>
<td>Nombre de usuario:</td><td><input type="text" name="username" maxlength="30"></td>
</tr>
<tr>
<td>Contraseña: </td><td><input type="password" name="password" maxlength="30"></td>
</tr>
<tr>
<td>Re-escribir contraseña: </td><td><input type="password" name="repassword" maxlength="30"></td>
</tr>
<tr>
<td>Nombre: </td><td><input type="text" name="nombre" maxlength="30"></td>
</tr>
<tr>
<td>Apellido: </td><td><input type="text" name="apellido" maxlength="30"></td>
</tr>
<tr>
<td>Correo electronico: </td><td><input type="text" name="email" maxlength="30"></td>
</tr>
<tr><td><input type="submit" name="submit" value="Aceptar"></td></tr>

</form>

 

And here is the script containing the class:

new_user_process.php

<?php require_once("includes/connection.php"); 

class User{
function addUser($username, $hash_password, $nombre, $apelllido, $email, $userlevel){
	$this->username = mysql_real_escape_string($_POST['username']);
	$this->hash_password = sha1($_POST['hash_password']);
	$this->nombre = $_POST['nombre'];
	$this->apellido = $_POST['apellido'];
	$this->email = $_POST['email'];
	$this->userlevel = $_POST['userlevel'];
	$this->query = "INSERT INTO users  
			VALUES ({$this->username}, {$this->hashed_password}, {$this->nombre}, {$this->apellido},
			{$this->email}, {$this->userlevel})";
	return mysql_query($this->query, $this->connection);
}
}
$user = new User();
?>

the second file is contained in the process subfolder, that's why the form action is process/new_user_process.php

The problem is the data is not being entered into the table.

 

Thanks very much

Link to comment
https://forums.phpfreaks.com/topic/134532-add-account-class-question/
Share on other sites

Im using a class to insert fields into a table:

<?php class User{
public function addUser($username, $hash_password, $nombre, $apelllido, $email, $userlevel){
	$this->username = mysql_real_escape_string($_POST['username']);
	$this->hash_password = sha1($_POST['hash_password']);
	$this->nombre = $_POST['nombre'];
	$this->apellido = $_POST['apellido'];
	$this->email = $_POST['email'];
	$this->userlevel = $_POST['userlevel'];
	$this->query = "INSERT INTO users  
			VALUES ({$this->username}, {$this->hashed_password}, {$this->nombre}, {$this->apellido}, {$this->email}, {$this->userlevel});";
	return mysql_query($this->query, $this->connection);
}
}
$user = new User();
$user->addUser($this->username, $this->hash_password, $this->nombre, $this->apellido, $this->email, $this->userlevel);
?>

 

Im getting "Using $this when not in object context"

 

When I try to pass the method arguments as:

<?php$user = new User();
$user->addUser($username, $hash_password, $nombre, $apellido, $email, $userlevel);?>

 

I get "mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\vanvien\new_user_process.php on line 14"

 

:'( :'( I am very frustrated... please help!

Thanks

 

This line:

 

$user->addUser($this->username, $this->hash_password, $this->nombre, $this->apellido, $this->email, $this->userlevel);

 

Is the source of your problem.  Like the error says, you can't use '$this' outside of an object.  What you should be doing is the following:

 

class User
{
   public function addUser($userName, $hashPassword, $nombre, $apellido, $email, $userLevel)
   {
      $this->username = mysql_real_escape_string($userName);
      $this->hash_password = sha1($hashPassword);
      $this->nombre = $nombre;
      $this->apellido = $apellido;
      $this->email = $email;
      $this->userlevel = $userLevel;
      $this->query = "INSERT INTO users VALUES ({$this->username}, {$this->hash_password}, {$this->nombre}, {$this->apellido}, {$this->email}, {$this->userlevel});";

      return mysql_query($this->query, $this->connection);
   }
}

$user = new User();
$user->addUser($_POST['username'], $_POST['hash_password'], $_POST['nombre'], $_POST['apellido'], $_POST['email'], $_POST['userlevel']);

1. $this->connection is included at the top in an external file, just forgot to paste it.

 

2. using $POST[''] I think the hole concept of oop is lost. Besides for example, the record added in the hash_password field would not have the encryption sha1().

 

And using your solution I still get "mysql_query(): supplied argument is not a valid MySQL-Link resource".

 

I really appreciate you guys trying to help me

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.