Jump to content

New To Php/MYSQLi


Seanphpwannabe

Recommended Posts

Hi PHP'ers

 

Working on learning as much as I can and thought I would try to make a little chat program. 

 

I have 4 main files. 

 

index.php

<?php 
	require ('db/connect.db.php');

	if(isset($_POST['send'])){
		if (send_msg($_POST['sender'], $_POST['message'])){
			echo "Message Sent";
		} else {
			echo "Message Failed To Send";
		}
	}

?>

<div id="messages">
<?php
$messages = get_msg();
foreach ($message as $message) {
	echo $message['sender']."Sent<br>";
	echo $message['message']."<br>";
}
?>
</div>
<form action="index.php" method="post">
<labal>Enter Name: <input type="text" name="sender"></labal>
<label>Enter Message:<input type="text" name="message"></label>
<input type="submit" name="send" value="Send Message">
</form>

chat.func.php

<?php 

	function get_msg(){
		$query = "SELECT 'sender','Message' FROM 'chat'.'chat'";

		$run = mysqli_query($query);

		$message = arrary();

		while($message = mysqli_fetch_assoc($run)) {
			$message[] =array('sender'=>$message['Sender'],
							  'message'=>$message['Message']);
		}
		return $messages;
	}

	function send_msg($sender, $message) {
		if(!empty($sender) && !empty($message)){

			$sender = mysqli_real_escape_string($sender);
			$message = mysqli_real_escape_string($message);

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

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

		} else {
			return false;
		}
	}


 ?>

core.inc.php

<?php 

	require ('db/connect.db.php');
	require ('function/chat.func.php');	



 ?>

and final connect.db.php

<?php 

	$servername = "localhost";
	$username = "root";
	$password = "";
	$dbname = "chat";

	$conn = new mysqli($servername,$username,$password,$dbname);

	if ($conn) {
		echo "Connection Success";
	} else {
		echo "Connection Failed:" . mysql_errno();
	}
?>

When I try to run the script I get the following error

 

Fatal error: Uncaught Error: Call to undefined function get_msg() in C:\xampp\htdocs\ChatAPP\index.php:16 Stack trace: #0 {main} thrown in C:\xampp\htdocs\ChatAPP\index.php on line 16

 

I am not 100% sure what I have done. Should I not have this as a function or is it more than that. 

 

Please help when you can. 

 

Thank you,

Sean

Link to comment
Share on other sites

Well, when you run index.php, ONLY the code in index.php is run. You "require" connect.db.php at the beginning, so that code is run. Then the rest of index.php.

 

Where is get_msg() defined, if only those two files are run? How is PHP supposed to know get_msg() (and send_msg(), as you'll find out) is in the chat.func.php file?

 

Hint, it's the same way PHP knows what's in the connect.db.php file...

 

-John

Link to comment
Share on other sites

Hi Sepodati.

 

Okay in the index.php file I have now require to the file

 

I am now getting the following 

Connection Success


Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\ChatAPP\functions\chat.func.php on line 6

Fatal error: Uncaught Error: Call to undefined function arrary() in C:\xampp\htdocs\ChatAPP\functions\chat.func.php:8 Stack trace: #0 C:\xampp\htdocs\ChatAPP\index.php(17): get_msg() #1 {main} thrown in C:\xampp\htdocs\ChatAPP\functions\chat.func.php on line 8
Link to comment
Share on other sites

Hello again. 

 

So I think I figured something out so I will repost all again. 

 

index.php

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

	if(isset($_POST['send'])){
		if (send_msg($_POST['sender'], $_POST['message'])){
			echo "Message Sent";
		} else {
			echo "Message Failed To Send";
		}
	}

?>

<div id="messages">
<?php
$messages = get_msg();
foreach($message as $message) {
	echo $message['sender']."Sent<br>";
	echo $message['message']."<br>";
}
?>
</div>
<form action="index.php" method="post">
<labal>Enter Name: <input type="text" name="sender"></labal>
<label>Enter Message:<input type="text" name="message"></label>
<input type="submit" name="send" value="Send Message">
</form>

chat.func.php page 

<?php 

	function get_msg() {
		$query = "SELECT * 'Sender','Message' FROM 'chat'";

		//$run = mysqli_query($query);

		$message = arrary();

		while($message = mysqli_fetch_assoc($run)) {
			$message[] =array('sender'=>$message['Sender'],
							  'message'=>$message['Message']);
		}
		return $messages;
	}

	function send_msg($sender, $message) {
		if(!empty($sender) && !empty($message)){

			$sender = mysqli_real_escape_string($sender);
			$message = mysqli_real_escape_string($message);

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

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

		} else {
			return false;
		}
	}


 ?>

Seem's there is a issue on this page which is causing this error 

Fatal error: Uncaught Error: Call to undefined function arrary() in C:\xampp\htdocs\ChatAPP\functions\chat.func.php:8 Stack trace: #0 C:\xampp\htdocs\ChatAPP\index.php(17): get_msg() #1 {main} thrown in C:\xampp\htdocs\ChatAPP\functions\chat.func.php on line 8"

 

I am trying so hard to figure this out. I really do not know what I have done wrong. 

 

PLEASE, someone help. 

 

The index.php and chat.func.php are the only two pages I have changed. 

 

Thanks

Sean

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.