0

I have a simple code that is supposed to add data to a MySQL table through PHP. I have code that checks if the machine manages to connect to the database and everything passes successfully however it's not working. Can anyone help me found the problem?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$mysqlhost="host";
$mysqldatabase="database";
$mysqlusername="username";
$mysqlpassword="password";
$connect=new mysqli($mysqlhost,$mysqlusername,$mysqlpassword);
if (!$connect) {
    die("Connection failed: " . $connect->connect_error);
}
echo "Connected Successfully";
mysqli_select_db($connect,"b33_15887129_Accounts");
$username=$_POST['username'];
$password=$_POST['Password'];
$email=$_POST['Email'];
$gender=$_POST['Gender'];
$tutorgroup=$_POST['tutorgroup'];
	$newaccount="INSERT INTO Users (Username,Password,Gender,Email,Tutor Group)
	VALUES('$username','$password','$email','$gender','$tutorgroup')";
if(!$newaccount){
	die("Failed to create new account.");
	
	}else{
		echo "New Account Successfully Created!";
		
		}
?>
</body>
</html>

1
  • 1
    Tutor Group a single column name ? try wrapping them in backtics ` ` Commented Feb 26, 2015 at 10:23

2 Answers 2

2

The problem seems to be that you are not executing your query. After storing the query in $newaccount, you should replace the if-statement that follows with that one:

if(!$connect->query($newaccount))
    die("Failed to create new account. DB Error: [" . $connect->error . "]");
else
    echo "New Account Successfully Created!";

Additionally, if you do it like I described, any error message that results from an unsuccessful query will be displayed.

Check here for an easy tutorial on how to use mysqli properly.

Sign up to request clarification or add additional context in comments.

3 Comments

After using your code i got the failed message. The reason was: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group) VALUES(values here) Any ideas?
That's probably because of the space in your column name. Replace Tutor Group with `Tutor Group` in your query and it should work.
Thanks for your help. Worked
1

PLease execute your query like

mysqli_query($newaccount) or die(mysqli_error());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.