Page 1 of 1

PHP SQL Code

Posted: Tue Oct 15, 2013 2:57 pm
by tazz24mania
Hello, im starting out with a basic php sql code and I will be building on it. However upon testing I have found that It is refusing to write to the database. Can some one please check over the code and tell me if its all correct.

Code: Select all

<?php

$f_name = $_POST['firstname'];
$l_name = $_POST ['lastname'];
$username = $_POST['username'];
$pass1 = $_POST['password'];
$pass2 = $_POST['password2'];
$date = date('m/d/Y h:i:s', time());
$domain = 'mydomain.org.uk'; //Insert Domain here.

include ('inc/db/conn.php');

$hash = md5($pass1);
$hash2 = md5($pass2);

if ($hash == $hash2) {
	$mysql = "INSERT INTO `hm_accounts`(
	`accountdomainid`, 
	`accountadminlevel`, 
	`accountaddress`, 
	`accountpassword`, 
	`accountactive`, 
	`accountisad`, 
	`accountaddomain`, 
	`accountadusername`, 
	`accountmaxsize`, 
	`accountvacationmessageon`, 
	`accountvacationmessage`, 
	`accountvacationsubject`, 
	`accountpwencryption`, 
	`accountforwardenabled`, 
	`accountforwardaddress`, 
	`accountforwardkeeporiginal`, 
	`accountenablesignature`, 
	`accountsignatureplaintext`, 
	`accountsignaturehtml`, 
	`accountlastlogontime`, 
	`accountvacationexpires`, 
	`accountvacationexpiredate`, 
	`accountpersonfirstname`, 
	`accountpersonlastname`) 
	VALUES (
		'2',
		'0',
		'$username@$domain',
		'',
		'1',
		'0',
		'',
		'',
		'0',
		'0',
		'0',
		'0',
		'3',
		'0',
		'',
		'0',
		'0',
		'',
		'',
		'$date',
		'0',
		'$date',
		'$f_name',
		'$l_name')";
}
else {
	
	echo "Passwords do not match. Please try again";
	
	
}

mysql_close()

?>

Re: PHP SQL Code

Posted: Tue Oct 15, 2013 2:59 pm
by tazz24mania
Quick update... I am able to get the SQL code to work if I enter it directly as a syntax into mysql using phpmyadmin

Re: PHP SQL Code

Posted: Tue Oct 15, 2013 3:04 pm
by Celauran
You're writing out the query but never executing it. Also, mysql_ functions have been deprecated. Look into PDO.

Re: PHP SQL Code

Posted: Tue Oct 15, 2013 3:26 pm
by tazz24mania
Ok so i looked into PDO but im still trying to get my head round it.

here is the updated code. Can this be checked

Code: Select all

<?php

$f_name = $_POST['firstname'];
$l_name = $_POST ['lastname'];
$username = $_POST['username'];
$pass1 = $_POST['password'];
$pass2 = $_POST['password2'];
$date = date('m/d/Y h:i:s', time());
$domain = 'mydomain.org.uk'; //Insert Domain here.

include ('inc/db/conn.php');

$hash = md5($pass1);
$hash2 = md5($pass2);

if ($hash == $hash2) {
	$affected_rows = $db->exec("INSERT INTO `hm_accounts`(
	`accountdomainid`, 
	`accountadminlevel`, 
	`accountaddress`, 
	`accountpassword`, 
	`accountactive`, 
	`accountisad`, 
	`accountaddomain`, 
	`accountadusername`, 
	`accountmaxsize`, 
	`accountvacationmessageon`, 
	`accountvacationmessage`, 
	`accountvacationsubject`, 
	`accountpwencryption`, 
	`accountforwardenabled`, 
	`accountforwardaddress`, 
	`accountforwardkeeporiginal`, 
	`accountenablesignature`, 
	`accountsignatureplaintext`, 
	`accountsignaturehtml`, 
	`accountlastlogontime`, 
	`accountvacationexpires`, 
	`accountvacationexpiredate`, 
	`accountpersonfirstname`, 
	`accountpersonlastname`) 
	VALUES (
		'2',
		'0',
		'$username@$domain',
		'',
		'1',
		'0',
		'',
		'',
		'0',
		'0',
		'0',
		'0',
		'3',
		'0',
		'',
		'0',
		'0',
		'',
		'',
		'$date',
		'0',
		'$date',
		'$f_name',
		'$l_name')");
$affected_rows = $stmt->rowCount();
}
else {
	
	echo "Passwords do not match. Please try again";
	
	
}

mysql_close()

?>

Re: PHP SQL Code

Posted: Tue Oct 15, 2013 3:27 pm
by tazz24mania
and heres the connection script im using

Code: Select all

<?php

$db = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Re: PHP SQL Code

Posted: Tue Oct 15, 2013 4:10 pm
by Celauran
What errors are you getting?

Re: PHP SQL Code

Posted: Thu Oct 17, 2013 10:56 am
by tazz24mania
sry... i had to go back to the old code as my server has olod php and sql