samedi 27 juin 2015

Data in database is entered only for the first time

Iam trying to create a private messaging system in which user sends message to another user and that content is inserted into database..Iam using a random number called hash to identify a conversation between two people..table for that is "message_group" and table for saving messages is "messages"..here comes the problem..
When I type something in text area and click on sendmessage button it inserts the data into the messages database..But if type something again and try to send it , the data wont enter into database..coz of this the other person is getting only first message..Please help me solving this problem..here's the code

<html>
<head>
<title>new convo</title>
</head>
<body>
<?php include 'connect.php';?>
<?php include 'message_title_bar.php';?>
<?php include 'functions.php';?>

<div>
<?php 
if(isset($_GET['user']) && !empty($_GET['user'])){ 
?>


<form method='post'>

<?php
if(isset($_POST['message']) && !empty($_POST['message'])){


        $my_id=$_SESSION['user_id'];

    $user=$_GET['user'];
    $random_number=rand();
    $message=$_POST['message'];


    $connect = mysqli_connect('localhost','root','','php_mysql_login_system');
    if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $query_string = "SELECT `hash` FROM `message_group` WHERE (`user_one`='$my_id' AND `user_two`='$user') OR (`user_one`='$user' AND `user_two`='$my_id')";

  $check_con=mysqli_query($connect,$query_string) or die(mysqli_error($connect));
    if(mysqli_num_rows($check_con)==1){
        echo "<p>Conversation already Started</p>";
    }else{
        $connect = mysqli_connect('localhost','root','','php_mysql_login_system');

    if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
        mysqli_query($connect,"INSERT INTO message_group VALUES('$my_id' , '$user' , '$random_number')");
    mysqli_query($connect,"INSERT INTO messages VALUES ('','$random_number','$my_id','$message')");
    echo "<p>Conversation started</p>";
    }
}
?>

Enter message:<br />
<textarea name='message' rows='7' cols='60'></textarea>
<br />
<br />
<input type='submit' name="submit" value="sendmessage" />
</form>


<?php


}

else{
    echo "<b>Select User</b>";
$connect = mysqli_connect('localhost','root','','php_mysql_login_system');
   $user_list=mysqli_query($connect,"SELECT `id`,`username` FROM `users`");
    while($run_user=mysqli_fetch_array($user_list)){
       $user = $run_user['id'];
        $username = $run_user['username'];
        echo "<p><a href='send.php?user=$user'>$username</a></p>";
        }
}
?>
</div>
</body>
</html>  

Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire