OK. I’m new to Classes in PHP and trying to pass variables into the protected functions in the class. How do I do this?
CLASSES.PHP
<?php
include($_SERVER['DOCUMENT_ROOT']."/includes/con.php");
class gindex {
protected function rdev($a,$b,$c,$d){
$d = base64_encode($d);
mysql_query("INSERT INTO mem(first_name,last_name,email,password,type) VALUES(".$a.",".$b.",".$c.",".$d.",'developer')", $db);
}
?>
INDEX.PHP
<?php
include($_SERVER['DOCUMENT_ROOT']."/includes/con.php");
if(isset($_POST['developerbtn'])){
$fname = $_REQUEST['fname'];
$lname = $_REQUEST['lname'];
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
$Cgindex = new gindex();
$Cgindex->rdev($fname,$lname,$email,$password);
}
?>
You cannot do that because the method is set to
protectedyou have to set it topublicto be able to access it. Otherwise you can only call it from the same class or the children of that class.May I also suggest to start your classname with an uppercase:
class Gindex. You may also want to improve the name of the class becausegindexsays nothing of what it does. Same goes for yourmethodname. And your parameters names are also terrible. Name things correctly so people (including) yourself will know exactly what a variable contains / class or method does when they are (re)viewing your code.You are also using a variable
$dbwhich isn’t defined anywhere in the scope of the class.Also please, don’t use
mysql_*functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can’t decide, this article will help to choose. If you care to learn, here is a good PDO tutorial.This will also fix that nasty SQL Injection you have there in your code. For more information about how to fix this with either PDO or mysqli see this question.
It also looks like you are using the website as the password when inserting the data into the database. If you are really going to store database I also suggest you read into hashing the password for security.