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);
?>
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);
?>