?
Solved

GET method and querying and displaying associated data EASY

Posted on 2007-04-03
15
Medium Priority
?
195 Views
Last Modified: 2013-12-12
This is probably really easy, I've searched and can't find a similiar situation.

I'm creating a reply script in PHP...a user clicks reply on an ad and that ad's ID is appended to the URL via the GET method.

What I'm trying to do is display the original ad on the top of the reply page. The ads are stored in a MySQL DB.  What query string can I use to display the ad whos ID is in the URL?

I will also have a form on that page that will send the reply to the original poster via mail(). I might have more easy questions later.
0
Comment
Question by:bwsycom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 400 total points
ID: 18845926
First, get the id from the query string:

if (isset($_GET['id']))
{
   $id = intval($_GET['id']);
   $query = "SELECT * FROM ads WHERE id=$id;";
}

You can modify this to fit your table structure and the name of the parameter passed in the URL string
0
 
LVL 1

Author Comment

by:bwsycom
ID: 18847176
How do I display the results?
0
 
LVL 14

Assisted Solution

by:Aamir Saeed
Aamir Saeed earned 400 total points
ID: 18847232
First you 'll get the ID from the query string and 'll get the record from DB against it e.g.

if (isset($_GET['id']))
{
   $id = intval($_GET['id']);
   $query = " SELECT * FROM ads WHERE id='$id' ";
}

if you have ID of that user, you can get email address of him / her from the DB and send an email e.g.
http://www.php.net/mail
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 24

Expert Comment

by:glcummins
ID: 18847239
// First, run the query that you created:

$result = mysql_query($query) or die(mysql_error());

// Next, loop through each of the results and display them:
while ($row = mysql_fetch_object($result))
{
   // Since I do not know what fields you are using, I cannot use specifics here. But if you
   // have a field named 'company', you would display it like this:
   print $row->company . "<br />";

   // A field named 'telephone':
   print $row->telephone . "<br />";
}
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18847261
@amir: When a variable contains an integer or decimal value, there is not a need to quote it:

   $query = " SELECT * FROM ads WHERE id='$id' ";

should be:

   $query = " SELECT * FROM ads WHERE id=$id;";

since we know $id is an integer from the line before.
0
 
LVL 1

Author Comment

by:bwsycom
ID: 18847307
This is what I've got so far:

<?php # reply.php, inline replies to ads.php

require_once ('./includes/config.inc.php');

$page_title = 'Reply to an ad';

include ('./includes/header.html');

require_once ('./includes/mysql_connect.php');

if (isset($_GET['post_id']))
{
   $post_id = intval($_GET['post_id']);
   $query = "SELECT content FROM adtable WHERE post_id=$post_id;";
}

$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_object($result))
{
      print $row->content . "<br />";
      }
?>

But then when ran through a browser I get:

An error occurred in script '/home/isawu/public_html/reply.php' on line 17:
Undefined variable: query
Date/Time: 4-3-2007 15:25:54
0
 
LVL 11

Assisted Solution

by:f_o_o_k_y
f_o_o_k_y earned 400 total points
ID: 18847437
Hello
That's because you only use $query inside if{} so there is no $query outside

try:
if (isset($_GET['post_id']))
{
   $post_id = intval($_GET['post_id']);
   $query = "SELECT content FROM adtable WHERE post_id=$post_id;";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
      print $row->content . "<br />";
      }

}

0
 
LVL 1

Author Comment

by:bwsycom
ID: 18847741
Its not returning any data...the page is completely blank. I know mysql_connect is OK, I have MySQL debugging enabled and I can run queries fine through the mysql monitor.

I guess I'll turn on PHP error handling and see if I can pinpoint the problem. Its a really simple (one table) database, and I don't think what I'm doing is that hard, although this is my first site that I'm doing by myself.
0
 
LVL 14

Accepted Solution

by:
raja_ind82 earned 800 total points
ID: 18849305
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("iml_pda_remote");

if (isset($_GET['post_id']) && is_numeric(($_GET['post_id'])))
{
    $post_id =$_GET['post_id'];
    $query = "SELECT content FROM adtable WHERE post_id=$post_id";
    $result = mysql_query($query);
      if (!$result)
      {
            die('Invalid query: ' . mysql_error());
      }

      $numofrows=mysql_num_rows($result);
      if($numofrows>0)
      {
            $row=mysql_fetch_array($result);
            $Content=$row['content'];
            echo $Content."<br>";
      }
      else
      {
            echo "There is no record with this ID : ".$post_id;
      }
}
else
{
      Echo "Invalid data processing";
}
?>

Please check this. i hope this will help you.

Regards,
M.Raja
0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 18849313
For the above code, change the database name and check the table structure. and save that code as getreply.php.

try this with the following link
http://yourserver/yourfolder/getreply.php?post_id=111
0
 
LVL 1

Author Comment

by:bwsycom
ID: 18851905
Plugged everything in and got "Invalid data processing" with PHP 4.4.6 and MySQL 4.0.27
0
 
LVL 11

Expert Comment

by:f_o_o_k_y
ID: 18852480
Did You use link like

http://yourserver/yourfolder/getreply.php?post_id=111

?post_id=2222 is very important :)
             
0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 18852494
did you  check with the correct querystring value (Ex: getreply.php?post_id=111)

Check your querystring, that may not spelled correctly or not an integer.
0
 
LVL 11

Expert Comment

by:f_o_o_k_y
ID: 18852506
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("iml_pda_remote");

if (isset($_GET['post_id']))
{
 
   if( is_numeric($_GET['post_id'])){

    $post_id =$_GET['post_id'];
    $query = "SELECT content FROM adtable WHERE post_id=$post_id";
    $result = mysql_query($query);
      if (!$result)
      {
            die('Invalid query: ' . mysql_error());
      }

      $numofrows=mysql_num_rows($result);
      if($numofrows>0)
      {
            $row=mysql_fetch_array($result);
            $Content=$row['content'];
            echo $Content."<br>";
      }
      else
      {
            echo "There is no record with this ID : ".$post_id;
      }
  }else{
           echo 'post_id is not a number';
  }
}
else
{
      Echo "Please provide post_id";
}
?>


try this for debug
0
 
LVL 1

Author Comment

by:bwsycom
ID: 18852544
I tried to award points as best I could, everyone was right from the beginning.

Now to work on the mail() functions
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question