Chat Application

Greetings,

I’ve been attempting to make a chatbox application for a couple hours now and I’ve run into a bit of a problem. I’ve setup so users can register an account to chat with, but the issue is that it’s not actually adding anything into my sql table. I’ve narrowed it down to one particular section but I can’t seem to figure out why it’s not working. If I could share emails with someone to help me figure this issue out, that would be awesome! Thanks!

-Kyal Cantwell

You could post the relevant code you are using.

[php] public function InsertUser(){
include “conn.php”;
$req=$bdd->prepare(“INSERT INTO users(UserName, UserMail, UserPassword) VALUES (:UserName, :UserMail, :UserPassword)”);

	$req->execute(array(
		'UserName'=>$this->getUserName(),
		'UserMail'=>$this->getUserMail(),
		'UserPassword'=>$this->getUserPassword()
		));
}[/php]

As you’re using mysqli or pdo you should wrap your application / queries in try catch blocks. that way you can catch any exceptions/errors thrown by the database.

Here’s my connection:

[php] try{

	$bdd = new PDO("mysql:host=localhost;dbname=chat","root","");
	
}catch(Exception $e){
	die("ERROR : ".$e->getMessage());
}

[/php]

and here’s the file that actually is supposed to insert the user:

[php]<?php
include’classes.php’;
$user = new user();

if(isset($_POST['UserName']) && isset($_POST['UserMail']) && isset($_POST['UserPassword'])){
	$user->setUserName($_POST['UserName']);
	$user->setUserMail($_POST['UserMail']);
	$user->setUserPassword(sha1($_POST['UserPassword']));
	$user->InsertUser();

	header('Location: ../index.php?success=1');
}

?>[/php]

I’m no expert with PHP, and I’ve had to watch some tutorials to get this far. I can understand a lot of it since I went over it again and again and tested bits and pieces out multiple times, but these few pieces stump me a little.

try to have the same try catch block (with the error handling) around the query (queries). It will allow you to see/log errors from pdo

It’s still telling me that the user has been successfully inserted into the table…but when I look, nothing is there.

Hum, I thought it should give you an error since you have a named placeholder mismatch.

[php]$req->execute(array(
‘UserName’=>$this->getUserName(),
‘UserMail’=>$this->getUserMail(),
‘UserPassword’=>$this->getUserPassword()
));[/php]

should really be

[php]$req->execute(array(
‘:UserName’=>$this->getUserName(),
‘:UserMail’=>$this->getUserMail(),
‘:UserPassword’=>$this->getUserPassword()
));[/php]

Also you shouldn’t use SHA1 as password storage, change to Bcrypt or PBKDF2 :slight_smile:

I tried doing this earlier:

[php]$req->execute(array(
‘:UserName’=>$this->getUserName(),
‘:UserMail’=>$this->getUserMail(),
‘:UserPassword’=>$this->getUserPassword()
));[/php]

But nothing changed :’(

Hm, just threw this together and you’re right, you don’t need the colons when sending in the params. It does however work either way here.

[php]<?php
try{
$bdd = new PDO(‘mysql:dbname=test;host=localhost;charset=utf8’, ‘test’, ‘Correct Horse Battery Staple’);

$req=$bdd->prepare(“INSERT INTO users(UserName, UserMail, UserPassword) VALUES (:UserName, :UserMail, :UserPassword)”);

$req->execute(array(
‘UserName’ => ‘JimL’,
‘UserMail’ => ‘[email protected]’,
‘UserPassword’ => ‘Correct Horse Battery Staple’
));
} catch (Exception $e) {
die("ERROR : ".$e->getMessage());
}
[/php]

Could you post the entire user class?

I still think it’s strange you don’t get any errors at all o.O

I think it might be easier if I just post my GitHub project of it here: https://github.com/Balthizar01/Otaku-Chat

Sorry for taking up your time like this! >.<

Great! much simpler to just clone it and see what’s wrong :slight_smile:

Uncomment the redirection in insertUser.php

[php]<?php
include’classes.php’;
$user = new user();

if(isset($_POST['UserName']) && isset($_POST['UserMail']) && isset($_POST['UserPassword'])){
	$user->setUserName($_POST['UserName']);
	$user->setUserMail($_POST['UserMail']);
	$user->setUserPassword(sha1($_POST['UserPassword']));
	$user->InsertUser();

	//header('Location: ../index.php?success=1'); <---------
}

?>[/php]

Then you’ll see the error :slight_smile:

I am ashamed of myself… D:

To miss such a simple correction >.< Thank you so much for the help! I’ll be sure to come to you next time I start having issues!

Well programming is pretty logic stuff really, so most errors tend to be trivial like this. (Far too) many times examining the code blind you from seeing small stuff, like this. Often it helps just to get someone else to look over it, and the issue can be solved in seconds.

Sponsor our Newsletter | Privacy Policy | Terms of Service