PDA

View Full Version : PHP_SELF variable


js
09-24-2002, 05:48 PM
Having some trouble getting access to a variable ($cat_id) after it has been passed through a PHP_SELF form. The script works fine (needs some tweaking but generally ok) up to the second SQL statement: $queryÂ*=Â*"SELECTÂ*article_title,Â*id,Â*cat_nameÂ*FROMÂ*NewsÂ*WHEREÂ*cat_id *= '$_POST[cat_id]'";

What I get is a blank (no errors) unless I hard code it to, for example:
$query = "SELECT DISTINCT article_title, id, cat_name FROM News WHERE cat_id = '1'";

In this case I do get a result. Anybody have any idea why this variable is not getting passed on? I've tried a few different ways but it has not worked unless I hard code it!

Here is my current code:

<?php require('/pagedresults.php'); ?>

<?php
//dbConnect("****");
$cnx = @mysql_connect('localhost','root','****');
mysql_select_db('****',$cnx);
$rs = new MySQLPagedResultSet("select date_format(date, '%M %D, %Y') AS nicedate,id, article_title, cat_name, cat_id, article from News ORDER BY date DESC", 10,$cnx);

?>
<?php

if(!$_POST['submit'])
// form not yet submitted, display search categories form
{
?>
<table>
<?php while ($row = $rs->fetchArray()): ?>
<tr><td><a href=news_story.php?id=<?=$row['id']?>><?=$row['article_title']?></a></td></tr>
<tr><td><?=$row['cat_name']?>, <?=$row['nicedate']?></td></tr>
<?php endwhile; ?>
</table>
<p><?=$rs->getPageNav("id=$id")?></p>

<form action="<? echo ($_SERVER['PHP_SELF']); ?>" method="post">
<select name="newsid">
<?php
// get list of news categories
$cnx = @mysql_connect('localhost','root','*****');
$db = '****';
$query = "SELECT DISTINCT cat_id, cat_name from News";
$result = mysql_db_query($db, $query, $cnx) or die ("Error in query: $query. " . mysql_error());

// and print
while(list($cat_id, $cat_name) = mysql_fetch_row($result))
{
echo "<option value=$cat_id>$cat_name</option>";
}
mysql_free_result($result);
?>
</select>
<input type="submit" name="submit" value="Go!">
<?php

}
if($_POST['submit'])
{

// get list of news categories
$cnx = @mysql_connect('localhost','root','****');
$db = '******';
$queryÂ*=Â*"SELECTÂ*article_title,Â*id,Â*cat_nameÂ*FROMÂ*NewsÂ*WHEREÂ*cat_id *= '$_POST[cat_id]'";
$query = "SELECT DISTINCT article_title, id, cat_name FROM News WHERE cat_id = '$cat_id'";
// execute query
$result = mysql_db_query($db, $query, $cnx) or die ("Error in query: $query. " . mysql_error());
// number of records found
$count = mysql_num_rows($result);

?>

Your search returned <? echo $count; ?> match(es)?>
<p>
<ul>

<?php
// list matches
while(list($article_title, $id) = mysql_fetch_row($result))

{
echo "<li><a href=news_details.php?id=$id>$article_title</a>";
}
?>
</ul>
<?php

}
// clean up
mysql_close($cnx);

?>

dank
09-24-2002, 06:00 PM
I haven't looked through the full code, but you might need to bust out of the quotes in the SQL query you posted:

$query = "SELECT article_title, id, cat_name FROM News WHERE cat_id = '". $_POST[cat_id] ."'";

You could probably also use $_REQUEST in place of $_POST if you know the PHP installation is recent and want a bit more flexibility.

Dan

js
09-24-2002, 06:10 PM
Hi Dan,

Thanks for the tip. I tried your code and it does not produce any errors but nothing else either!

The code is in three parts:

1) Get the ten latest news stories from the database and list the rest within a next/previous page structure
2) put up a drop down box with the news categories available from the database, after checking first with the database
3) If a user selects one of the categories from the drop down list then display the results as links

It works basically, just can't get no.3 to work because my little variable is not getting passed on! :mad:

dank
09-24-2002, 06:47 PM
Hmm, I don't see anything that would keep the variable from being passed through (the style of coding is much different than my own, so I'm having a bit of a hard time looking for clues as to what might be missing), so I would try echoing $query to the screen upon submission to see what, if anything, it is receiving.

Dan

PaulKroll
09-24-2002, 11:19 PM
It's amazing how hard this was to see. I looked at it about twelve times...
<select name="newsid">

You're not setting a POST variable named "cat_id" to come back . You're setting a POST variable named "newsid" to come back.
:)

dank
09-24-2002, 11:30 PM
Brilliant! I actually looked for that very thing, but I was thrown off by "<option value=$cat_id>$cat_name</option>";. I suppose it would help to have $cat_id in the right place, huh? :)

Dan

js
09-25-2002, 03:30 AM
Wow that's great! Can't believe I missed that! Thanks Dan and Paul for helping out. It works now beautifully.

The first part of the code is from a Kevin Yank article on Sitepoint for returning next/previous page results, and the rest is also borrowed as I did not know how to get a drop down box based on what was in the database. This is why the code looks messy - I just have to clean it up now.

One thing from the SQL statement - I tried this but it doesnt work:

$queryÂ*=Â*"SELECTÂ*article_title,Â*id,Â*cat_nameÂ*FROMÂ*NewsÂ*WHEREÂ*cat_id *= '$_POST[cat_id]'";

Dan's sql does work:

$query = "SELECT article_title, id, cat_name FROM News WHERE cat_id = '". $_POST[cat_id] ."'";

Shouldn't the first statement work (what's wrong with it, I get a parse error)? What is the difference?

js
09-25-2002, 03:32 AM
This one didn't work either:

$queryÂ*=Â*"SELECTÂ*article_title,Â*id,Â*cat_nameÂ*FROMÂ*NewsÂ*WHEREÂ*cat_id *= '$_POST['cat_id']'";

PaulKroll
09-25-2002, 10:24 AM
PHP Rules on parsing strings (http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing)
For an array, that means:

$query = " SELECT article_title, id, cat_name FROM News WHERE
cat_id = '{$_POST['cat_id']}'";

Maybe...

dank
09-25-2002, 11:36 AM
Paul's way should work, too. I prefer to "step outside" the quotes for such things to make it a bit easier to follow visually, but same net effect.

Dan