Solved

GET method and querying and displaying associated data EASY

Posted on 2007-04-03
15
185 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now