KodeInfo | Learning resources for web and mobile development

PHP Message System

October 1st, 2014 17:18:12 by Irfan Iqbal Comments(0) - Views(5777)

In this post let us see how to send messages between two users for that we need to create a table with the following attributes.

  1. user_id
  2. email 
  3. message 

At first let us store these attributes in the database .

<?php
// Create connection
$con = mysqli_connect("localhost", "root", "", "shopefy");

// Check connection
if (mysqli_connect_errno()) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$email = mysqli_real_escape_string($con, $_POST['email']);
$message = mysqli_real_escape_string($con, $_POST['message']);
$user_id = mysqli_real_escape_string($con, $_POST['user_id']);



mysqli_query($con, "INSERT INTO messages (customer_email, message,user_id)
		VALUES ('$email', '$message','$user_id')");
?>

After insertion of  these attributes we have to fetch them at other user side .

Email: used for identification purpose.

Message:It contains the information to be sended .

User_id:each user has a unique user id from which we can distinguish the users.

Now let us see how to fetch the attributes .

<?php

session_start();
// Create connection
$con = mysqli_connect("localhost", "root", "", "shopefy");

// Check connection
if (mysqli_connect_errno()) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$data=array();
$user_id = $_SESSION['user_id'];
$query = "SELECT * FROM messages WHERE user_id='$user_id'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));

while($resultarr = mysqli_fetch_assoc($result)){
	$arr['customer_email']=$resultarr['customer_email'];
	$arr['message']=$resultarr['message'];	
	$data[]=$arr;
}
?>

 

Now we need to display the email and message place the below php code in html code where you want to display them.

For displaying email:

		<?php
				foreach($data as $d){
	
	
				?>

				<li>
					<h2 style="margin-left:30px;"><?php echo $d['customer_email']?></h2>
					<hr>
				</li>
				
				
<?php
}
                ?>

 

For displaying message

<?php

				foreach($data as $d)
				{
	
	
				?>

			<li>
				<p style="padding-left:50px; padding-top:6px;">
					<?php echo $d['message']?>
				</p>
				<hr>

			</li>
			<?php
				}
				?>

 

Author

  • Irfan Iqbal
    Irfan Iqbal

    Irfan is a web developer and consultant from India. He is the co-founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, manages everything related to marketing and seo for kodeinfo , works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus