Solved

GET method and querying and displaying associated data EASY

Posted on 2007-04-03
15
190 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
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 100 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 100 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 100 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 200 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 60
How to use embargo Date/time in php 29 37
Call to undefined method mysqli_stmt on one live server but not the other 21 49
Curl & PHP Command Help 4 23
This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…

770 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