Web Hosting Talk







View Full Version : Can you find the bug? (Easy Script)


Jeanco
03-27-2004, 07:03 PM
Here's the situation: Right now I have a static title at epasture.com What I want to do is when a particular product, laughable, tutorial, or brainteaser is selected it becomes the title of that page.

I use the ID variable to ID the item from the the database if an item is selected. Every item, regardless of which table it is in has a TITLE record so thats not an issue. The code seems to work fine when there isn't an item selected ($ID < 1). However, when an item is selected it doesn't seem to work. The title doesn't return properly and the whole page is left as a blank white sheet (so no content gets returned either - that code isn't shown here but does work before I tried this so its not the problem).

Here is the code I'm using:

<TITLE>ePasture.com -

<?php /* IF NO TITLE IS DEFINED */
if ($ID < 1) {

echo "Laughables, Brainteasers, Ridiculous Products, Tutorials";

} else { //$ID has a value

if ($content = "laughables.php") { //if we are in the laughables section
$result = mysql_query("SELECT * FROM laughables WHERE ID = {$row['ID']}")
or die("Invalid query: " . mysql_error()); //then select the appropriate laughable

while ($row = mysql_fetch_array($result)) {
echo "{$row['TITLE']}"; // and out put the title
}
} elseif ($content = "brainteasers.php") { //if we are in the brainteasers section
$result = mysql_query("SELECT * FROM brainteasers WHERE ID = {$row['ID']}")
or die("Invalid query: " . mysql_error()); //then select the appropriate brainteaser

while ($row = mysql_fetch_array($result)) {
echo "{$row['TITLE']}"; // and out put the title
}
} elseif ($content = "products.php") { //if we are in the products section
$result = mysql_query("SELECT * FROM products WHERE ID = {$row['ID']}")
or die("Invalid query: " . mysql_error()); //then select the appropriate product

while ($row = mysql_fetch_array($result)) {
echo "{$row['TITLE']}"; // and out put the title
}
} elseif ($content = "tutorials.php") { //if we are in the tutorials section
$result = mysql_query("SELECT * FROM tutorials WHERE ID = {$row['ID']}")
or die("Invalid query: " . mysql_error()); //then select the appropriate tutorial

while ($row = mysql_fetch_array($result)) {
echo "{$row['TITLE']}"; // and out put the title
}
} else {
echo "Laughables, Brainteasers, Ridiculous Products, Tutorials";
}
}
?>


</TITLE>


Please let me know if there is anything else I can provide you to help you help me solve this problem. Any Ideas?

CyberAlien
03-27-2004, 07:33 PM
on this line:if ($ID < 1) {$ID isn't defined. But i assume you are using register_globals so ID is sent in get/post.

on this line:if ($content = "laughables.php")should be == instead of = (same on all other similar lines)

on this line: $result = mysql_query("SELECT * FROM laughables WHERE ID = {$row['ID']}")$row['ID'] isn't defined before so it is empty

in this code:echo "{$row['TITLE']}";what's the point in using double quotes? it only slows down php and makes your code harder to read. use echo $row['TITLE']; instead. (same on all other similar lines)

And you should know that variables are case-sensitive, so $ID and $id are not the same. that might also be an error in your code.

Jeanco
03-27-2004, 07:36 PM
Thanks, I'm gonna try changing those = to == that may be it. When you say $ID isn't defined, that is true for the line $ID < 1, but in the other parts ID should be defined in the url content.php?content=laughables.php&ID=3 for example.

Thanks for the help I'm gonna try your suggestions and let you know.

Jeanco
03-27-2004, 07:43 PM
Its still doing the same thing. I've made all changed you've recommended. If a link would point to one of two options:

content.php?content=something.php

OR

content.php?content=somethingelse.php&ID=5 (or some other number)

Is there something I need to do in the code I oroiginally posted to make it work?


EDIT ADDON: I also use the rewrite_mod (I think thats it) to rewrite how the browser sees dynamic links to make it easier for search engines to index the site. Would the fact that a link like:

epasture.com/content.php?content=laughables.php&ID=28

would show up in the browser as:

epasture.com/laughables28.html

make any difference?

orbitz
03-27-2004, 08:13 PM
I am not good at PHP, but you may try to use:

$ID = $_REQUEST['ID'];

have this line before the rest of the above code.
this will get the value of ID from the link.

Jeanco
03-27-2004, 08:19 PM
I tried your idea orbitz and no luck, sorry.

I found a clue... When I look at the source code the the blank white page I get (this is in the area relevant to the code above):

<TITLE>ePasture.com - Bored? You're in the right spot

Invalid query: You have an error in your SQL syntax. Check the
manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


Here is the PHP code that appears around line one. It has to do
with rewriting the url for search engines. If you need to see the .htaccess code that goes along with this then let me know:

<?php

if ($_GET['rewrite'] <> 1) {
$url = "/".ereg_replace(".php$","",$content).$ID.$op.".html";

header("Location: http://" . $_SERVER['HTTP_HOST'] . "$url") ;
}

?>

Jeanco
03-29-2004, 12:13 AM
Anyone?

Hercule
03-29-2004, 03:48 AM
The error is in line 1 but on your mysql query

I will try something like this:

mysql_query("SELECT * FROM laughables WHERE ID = '$ID' ")

unlucky1
03-29-2004, 08:09 PM
Try this:

Here is the code I'm using:

<TITLE>ePasture.com -

<?php /* IF NO TITLE IS DEFINED */
if ($ID < 1) {

echo "Laughables, Brainteasers, Ridiculous Products, Tutorials";

} else { //$ID has a value

if ($content == "laughables.php") { //if we are in the laughables section
$result = mysql_query("SELECT * FROM laughables WHERE ID =" .$ID)
or die("Invalid query: " . mysql_error()); //then select the appropriate laughable

while ($row = mysql_fetch_array($result)) {
echo "{$row['TITLE']}"; // and out put the title
}
} elseif ($content == "brainteasers.php") { //if we are in the brainteasers section
$result = mysql_query("SELECT * FROM brainteasers WHERE ID =". $ID)
or die("Invalid query: " . mysql_error()); //then select the appropriate brainteaser

while ($row = mysql_fetch_array($result)) {
echo "{$row['TITLE']}"; // and out put the title
}
} elseif ($content == "products.php") { //if we are in the products section
$result = mysql_query("SELECT * FROM products WHERE ID = ". $ID)
or die("Invalid query: " . mysql_error()); //then select the appropriate product

while ($row = mysql_fetch_array($result)) {
echo "{$row['TITLE']}"; // and out put the title
}
} elseif ($content == "tutorials.php") { //if we are in the tutorials section
$result = mysql_query("SELECT * FROM tutorials WHERE ID = ". $ID)
or die("Invalid query: " . mysql_error()); //then select the appropriate tutorial

while ($row = mysql_fetch_array($result)) {
echo "{$row['TITLE']}"; // and out put the title
}
} else {
echo "Laughables, Brainteasers, Ridiculous Products, Tutorials";
}
}
?>


</TITLE>


Let me know what happens.

Jeanco
03-29-2004, 08:25 PM
Thanks everyone I got it working with a little help from someone here who was nice enough to assist me via email. Here is the solution in case someone can use it on their sites later:


<?php
if (!isset($_GET['ID'])) {
$title="Laughables, Brainteasers, Ridiculous Products, Tutorials";
} else {
$curid=$_GET['ID'];
$content=$_GET['content'];
if ($content == "laughables.php") {
$result = mysql_query("SELECT * FROM laughables WHERE ID = $curid")
or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array($result)) {
$title=$row['TITLE'];
}
} elseif ($content == "brainteasers.php") {
$result = mysql_query("SELECT * FROM brainteasers WHERE ID = $curid")
or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array($result)) {
$title=$row['TITLE'];
}
} elseif ($content == "products.php") {
$result = mysql_query("SELECT * FROM products WHERE ID = $curid")
or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array($result)) {
$title=$row['TITLE'];
}
} elseif ($content == "tutorials.php") {
$result = mysql_query("SELECT * FROM tutorials WHERE ID = $curid")
or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array($result)) {
$title=$row['TITLE'];
}
} else {
$title="Laughables, Brainteasers, Ridiculous Products, Tutorialsxxxxx";
}
}

echo $title;

?>

srinipm
03-30-2004, 04:11 AM
Hi Jeanco,
Contact me on MSN srinipm@hotmail.com or aim - pmsrini.
Thanks and looking forward to hear from you...Srinivas
URL : www.jinis.com
Mail:srinivas@jinis.com