PHP SQL Code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tazz24mania
Forum Newbie
Posts: 10
Joined: Tue Oct 15, 2013 2:53 pm
Location: Newcastle Upon Tyne

PHP SQL Code

Post 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()

?>
tazz24mania
Forum Newbie
Posts: 10
Joined: Tue Oct 15, 2013 2:53 pm
Location: Newcastle Upon Tyne

Re: PHP SQL Code

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP SQL Code

Post by Celauran »

You're writing out the query but never executing it. Also, mysql_ functions have been deprecated. Look into PDO.
tazz24mania
Forum Newbie
Posts: 10
Joined: Tue Oct 15, 2013 2:53 pm
Location: Newcastle Upon Tyne

Re: PHP SQL Code

Post 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()

?>
tazz24mania
Forum Newbie
Posts: 10
Joined: Tue Oct 15, 2013 2:53 pm
Location: Newcastle Upon Tyne

Re: PHP SQL Code

Post 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);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP SQL Code

Post by Celauran »

What errors are you getting?
tazz24mania
Forum Newbie
Posts: 10
Joined: Tue Oct 15, 2013 2:53 pm
Location: Newcastle Upon Tyne

Re: PHP SQL Code

Post by tazz24mania »

sry... i had to go back to the old code as my server has olod php and sql
Post Reply