0

I have two tables named comments and users where id in comments is the foreign key.

I am successfully able to show notification of the message posted. But I also want to post the name and details of the user who posted it but it doesn't seem to work.I tried joins but I am not able to figure out where the problem exactly is.

Here is my comments table:

 CREATE TABLE IF NOT EXISTS 'comments' (
 'notificationid' int(30) NOT NULL,
 'comment' varchar(300) NOT NULL,
 'id' bigont(20) NOT NULL        /* foreign key */
 'status' int(1) NOT NULL DEFAULT '0',
 'subject' varchar(300) NOT NULL
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci  ;

users.sql:

CREATE TABLE 'users' (
 'id' bigint(20) NOT NULL auto_increment,
 'full_name' tinytext collate latin1_general_ci NOT NULL,
 'user_name' varchar(200) collate latin1_general_ci NOT NULL default '',
 'user_email' varchar(220) collate latin1_general_ci NOT NULL default ''
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci ;

This is my view-notification.php:

<?php
$conn = new mysqli("localhost","root","","noti");
$sql="UPDATE comments SET status=1 WHERE status=0"; 
$result=mysqli_query($conn, $sql);
$sql="select * from comments ORDER BY id DESC limit 5";
$result=mysqli_query($conn, $sql);
$result3 = mysql_query("SELECT * FROM 'users'
 JOIN comments ON comments.id = users.id
 WHERE `user_id` = 54"); // user_id not is a column, is users.id
 $response='';
 while($row=mysqli_fetch_array($result)) {
$response = $response . "<div class='notification-item'>" .
"<div class='notification-subject'>". $row["subject"] . "</div>" . 
"<div class='notification-comment'>" . $row["comment"]  . "</div>" .
"</div>";
   }

 while($row1=mysqli_fetch_array($result3)) {         //This is what is tried

"<div class='notification-comment'>" . $row["user_name"]  . "</div>". 

"</div>"; // change $row for $row1
 }
if(!empty($response)) {
print $response;
}
?>



The notification.php file is responsible for displaying notifications in the index.php page.

And this is the final index.php page:

<?php
$conn = new mysqli("localhost","root","","noti");
$count=0;
if(!empty($_POST['add'])) {
    $subject = mysqli_real_escape_string($conn,$_POST["subject"]);
    $comment = mysqli_real_escape_string($conn,$_POST["comment"]);
    $sql = "INSERT INTO comments (subject,comment) VALUES('" . $subject . "','" . $comment . "')"; 
    mysqli_query($conn, $sql);
}
$sql2="SELECT * FROM comments WHERE status = 0";
$result=mysqli_query($conn, $sql2);
$count=mysqli_num_rows($result);
?>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Facebook Like Header Notification in PHP</title>
    <link rel="stylesheet" href="notification-demo-style.css" type="text/css">
    <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">

    function myFunction() {
        $.ajax({
            url: "view_notification.php",
            type: "POST",
            processData:false,
            success: function(data){
                $("#notification-count").remove();                  
                $("#notification-latest").show();$("#notification-latest").html(data);
            },
            error: function(){}           
        });
     }

     $(document).ready(function() {
        $('body').click(function(e){
            if ( e.target.id != 'notification-icon'){
                $("#notification-latest").hide();
            }
        });
    });

    </script>
    </head>
    <body>
    <div class="demo-content">
        <div id="notification-header">
               <div style="position:relative">
               <button id="notification-icon" name="button" onclick="myFunction()" class="dropbtn"><span id="notification-count"><?php if($count>0) { echo $count; } ?></span><img src="notification-icon.png" /></button>
                 <div id="notification-latest"></div>
                </div>          
        </div>
    <?php if(isset($message)) { ?> <div class="error"><?php echo $message; ?></div> <?php } ?>


    <?php if(isset($success)) { ?> <div class="success"><?php echo $success;?></div> <?php } ?>

        <form name="frmNotification" id="frmNotification" action="" method="post" >
            <div id="form-header" class="form-row">Add New Message</div>
            <div class="form-row">
                <div class="form-label">Subject:</div><div class="error" id="subject"></div>
                <div class="form-element">
                    <input type="text"  name="subject" id="subject" required>

                </div>
            </div>
            <div class="form-row">
                <div class="form-label">Comment:</div><div class="error" id="comment"></div>
                <div class="form-element">
                    <textarea rows="4" cols="30" name="comment" id="comment"></textarea>
                </div>
            </div>
            <div class="form-row">
                <div class="form-element">
                    <input type="submit" name="add" id="btn-send" value="Submit">
                </div>
            </div>
        </form>
        </div>
    </body>
</html>

Everything is working except for that the user details in the notification.

Dr._Duck
  • 125
  • 1
  • 12
  • Which index is undefined, and in which file? And CSS isn't relevant to this question, as PHP don't care how things look ;-) – Qirel Jun 19 '17 at 15:55
  • Which line in your code is giving you the error? – Sloan Thrasher Jun 19 '17 at 15:56
  • See the view-notification.php file I tried to display user details in this file – Dr._Duck Jun 19 '17 at 15:57
  • One issue is that you are ```select *``` in your query, but after the join, you will have two columns with the same name: **id**, you will need to specify which columns to return in your query. – Sloan Thrasher Jun 19 '17 at 15:58
  • @Dr._Duck: That doesn't tell us on which line the error occurred. – Sloan Thrasher Jun 19 '17 at 15:59
  • I have joined comments.id with users.id they both are different – Dr._Duck Jun 19 '17 at 15:59
  • But not in the set of columns returned. Use phpadmin or similar to run your query. You will see two columns named id in the results. – Sloan Thrasher Jun 19 '17 at 16:00
  • The error is on line 7 and 18 – Dr._Duck Jun 19 '17 at 16:01
  • If anything is wrong with my query can you please correct it. I just want to show the user id of the comment maker. – Dr._Duck Jun 19 '17 at 16:03
  • Now it is giving: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in F:\xampp\htdocs\notifications\view_notification.php on line 24 – Dr._Duck Jun 19 '17 at 16:51
  • Thats because your query is failing, as mentioned above, in this case id column is ambiguous. MySQL uses "AS" so you can assign unique column names in scenerios like this. `select user.id AS uid, coments.id AS cid ...` The only drawback is you have to specify every column you want in the results, * wont work with AS – Duane Lortie Jun 19 '17 at 17:06

0 Answers0