• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

GET method and querying and displaying associated data EASY

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
bwsycom
Asked:
bwsycom
  • 5
  • 3
  • 3
  • +2
4 Solutions
 
glcumminsCommented:
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
 
bwsycomAuthor Commented:
How do I display the results?
0
 
Aamir SaeedCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
glcumminsCommented:
// 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
 
glcumminsCommented:
@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
 
bwsycomAuthor Commented:
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
 
f_o_o_k_yCommented:
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
 
bwsycomAuthor Commented:
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
 
raja_ind82Commented:
<?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
 
raja_ind82Commented:
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
 
bwsycomAuthor Commented:
Plugged everything in and got "Invalid data processing" with PHP 4.4.6 and MySQL 4.0.27
0
 
f_o_o_k_yCommented:
Did You use link like

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

?post_id=2222 is very important :)
             
0
 
raja_ind82Commented:
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
 
f_o_o_k_yCommented:
<?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
 
bwsycomAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now