Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Returning Information From PHP File 2

Status
Not open for further replies.

likelylad

IS-IT--Management
Jul 4, 2002
388
GB
I would love if someone could help me this one, it has been driving me mad for the last week.

I am trying to create a dynamic menu system using Ajax.

This is the code that I am having problems with.

The filename is called getinfo.php
Code:
<?php
$r=$_GET["r"];

$db = mysql_connect("servername", "username", "password");

mysql_select_db("dbname",$db);

$sql="SELECT * FROM tablename Where Grouping='$r'";

$result = mysql_query($sql,$db);
echo "Start If Loop";
if ($row = mysql_fetch_array($result)) {
echo "Start Do Loop";
do {
echo "Inside Do Loop";
$product=$row["Product"];
echo $product."<br>";
 } while($row = mysql_fetch_array($result));
echo "End Do Loop";
}
echo "End If Loop";


?>

When I call the file via Ajax, then it will show,"Start If Loop" and "End If Loop" only.

If I add in (outside of the Loops)
Code:
echo $r;
then it will echo out the correct variable

If I add in (outside of the Loops)
Code:
echo $sql;
then it will echo out the correct SQL statement.

If I call the file separately with getinfo.php?r=book, then it will output all the information correctly on that page.

If I change the Code as follows
Code:
$sql="SELECT * FROM tablename Where Grouping='book'";
Then this works correctly.

The issue is with the variable r but I can't figure out exactly what.

 
Try changing your SQL assignment to this:
Code:
$sql="SELECT * FROM tablename Where Grouping='" . $r . "'";

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Also, I would change your if/do combination to the following simplification:
Code:
while ($row = mysql_fetch_array($result)) {
  $product=$row["Product"];
  echo $product."<br />";
}

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Did Clive's suggestions help you? I'm not fully convinced that the problem is not in your query itself or, as you say, in your variable handling.

can you debug this by checking the input variables sent in your ajax call? essentially change the code to this

Code:
$output = '<pre>';
$output .= print_r($_POST, true);
$output .= print_r($_GET, true);
$output .= "</pre>";
echo $output;

i suspect that the variable you are sending is not arriving in the GET superglobal. perhaps your ajax is not behaving as you intend.
 
Hi Clive, sorry your suggestions have made no difference:

Hi jpadie: please see the message given below:
[!]
Gifts

Array
(
)
Array
(
[r] => Gifts
[sid] => 0.8088335912447162
)
[/!]

When I click on Gifts in my menu system, it shows the array information.
 
What does your Ajax call to getinfo.php look like?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
are you getting any sql errors?
Code:
$result = mysql_query($sql,$db) or die (mysql_error());

can you show the code snip that outputs the clickable links?
 
and it goes without saying that you should be cleansing the variable before using it in a query
Code:
$r = empty($_GET['r']) ? 'some default value' : mysql_escape_string(trim($_GET['r']));
 
jpadie is quite right regarding the cleansing of your $_GET var. However, as of PHP 4.3.0, mysql_escape_string became deprecated, so the PHP manual advises not using this function. Instead, use mysql_real_escape_string().

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
you can only use mysql_real_escape_string if you have already instantiated your database connection.

sfaik, if you are using a western character set, there is no difference between the two versions. But Clive is absolutely correct: best practice is to use the _real_ alternative and establish your connection ab initio.
 
OK, may have found something,still don't know how to fix it. If I try the following code:
Code:
<?php
$r = empty($_GET['r']);
$db = mysql_connect("servername", "username", "password");

mysql_select_db("dbname",$db);

$sql="SELECT * from tablename Where Grouping='".$r."'";
echo $sql;
$result = mysql_query($sql,$db) or die (mysql_error());
echo "Start If Loop";

if ($row = mysql_fetch_array($result)) {
echo "Start Do Loop";
do {
echo "Inside Do Loop";
$product=$row["Product"];
echo $product."<br>";
 } while($row = mysql_fetch_array($result));
echo "End Do Loop";
}
echo "End If Loop";

?>

Then my output is as follows
[!]
Gifts

SELECT * from productgrouping Where Grouping=''Start If LoopEnd If Loop
[/!]

If I change the code back to
Code:
$r = $_GET['r'];

Then may output changes to
[!]
Gifts

SELECT * from productgrouping Where Grouping='Gifts
'Start If LoopEnd If Loop
[/!]

So it looks like I am getting some extra new line characters or something after the variable.

I have tried:
Code:
$r = trim($_GET['r']);

But It doesn't seem to make any difference
 
Have It.

I entered the following code

Code:
if ($_GET) {
    echo '<pre>';
    echo htmlspecialchars(print_r($_GET, true));
    echo '</pre>';
}

And the response was as follows
[!]
Array
(
[r] => Gift<br>
[sid] => 0.8373895446589314
)

[/!]

I then added in
Code:
$r = rtrim($_GET['r'],"<br>");

And it is now working for me.

Thanks very much for the both of you for your time and for getting me to think in a different direction.
 
You're welcome - glad you got it sorted.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
likelylad
it would be better to address the issue in the html that you output or the js you use. hence my request to see the code that you use to deliver the html links.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top