Hi I get my data posted from another form.html page i receive it just fine and decode the json just fine, and the validation works just fine, the only problem is the part where i have to insert the values into the database.. any suggestions..

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
<title>User Details</title>
</head>
<body >
<?php
//dealing with json
$bla = $_REQUEST['jsonstring'];
$decoded = json_decode($bla);

//request pulls values through
$username = $decoded->username;
$email = $decoded->email;
$firstname = $decoded->firstname;
$surname = $decoded->surname;
$birthday = $decoded->birthday;
$password = $decoded->password;
$pwdconfirm = $decoded->pwdconfirm;
$gender = $decoded->gender;
$category = "not valid"; 
//basic validation
if($username == ""){
	echo "enter a username!! ";
}
if($surname == ""){
	echo "enter a surname!! ";
}
if($email == ""){
	echo "enter an email!! ";
}
if($firstname == ""){
	echo "enter a firstname!! ";
}
if($birthday == ""){
	echo "enter a yearborn!! ";
}
if($password == ""){
	echo "enter a password!! ";
}
if($pwdconfirm == ""){
	echo "enter a confirm password!! ";
}
if($username == ""){
	echo "enter a username!! ";
}
if($gender == "nothing"){
	echo "enter a gender!! ";
}
if($password == $pwdconfirm){}
else{
	echo "Passwords don't match ";
}

//code to do pre-category formatting
$fl = substr($surname, 0, 1);
$flu = strtoupper($fl);
$cat = ord($flu);

//calls cat function
cat($cat);

//function to work out category
function cat($cat){
	global $category;
	switch (true){
		case  ($cat >= '65' && $cat <= '67'):
			$category = "A-C";
			//echo "A-C";
			break;
		case  ($cat > '67' && $cat <= '70'):
			$category = "D-F";
			//echo "D-F";
			break;
		case  ($cat > '70' && $cat <= '73'):
			$category = "G-I";
			//echo "G-I";
			break;
		case  ($cat > '73' && $cat <= '76'):
			$category = "J-L";
			//echo "J-L";
			break;
		case  ($cat > '76' && $cat <= '79'):
			$category = "M-O";
			//echo "M-O";
			break;
		case  ($cat > '79' && $cat <= '82'):
			$category = "P-R";
			//echo "P-R";
			break;
		case  ($cat > '82' && $cat <= '85'):
			$category = "S-U";
			//echo "S-U";
			break;
		case  ($cat > '85' && $cat <= '86'):
			$category = "V-Z";
			//echo "V-Z";
			break;
		default:
			$category = "Null";
			echo "letter out of range!!";
	}
}
//here is where the problem lies, i think****************
	$con = mysql_connect(localhost,root);
	if(!$con){
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db('dbuser', $con);
	mysql_query("INSERT INTO tbusers (username, email, password, firstname, surname, category, gender, birthday)
	VALUES('$username','$email','$password','$firstname','$surname','$category','$gender','$birthday')");
	
	/*$result = mysql_query("SELECT * FROM tbusers");

	echo "<table border='1'>
	<tr>
	<th>username</th>
	<th>email</th>
	<th>password</th>
	<th>firstname</th>
	<th>surname</th>
	<th>category</th>
	<th>gender</th>
	<th>birthday</th>
	</tr>";

	while($row = mysql_fetch_array($result)){
		echo "<tr>";
		echo "<td>" . $row['username'] . "</td>";
		echo "<td>" . $row['email'] . "</td>";
		echo "<td>" . $row['password'] . "</td>";
		echo "<td>" . $row['firstname'] . "</td>";
		echo "<td>" . $row['surname'] . "</td>";
		echo "<td>" . $row['category'] . "</td>";
		echo "<td>" . $row['gender'] . "</td>";
		echo "<td>" . $row['birthday'] . "</td>";
		echo "</tr>";
		}
	echo "</table>";*/
	
	mysql_close($con);
?>
</body>
</html>

Recommended Answers

All 4 Replies

Is it connecting to the database ok? there isn't a password on that connect statement.

mysql_query("INSERT INTO tbusers (username, email, password, firstname, surname, category, gender, birthday)
VALUES('$username','$email','$password','$firstname','$surname','$category','$gender','$birthday')");

add this to it(just to see where the error is coming from):

mysql_query("INSERT INTO tbusers (username, email, password, firstname, surname, category, gender, birthday)
VALUES('$username','$email','$password','$firstname','$surname','$category','$gender','$birthday')") or die("Could not add to database beacuse: ".mysql_error());

Always add or die(mysql_error()); at the end of your queries just to see where the errors come from.

You need to encode localhost and root with double quotes as below

$con = mysql_connect("localhost","root");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.