Simple PHP application

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Simple PHP application

Post by gautamz07 »

This is a simple php chat application , it gives me the message "Message failed to send "

Here are all the files

connect.db.php

Code: Select all

<?php

	$db_host = 'localhost';
	$db_user='root';
	$db_pass='';

	$db_name= 'chat';

	if($connection = mysql_connect($db_host, $db_user, $db_pass)){

		$feedback[] = "connected to database server <br/>";

		if($database =mysql_select_db($db_name , $connection)){
			$feedback[] = 'database has been selected .. <br/>';
		}else{
			$feedback[] = 'database was not found <br/>';
		}
	}else{
		$feedback[] = 'unable to connect to SQL server  .. <br/>';
	}

?>
core.inc.php

Code: Select all

<?php

$feedback = array();
require('includes/database/connect.db.php');
require('includes/functions/chat.func.php');


?>
chat.func.php

Code: Select all

<?php

function get_msg(){

	$query = "SELECT `Sender` , `Message` FROM `chat`.`chat` ORDER BY `Msg_ID` DESC";

	$run = mysql_query($query);

	$messages = array();

	while($message = mysql_fetch_assoc($run)) {

		$messages[] = array('sender'=>$message['Sender'],
							'message'=>$message['Message']);
	}

	return $messages;
}

function send_msg($sender , $message){

	if(!empty($sender) && !empty($message)){

		$sender = mysql_real_escape_string($sender);
		$message= mysql_real_escape_string($message);

		$query = "INSERT INTO  `chat`.`chat` VALUES (null , '{$sender}' , '$message')";

		if($run = mysql_query($query)){
			return true;
		}else{
			return false;
		}
	}
	else{
		return false;
	}
}


?>
Chat.php

Code: Select all

<?php
	require('../../includes/database/connect.db.php');
	require('../../includes/functions/chat.func.php');

	$messages = get_msg();

				foreach($messages as $message) {

					echo '<strong>'.$message['sender'].'</strong>'.' <em>Sent</em><br/>';
					echo $message['message'].'<br/><br/>' ;
				}
?>
and Index.php

Code: Select all

<?php

	require('includes/core.inc.php');
	

	if(isset($_POST['send'])){
		if(send_msg($_POST['sender'] , $_POST['message'])){
			echo "message sent .";
		}else {
			echo "Message failed to send .";
		}
	}
	
?>
<DOCTYPE html>
<html lang="en">
	<head>
		<title>Chat Application</title>

		<link rel="stylesheet" type="text/css" href="public/css/main.css"/>

	</head>

	<body>

		<form action="index.php" method="post">
			<label>Enter Name :<input type="text" name="sender"/></label>
			<label>Enter Message :<input type="text" name="message"></label><br/>
			<input type="submit" name="send" value="Send Message">
		</form>

		<div id="messages">

		</div><!-- Messages-->

		<!-- Javascript -->

		<script type="text/javascript" src="scripts/js/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="scripts/js/auto_chat.js"></script>

		


	</body>
</html>

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Simple PHP application

Post by Celauran »

So send_msg is returning false. Do both parameters contain data? If not, there's the problem. If so, your SQL is failing. Check mysql_error()
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Simple PHP application

Post by gautamz07 »

well the actually , i needed to create the database again , i donno why it got deleted .
Now the problem is that in message is not displaying in the browser , its inserting the messages in the database
but not displaying in the browser .

Thank you gautam .
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Simple PHP application

Post by gautamz07 »

well got it all working now the problem was tat i forgot to add the auto_chat.js file .

Thank you anyway.
Post Reply