Michael Lee
Michael Lee

Reputation: 67

Can't print certain values from database in PHP

I'm using this database and I have created a table in my PHP file where I list all of these values. I have link my "subject" values to a different file which is supposed to display "description", but every time when I click on "Subject" I get the description from the first input in the table.

This is my code for displaying the table

<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");
?>
<?php
ob_start();
include 'organize_meeting.php';
ob_end_clean();

$mysqli    = new mysqli("localhost", "root", "", "users");
$curr_user = $loggedInUser->username;

mysql_connect('localhost', 'root', '');

mysql_select_db('users');

$query = "SELECT * FROM uc_messages WHERE `to` = '$curr_user'";

$records = mysql_query($query);

echo "
<body>
<div id='wrapper'>
<div id='top'><div id='logo'></div></div>
<div id='content'>
<h2>My Meetings</h2>
<div id='left-nav'>";

include("left-nav.php");

echo "
</div>
<div id='main'>

</div>
<div id='bottom'></div>
</div>
</body>
</html>";
?>

    <table width = "600" border = "1" cellpadding = "1" cellspacing = "1">
    <tr>
        <th>From</th>
        <th>Subject</th>
        <th>Beginning of meeting</th>
        <th>End of meeting</th>
    </tr>

    <?php

        while($msgs = mysql_fetch_assoc($records)){
            echo "<tr>";

            echo "<td>".$msgs['from']."</td>";

            echo "
                <td>
                    <a href = 'info.php' target = '_blank_'> ".$msgs['subject']." </a>
                </td>
            </html>";

            echo "<td>".$msgs['TimeFrom']."</td>";

            echo "<td>".$msgs['TimeTo']."</td>";

            echo "<tr>";
        }

    ?>

And this is my code that is suppoused to display the description

<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");
?>

<?php

ob_start();
include 'organize_meeting.php';
ob_end_clean();

$mysqli    = new mysqli("localhost", "root", "", "users");
$curr_user = $loggedInUser->username;

mysql_connect('localhost', 'root', '');

mysql_select_db('users');

$query = "SELECT * FROM uc_messages WHERE `to` = '$curr_user'";

$records = mysql_query($query);

echo "
<body>
<div id='wrapper'>
<div id='top'><div id='logo'></div></div>
<div id='content'>
<h2>About Meeting</h2>
<div id='left-nav'>";


echo "
</div>
<div id='main'>

</div>
<div id='bottom'></div>
</div>
</body>
</html>";

?>

    <?php

        $msgs = mysql_fetch_assoc($records);

            echo "<p>".$msgs['from']."</p>";

            echo "<p>".$msgs['description']."</p>";

            echo "<p>".$msgs['TimeFrom']."</p>";

            echo "<p>".$msgs['TimeTo']."</p>";


    ?>

I always get the same description from the database, although I click on a different input.

Upvotes: 0

Views: 44

Answers (1)

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

Note: You are mixing up mysqli.* and the mysql together and you have to convert to mysqli.* the entire query that you have. You have to fix all the mix up codes that you have and after that you follow the code that i have given.

And the below example i am giving in the statement of the mysqli.* and you have to make changes to the code.

File One:

You are missing to pass the ID of the subject or the auto increment ID in the <a> tag which you want to display.

Hence the following <a> tag will be as follows.

Replace:

<td>
<a href = 'info.php' target = '_blank_'> ".$msgs['subject']." </a>
</td>

With:

<td>
<a href = 'info.php?subject_id=".$msgs['id']."' target = '_blank_'> ".$msgs['subject']." </a>
</td>

File Two:

Hence you can get the Request that we have passed in the <a> tag over here and we shall manipulate it.

$msgs = mysqli_fetch_assoc($records)    
echo "<p>".$msgs['from']."</p>";
echo "<p>".$msgs['description']."</p>";
echo "<p>".$msgs['TimeFrom']."</p>";
echo "<p>".$msgs['TimeTo']."</p>";    

Remove these lines since it makes no sense. Already you have connected to the DB using the mysqli connectivity.

mysql_connect('localhost', 'root', '');
mysql_select_db('users');

Change the query like this so that it will be the mysqli.* way.

$query = "SELECT * FROM uc_messages WHERE `id` = '".$_REQUEST['subject_id']."'";
$records = mysqli_query($mysqli,$query);

Upvotes: 2

Related Questions