Solved

GET method and querying and displaying associated data EASY

Posted on 2007-04-03
15
191 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Complex MySQL Query 2 33
error while installing php56 in redhat enterprise linux 20 50
How to make a good PHP + MySQL + JS pagination system? 3 31
PHP Mail error 3 26
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Creating and Managing Databases with phpMyAdmin in cPanel.
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 look for a specific file type in a local or remote server directory using PHP.

839 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