Help Needed:Random Image Links to its own page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
postmanager
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 2:21 am

Help Needed:Random Image Links to its own page

Post by postmanager »

Hello all,
I need help with a php code. I am stuck and I will really appreciate if you can help me.

I have one page that displays a random project image and project title out of a phpmyadmin database.

Code for this page:

******************************************
<?php
$result = mysql_query("SELECT id, project_title, img_bin FROM featured_projects WHERE W_C = 1 AND status = 1 ORDER BY Rand() LIMIT 1");

if (mysql_num_rows($result) == 0) { echo "<p>Sorry. We couldn`t find any projects.</p>";}
else {
while( $c = mysql_fetch_object($result)) {

$image = ($c->img_bin) ? "<img src=\"img.php?id={$c->id}&table=featured_projects\" "
."border=\"0\" alt=\"[image]\" />" : "";
printf("<div class=\"featured\">
<h3>%s</h3>
<p>$image</p>
</div>
",
$c->project_title, $c->project_title, $c->project_title, $c->project_title
);
}
}
?>
************************************************
When I click on the active image (selected randomly) in the first page, I want to go to another page and show a more detailed information about the selected project. Here is the code I write for this:
************************************************
<?php
$result = mysql_query("SELECT id, project_title, project_description , related_manager, team_members, img_bin FROM featured_projects WHERE DM = 1 AND status = 1");

if (mysql_num_rows($result) == 0) { echo "<p>Sorry. No news to report yet.</p>";}
else {
while( $c = mysql_fetch_object($result)) {

$image = ($c->img_bin) ? "<img src=\"../img.php?id={$c->id}&table=featured_projects\" "
."border=\"0\" alt=\"[image]\" />" : "";
print <<<END

<table width="690" border="0" cellpadding="0" cellspacing="20">
<tr>
<td width="550" align="left">
<h2>$c->project_title </h2><br>
<b>Manager: </b> $c->related_manager <br>
<b>Team Members: </b> $c->team_members <br>
$c->project_description <br>
</td>

<td width="140" valign="top">
$image
</td>

</tr>

</table>

END;
}
}
?>
*****************************************
My only problem is, the second code shows information about all of the projects, not the selected one.

How can I do that?

Thanks in advance,
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Help Needed:Random Image Links to its own page

Post by novice4eva »

Did you send the project id in the "second portion" of you code because in the sql there is no mention of the id..or is DM the primary key to identify that specific project???
postmanager
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 2:21 am

Re: Help Needed:Random Image Links to its own page

Post by postmanager »

yes, it is my id key in my only database and it is unique for every project
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Help Needed:Random Image Links to its own page

Post by novice4eva »

OK if DM is the PRIMARY KEY well it can't be if you say it's showing you all the projects since you have set DM=1 in the query!!!! Even if it were primary key then DM should be dependent in some variable, probably something sent via GET parameter..... :dubious:
postmanager
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 2:21 am

Re: Help Needed:Random Image Links to its own page

Post by postmanager »

Oh sorry, I misunderstood you.
My primary key named "id"
DM is not my primary key. DM is just a field in my database table.
I have many records in the table.
For this particular query, I want to select a random one from a set of pictures where DM field equals 1.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Help Needed:Random Image Links to its own page

Post by novice4eva »

OK THEN it is really simple, you have this code
$result = mysql_query("SELECT id, project_title, project_description , related_manager, team_members, img_bin FROM featured_projects WHERE DM = 1 AND status = 1");
What you would want to do is:

Code: Select all

 
$result = mysql_query("SELECT id, project_title, project_description , related_manager, team_members, img_bin FROM featured_projects WHERE DM = 1 AND status = 1 AND id='".$_GET['id']."'");
 
And you should send this id from the first portion of your code, probably as get parameter.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Help Needed:Random Image Links to its own page

Post by Ziq »

This is a really dangerous code! You should check all data received from users. Read about SQL Injecktion.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Help Needed:Random Image Links to its own page

Post by novice4eva »

Yes if the information is really VITAL/IMPORTANT, i suggest using bind variables.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Help Needed:Random Image Links to its own page

Post by novice4eva »

while speaking of bind variables -- for that you have to learn PDO or adodb!! I don't know php has function such as oci_bind_by_name that is available for oracle!! :dubious:
Post Reply