Jump to content

mysql insert not working (new to php)


matgarland

Recommended Posts

I'm new to PHP and I'm pulling my hair out and really need help with using a class to first, check if a customer exists, and second, if customer doesn't exist, insert a new customer record. That is, a select statement followed by an insert statement in mysql. The first connection works fine, then the select statement works fine, the problem lies somewhere with the insert statement called by function CreateNewUser(), nothing happens. I mean, no error and no table insert, the SQL works fine executing inside phpMyAdmin but not from my calling code. One suspicion of mine is that the column name 'TYPE' may be a reserved MYSQL word but if that is the case, why would the select statement work!? Furthermore I can't find any information regarding 'TYPE' being a reserved MYSQL word -  Any help that can be offered would be much appreciated.

 

===========================================================================

here are the class functions i am calling:

===========================================================================

 

class Connection
{
static $connected = false;

        protected function Connect_Server()
{
	try
	{
		if($this->connected != true)
		{
			//	SUPRESS DATABASE ERRORS '@', HANDLE ERROR GRACEFULLY
			@ $conn = new mysqli($_SESSION['MYSQL_SERVER1'], "$_SESSION['MYSQL_PWD']", "$_SESSION['MYSQL_USER']", $_SESSION['MYSQL_DB1']);
			if(mysqli_connect_errno())
			{
				throw new Exception('Unable to connect to SQL Server at this point');
			}
		}
		$this->connected = true;
		return $conn;
	}
	catch (Exception $e)
	{
		$this->connected = false;
		echo $e->getMessage();
		exit;
	}
}

        public function CheckUserExists($username, $email_addr = "")
{
	$query = "select USERNAME, EMAIL from members where USERNAME ='" .$username."' or EMAIL ='".$email_addr. "'";
	try
	{
		//	IF NO AVAILABLE CONNECTION
		if($this->connected != true)
		{
			$db = $this->Connect_Server();
		}

		if($result = $db->query($query))
		{
			if ($db->errno)
			{
				throw new exception('Possibly corrupted data, please try again');
			}
			// THERE IS AT LEAST 1 MATCHING USER RECORD
			if(($num_rows = $result->num_rows) >= 1)
			{
				$row = $result->fetch_row();
				// EMAIL ADDRESS ALREADY EXISTS IN THE DATABASE
				if($row[0] == $username)
				{
					return 1;
				}
				// USERNAME ADDRESS ALREADY EXISTS IN THE DATABASE
				elseif($row[1] == $email_addr)
				{
					return 2;
				}
			}
			else	// THERE ARE NO MATCHING USER RECORDS
			{
				return 0;
			}
			//	CLOSE RESOURCES
			mysqli_free_result($result);
			$db->close();
			$this->connected = false;
		}
	}
	catch (Exception $e)
	{
		$db->close();
		$this->connected = false;
		echo $e->getMessage();
		exit;
	}
}

        public function CreateNewUser()
{

        // the following is an example of the data to be inserted, column 'TYPE' is an integer data type.
        $query = "insert into members (CREATED, FNAME, LNAME, COMPANY, POSITION, POSTAL1, POSTAL2, COUNTRY, STATE, CITY, PCODE, EMAIL, TELEPHONE, USERNAME, PWD, TYPE, MAINBUSINESS, BRANDS) values (now(),'1','1','1','1','1','1','1','1','1','1','1','1','1','1',0,'1','111')";

	try
	{
		//	IF NO AVAILABLE CONNECTION
		if($this->connected != true)
		{
			$db = $this->Connect_Server();
		}

		$result = mysqli_query($db,$query);

		if ($db->errno)
		{
			throw new Exception();
		}

		//	CLOSE RESOURCES
		mysqli_free_result($result);
		mysqli_close($db);
		$this->connected = false;
		return true;

	}
	catch (Exception $e)
	{
		mysqli_close($db);
		$this->connected = false;
		return false;
	}
}
}

 

 

===========================================================================

Here's the calling code:

===========================================================================

 

//  CLASS INSTANTIATION
        $user = new Connection;
$exists = $user->checkUserExists($_SESSION['USERNAME'],$_SESSION['EMAIL']);

if($exists == 1)
{
	unset($user);
	$_SESSION['FLAG'] = "userexists";
	throw new Exception();
}
elseif($exists == 2)
{
	unset($user);
	$_SESSION['FLAG'] = "emailexists";
	throw new Exception();
}

//	USE THE CURRENT $user CLASS INSTANCE

        //      *********** this is where the problem lies ***********
$newuser = $user->CreateNewUser();
unset($user);

//	IF UNABLE TO CREATE USER, SEND BACK TO FORM
if(!$newuser)
{
	$_SESSION['FLAG'] = "dataerror";
	throw new Exception();
}
header("Location: http://localhost/Websites/reg_response.php");

}
catch (Exception $e)
{
reset($_POST);
while (list($key,$val) = each ($_POST))
{
	$_SESSION[$key] = trim($val);
}
header("Location: http://localhost/Websites/user_registration.php");
exit;
}

Link to comment
https://forums.phpfreaks.com/topic/85739-mysql-insert-not-working-new-to-php/
Share on other sites

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.