Reputation: 151
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>
Upvotes: 0
Views: 48
Reputation: 3284
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.
Upvotes: 2
Reputation: 4635
PLease execute your query like
mysqli_query($newaccount) or die(mysqli_error());
Upvotes: 1