Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
//user.class.php
<?php
	
	Class User
	{
		private $errormessage = "";
		public function signup($POST)
		{
			$data = array();
			$db = Dbconnect::getInstance();
			$data['$user_firstname'] = trim($POST['sig_user_firstname']);
			$data['$user_lastname'] = trim($POST['sig_user_lastname']);
			$data['$user_email'] = trim($POST['sig_user_email']);
			$data['$user_pass'] = trim($POST['sig_user_pass']);
			$user_pass2 = trim($POST['sig_user_pass2']);
			$data['$user_phone_num'] = trim($POST['sig_user_phone_num']);

			if(empty($data['$user_email']) || !preg_match("/^[a-zA-Z_-]+@[a-zA-Z]+.[a-zA-Z]+$/", $data['$user_email']))
			{
				$this->errormessage .= "Please enter a valid email.";
			}
			if(empty($data['$user_firstname']) || !preg_match("/^[a-zA-Z]+$/", $data['$user_firstname']))
			{
			 	$this->errormessage .= "Please enter a valid first name.";
			}
			if(empty($data['$user_lastname']) || !preg_match("/^[a-zA-Z]+$/", $data['$user_lastname']))
			{
			 	$this->errormessage .= "Please enter a valid last name.";
			}
			if($data['$user_pass'] !== $user_pass2)
			{
				$this->errormessage .= "The password does not match.";
			}
			if(strlen($data['$user_pass']) < 5)
			{
				$this->errormessage .= "The password must be at least 5 characters long.";
			}
			if($this->errormessage == "")
			{
				
				$data['user_type'] = "customer";
				$data['user_id_url'] = $this->get_random_string(60);
				show($result);
				$query = "insert into user (user_id_url, user_firstname, user_lastname, user_email, user_pass, user_phone_num, user_type) values (:user_id_url, :user_firstname, :user_lastname, :user_email, :user_pass, :user_phone_num, :user_type)";
				
				$result = $db->write($query,$data);
				//show($result);
				if($result)
				{
					header("Location: ". ROOT ."login");
					die;
				}
			}
		}
		public function login($POST)
		{

		}
		public function get_user($url)
		{
			
		}
		private function get_random_string($strlen)
		{
			$ranArray = array('!','@','#','$','%','^','&','*','(',')',0,1,2,3,4,5,6,7,8,9,'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m','Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M');
			$rantext="";

			$strlen = rand(4,$strlen);
			for ($i=0; $i < $strlen; $i++) 
			{ 
				$rankey = rand(0,61);
				$rantext .= $ranArray[$rankey]; 
			}

			return $rantext;
		}
	}

PHP
//dbconnect.php
<?php
	//this is ensure that the website itself connects to the database
	Class Dbconnect
	{
		public static $conn;
		public function __construct()
		{
			try
			{
				$str = DB_TYPE . ":host=" . DB_HOST . ";dbname=" . DB_NAME;
				self::$conn = new PDO($str,DB_USER,DB_PASS);
			}
			catch (PDOException $error)
			{
				die($error->getMessage());
			}
		}

		public static function getInstance()
		{
			if(self::$conn)
			{
				//return self::$conn;
			}
 
			return $instance = new self();
		}
		public static function newInstance()
		{
			return $instance = new self();
		}
		//reading data from the database
		public function read($query, $data = array())
		{
			$statem = self::$conn->prepare($query);
			$result = $statem->execute($data);

			if($result)
			{
				$data = $statem->fetchAll(PDO::FETCH_OBJ);
				if(is_array($data))
				{
					return $data;
				}
			}
			return false;
		}
		//write data to database incl. delete/update
		public function write($query, $data = array())
		{
			$statem = self::$conn->prepare($query);
			$result = $statem->execute($data);

			if($result)
			{
				return true;
			}
			return false;
		}
	}

The error occurs when I tried to insert values into MySQL database

What I have tried:

I have tried changing the value names but no luck.
Posted
Updated 27-Jan-22 17:21pm
v2
Comments
_Asif_ 24-Jan-22 1:51am    
Should not this be
$data['user_type'] => $data['$user_type'] and
$data['user_id_url'] => $data['$user_id_url'] ?
Richard Deeming 24-Jan-22 6:51am    
You are storing your users' passwords in plain text. Don't do that!
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

PHP has built-in functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]

1 solution

Using named parameters your array of values needs to be bound to the names as described at PHP: PDOStatement::execute - Manual[^].
 
Share this answer
 
Comments
ImNew2this 28-Jan-22 5:27am    
"bindParam" did the trick and also some formatting was needed.

Thank you so much.
Richard MacCutchan 28-Jan-22 5:42am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900