- Home /
Mysql PDO Register user insert not inserting, no error feedback
Hello, I have my connection to database working, as i can check if a username already exists in my database, i also have populated a drop down list with options from my database, my issue is with registering a new user i.e. inserting the $_POST data into my table.
PHP is not sending an error i can research, i have tried the binding values method of PDO with no luck.Also strangely earlier i got a my php file to echo 0 at the files end an execute my debug.log for user added in my c# as www.text = 0 (see code), but no user was actually added to my database. Now i am stuck with no user insert, no error data from the php file to www.text, I have tried all day. Any help would be appreciated.
Here is the relevant code:
MYSQL TABLE:
CREATE TABLE `players` (
`id` int(10) NOT NULL,
`country_id` smallint(11) UNSIGNED NOT NULL,
`language_id` smallint(11) UNSIGNED NOT NULL,
`first_name` varchar(50) CHARACTER SET utf8 NOT NULL,
`last_name` varchar(50) CHARACTER SET utf8 NOT NULL,
`email` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
`hash` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`salt` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`password_hint` text CHARACTER SET utf8 NOT NULL,
`score` int(10) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `players`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `username` (`username`),
ADD KEY `country_id` (`country_id`),
ADD KEY `language_id` (`language_id`);
CONNECT.PHP:
<?php
$host ='localhost';
$user ='XYZ';
$dbpassword = 'XYZ';
$dbname = 'XYZ';
//Set DSN
$dsn = 'mysql:host='. $host.';dbname='. $dbname;
//create PDO instance
$pdo = new PDO($dsn, $user, $dbpassword);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// THIS ERROR MODE IS ON
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
PHP CODE: register.php
<?php
require 'connect.php'; //WORKING
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$username = $_POST["username"];
$password = $_POST["password"];
$password_hint = $_POST["password_hint"];
//input from dropdowns
$country_id = $_POST['country'];
$language_id = $_POST['language'];
$namecheckquerry = 'SELECT username FROM players WHERE username = ?';
$stmt = $pdo->prepare($namecheckquerry);
$stmt->execute([$username]);
$nameCount = $stmt->rowCount();
if($nameCount > 0){
echo "3. A player with that Username already exists";
exit();
}
//add user to table
$salt = "\$5\$rounds=5000\$". "andrewjackson" . $username ."\$";
$hash = crypt($password, $salt);
$insertuserqry = 'INSERT INTO players(first_name, last_name, username, hash, salt)
VALUES (:first_name, :last_name, :username, :hash, :salt)';
$stmt = $pdo->prepare($insertuserqry);
// ERROR STATEMENT NOT SENDING TO WWW.TEXT
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
$stmt->execute(['first_name'=> $first_name , 'last_name'=> $last_name , 'username'=>
$username, 'hash'=> $hash, 'salt'=> $salt]);
echo '0';
?>
C# CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class registerme_new : MonoBehaviour
{
public InputField firstnamefield;
public InputField lastnamefield;
public InputField usernamefield;
public InputField passwordfield;
public InputField passwordhintfield;
public Dropdown countryField;
public Dropdown languageField;
public Text text;
public Button submitButton;
IEnumerator Start ()
{
//Connect to Database and get list of possible player registration countries
WWW countryRequest = new WWW("http://www.mysite.com/get_country_list.php");
yield return countryRequest;
//string[] webResults = countryRequest.text.Split(',');
List<string> countryList = new List<string>(countryRequest.text.Split(','));
countryField.AddOptions(countryList);
//Connect to Database and get list of possible player languages
WWW languageRequest = new WWW("http://www.mysite.com/get_language_list.php");
yield return languageRequest;
//string[] webResults = languageRequest.text.Split(',');
List<string> languageList = new List<string>(languageRequest.text.Split(','));
languageField.AddOptions(languageList);
}
public void CallRegister() {
StartCoroutine(Register());
}
IEnumerator Register()
{
WWWForm form = new WWWForm();
form.AddField("country", countryField.value);
form.AddField("language", languageField.value);
form.AddField("first_name", firstnamefield.text);
form.AddField("last_name", lastnamefield.text);
form.AddField("username", usernamefield.text);
form.AddField("password", passwordfield.text);
form.AddField("password_hint", passwordhintfield.text);
WWW www = new WWW("http://www.mysite.com/register.php", form);
yield return www;
if (www.text == "0")
{
Debug.Log("User created succesfully");
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
}
else {
// THIS IS THE ERROR I AM GETTING WITH NOTHING FROM WWW.TEXT
Debug.Log("User creation failed. Error #"+ www.text);
}
}
}