PHP : $_GET is not retrieving data from MySQL database - php

I have a blog-type php site with mysql database. This blog have some lessons. There's a table "lessons", which contain id, title, text of lesson, etc.
When I display the last lessons on the main page, it works just right.
I connect to db like this:
<?php
$db = mysql_connect ("localhost", "root", "");
mysql_select_db("kursach", $db);
mysql_query("SET NAMES utf8");
$result = mysql_query ("SELECT title, meta_k, meta_d, text FROM settings
WHERE page='index' ", $db);
$myrow = mysql_fetch_array($result);
?>
and display them using loop:
<?php
$result = mysql_query("SELECT id, title, date, description FROM lessons", $db);
$myrow = mysql_fetch_array($result);
do {
printf ("<div class='right-column-content'>
<div class='right-column-content-heading'>
<a href='lesson_view.php?%s'><h1>%s</h1></a>
<h2>%s </h2>
</div>
<div class='right-column-content-content'>
<p>%s</p>
<div class='button'><a href='lesson_view.php?%s' >Читати далі</a></div>
</div>
</div>", $myrow['id'], $myrow["title"], $myrow["date"], $myrow["description"], $myrow["id"]);
}
while($myrow = mysql_fetch_array($result))
?>
I also have a file for full content of the lesson - lesson_view.php. As you can see at the code above, i send the id of lesson to link to this lesson:
lesson_view.php?%s
$myrow["id"]
In the lesson_view.php I connect to db and get the id like this:
<?php
$db = mysql_connect ("localhost", "root", "");
mysql_select_db("kursach", $db);
mysql_query("SET NAMES utf8");
if (isset($_GET['id'])) {$id = $_GET['id'];}
$result = mysql_query("SELECT * FROM lessons WHERE id = '$id' ", $db);
$myrow = mysql_fetch_array($result);
?>
And use this code to display the data:
<div class="right-column-content">
<div class="right-column-content-heading">
<h1><?php echo $myrow['title'] ?></h1>
<h2><?php echo $myrow['date'] ?> </h2>
<h2><?php echo $myrow['author'] ?> </h2>
</div>
<div class='right-column-content-content'>
<p><?php echo $myrow["text"] ?></p>
</div>
</div>
The problem is, when I try to look the full content of lesson (for exaple, /lesson_view.php?1), it doesn't display any data: no title, no text, nothing. I've tried this query directly at MySQL and it works, so, maybe there's some error in php code that I can't find. Will be thankful for any help.
P.S. I'm a beginner at php.

if you want to have in $_GET id then your link should be instead lesson_view.php?%s -> lesson_view.php?id=%s
for example lesson_view.php?id=5 mean that $id = $_GET['id'] will give 5, $id = 5;

<a href='lesson_view.php?%s'><h1>%s</h1></a>
You did not name the 'id' variable.
Change the links to
<a href='lesson_view.php?id=%s'><h1>%s</h1></a>

The only issue is that your printf() have 5 arguments and you are using only four and fourth one is description that you are using here
<div class='button'><a href='lesson_view.php?%s' >Читати далі</a></div>
Solution is that:
printf ("<div class='right-column-content'>
<div class='right-column-content-heading'>
<a href='lesson_view.php?%s'><h1>%s</h1></a>
<h2>%s </h2>
</div>
<div class='right-column-content-content'>
<p>%s</p>
<div class='button'><a href='lesson_view.php?id=%s' >Читати далі</a></div>
</div>
</div>", $myrow['id'], $myrow["title"], $myrow["date"], $myrow["id"], $myrow["description"]);
Move $myrow["id"] in fourth position you will get the ID as query string.
And also add the id in query string.

Related

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.
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);

Looping through PHP database with columns that link to specific profiles

I have a website where I am getting information of college student profiles on a database and displaying it as a linked collection. When I'm looping through the database I want to give each row a specific link to the profile of the student. Right now I am linking it to "profilePage.html" with generic information but I want it to be correlated with the row the user chose on the last(college) page.How do I save/transfer that information to the page. I do not want multiple profile pages but one template that is filled with the user previous choice.
<?php
$result = mysql_query("SELECT * FROM student_info WHERE college='Boston College'", $db);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
?>
<a href="profilePage.html" class="collection-item">
<div class="row summary">
<div class="col s4 center">
<img class = "profile-pic" src="img/defaultProfile.jpg">
</div>
<div class="col s8">
<div class="title"> <?php echo $row[student_Name]; ?> </div>
<div class="black-text condensed thin"><i class="tiny material-icons">today</i> Founder, CEO at Linkle since January 1st, 2015</div>
<div></div>
</div>
</div>
</a>
<?php } ?>
Key thing, my urls are mysite.com/college.php and have no id's to specify them.
Structure of the Database student_info:
Shows the structure of the database
First, do you have an URL Rewriting ? If not, your target page should be a PHP page, like profilePage.php.
Your link to this page have to include a parameter (Query String) which is, for example, the student's ID :
<a href="profilePage.php?id=<?php echo $row['id'] ?>" class="collection-item">
This type of URL will be generated: profilePage.php?id=36
In profilePHP.php, retrieve the parameter in the Query String :
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
?>
mysql_real_escape_string() is really important, it prevents SQL injections.
After that, you could retrieve the student's informations with a SQL query.
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
$sql = sprintf("SELECT * FROM student_info WHERE id = %s", $idStudent);
$query = mysql_query($sql);
$student = mysql_fetch_object($query);
// you should check if the ID exists, and if there is 1 result
?>
<p>Student name is <?php echo $student['student_Name'] ?></p>
A little advice: mysql_query() will disappear soon, you should take a look at PDO.

How to insert echo into div

I'm new here, so do not be angry if I ask something that is already answered.
I connected sql database:
connect.php
<?php
$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mobilni", $connect) or die(mysql_error());
?>
displaydata.php
<?php
include "connection.php";
$sql= "SELECT * FROM imena WHERE Okrug='Beogradski'";
$query=mysql_query($sql) or die (mysql_error());
?>
<div class="beyondheader"></div>
<div class="header">
<div id='cssmenu'>
<ul>
<li class='active'><a href='index.html'>Početna</a></li>
<li><a href='proizvodjaci.html'>Proizvodjači</a></li>
<li><a href='oglasi.html'>Oglasi</a></li>
<li><a href='about.html'>O nama</a></li>
</ul>
</div>
</div>
<div class="middle">
<div class="leftmiddle">ss
</div>
<div class="rightmiddle">
<?php
while ($row = mysql_fetch_array($query)){
?>
<div class="divmobilni">
Ime:<div class="mobilniime"><?php echo $row['Ime'];?></div>
Okrug:<?php echo $row['Okrug'];?>
</div>
<?php } ?>
</div>
</div>
And everything is working fine. Now I want to put every result in the other <div> automatically?
How to do that?
Sorry if I wasn't clear enough. I want to sort results to be like this, in one <div> goes: Ime: Okrug: [that is data for one person]
Now, I want to make that all data from my SQL table display on this page, but every Person separately from this <div>.
To be something like this, with data from table: (This is just example drawn in Paint)
I fixed this with adding only break to the end of PHP. Thanks in any case!
Exactly the same as you would with php open and close <?php ?>
Use this inside the divs you would like or just use echo with the div inside

WHERE not working when it should be?

before I start I have looked over the answers related but I cannot figure this out
Hello, I am trying to fetch the values with just the data from formtype = 'Question1'
the value is in the database under column formtype as Question1, it is a radio option value which is one of 3 values that I use to redirect information over to one page, so I have to do this on each page to get that specific information. My question is, why is it not selecting the values that contain Question1? it doesn't grab anything at all, but if I get rid of WHERE it grabs all the information just fine(which i only wanna grab the one containing Question1). Why is this? it isnt showing any errors with SELECT so I don't know what I did wrong.
I am connected to the database
<?php $db_name = 'submissions';
$db_user = 'root';
$db_pass = '';
$db_host = 'localhost';
mysql_connect("localhost", $db_user, $db_pass) or die(mysql_error());
mysql_select_db("submissions") or die(mysql_error());
$query1 = mysql_query("SELECT actual_quote,poster,formtype FROM data WHERE formtype = 'Question1'");
$info = mysql_fetch_array($query1);
while($info = mysql_fetch_array($query1)){
echo '
<div class="wrapper">
<div class="submissions">
<div class="logo-logo"><h2>Questions.</h2>
<div class="checkboxes">'.$info['formtype'].'
</div>
</div>
<div class="top-submit">
&#8220'. $info["actual_quote"] . '”
</div>
<div class="poster">- '. $info["poster"].'
<div class = "like">
Like
<p id = "like" style = "color:green;">0</p>
</div>
<div class = "dislike">
Dislike
<p id = "dis" style = "color:red;">0</p>
</div>
</div>
<!-- use select to get the items to stay on the page-->
</div>
</div>
</div>
';
}
?>
Any suggestions?
Fix these lines:
$link = mysql_connect("localhost", $db_user, $db_pass) or die(mysql_error());
mysql_select_db("submissions",$link) or die(mysql_error());
$query1 = mysql_query("SELECT actual_quote,poster,formtype FROM data WHERE formtype = 'Question1'",$link);
//delete this-> $info = mysql_fetch_array($query1); ?
while($info = mysql_fetch_array($query1)){

update value from '0' to '1' on image click?

I'm new to php and mysql so sorry if i'm doing it wrong. i have a page on my site that lists the reviews that members give to other other users.
Basically i have approved and deleted in my database which means that after a user sends the review it has to be reviewed by the user before it gets displayed.
once the user clicks the approved image which is a tick it goes to approved_review.php and in their i have my sql code to update the value from 0 to 1 in my database.
It should work exactly the same for the delete but obviously instead of updating the approved column it will update deleted.
the code i have tried is not working i have been working on this for quite some time and can;t figure it out.
Can someone please tell me where i'm going wrong?
Heres the code:
<?php
$reviews_set = get_pending_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<p> </p>
<div class="pending-review-content">
<?php
$date = $reviews['date_added'];
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="message_pic"><?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">
<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" /></a>";?>
</div>
<div class="reviews-date"><? echo "$date"; ?></div>
<div class="reviews-from">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">{$reviews['display_name']}"; ?>
</a> Wrote:
</div>
<div class="reviews-content">
<?php echo "{$reviews['content']}"; ?>
</div>
</div>
<div class="reviews-approve">
<img src="assets/img/icons/tick.png" width="30" height="25" /></div>
<div class="reviews-delete">
<img src="assets/img/icons/cross.png" width="30" height="25" />
</div>
<? } ?>
approved_review.php function:
<?
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
echo "<div class=\"infobox1\">review approved.</div>";
?>
Your approach seems logical. After you loop through your reviews, you click on the tick or delete pngs to update or delete.
So, in approved_review.php
<?php
//you are missing the connection to your mysql database...
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
//execute the mysql query
$r = mysql_query($sql);
if (!mysql_error())
{
echo "<div class=\"infobox1\">Review Approved.</div>";
}
?>
a little edit rrrfusco's post
// or die for details if mysql_query won't work correct
$r = mysql_query($sql) or die (mysql_error());

Categories