EdwardJ Posted November 27, 2008 Share Posted November 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/ Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 You never actually do anything with the User class. You need to call adduser(). Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-700626 Share on other sites More sharing options...
EdwardJ Posted November 28, 2008 Author Share Posted November 28, 2008 yeah youre right. so like? $user = new User(); $user->addUser($this->username, $this->password......) is this correct? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-700826 Share on other sites More sharing options...
EdwardJ Posted November 28, 2008 Author Share Posted November 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-701139 Share on other sites More sharing options...
Mastodont Posted November 28, 2008 Share Posted November 28, 2008 You do not have initialized $this->connection Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-701160 Share on other sites More sharing options...
KevinM1 Posted November 28, 2008 Share Posted November 28, 2008 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']); Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-701162 Share on other sites More sharing options...
EdwardJ Posted November 28, 2008 Author Share Posted November 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-701207 Share on other sites More sharing options...
corbin Posted November 28, 2008 Share Posted November 28, 2008 1. But it's never set in your User class! 2. Uh, how else are you going to get user input? Some magical OOP way? Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-701225 Share on other sites More sharing options...
DarkWater Posted November 28, 2008 Share Posted November 28, 2008 You completely misunderstand OOP if you're using code like that. I'd suggest reading up some more. Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-701357 Share on other sites More sharing options...
trq Posted November 29, 2008 Share Posted November 29, 2008 You completely misunderstand OOP if you're using code like that. I'd suggest reading up some more. Id agree. Your really need to graqsp the concept of functions first too before moving on to classes / objects. Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-701388 Share on other sites More sharing options...
corbin Posted November 29, 2008 Share Posted November 29, 2008 I was being nice and didn't say that, which is weird for me. Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-701533 Share on other sites More sharing options...
EdwardJ Posted November 30, 2008 Author Share Posted November 30, 2008 hey no harm done by saying that right? i think im gonna go with procedural programming this time and move up to oop once i get more experienced. after all this is the first app i write in php, and the first one i do in a long time, so... Quote Link to comment https://forums.phpfreaks.com/topic/134532-add-account-class-question/#findComment-702231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.