Page 1 of 3

Webcomic Script Problem

Posted: Sun Jan 30, 2005 5:07 pm
by Dan++
I'm creating a script to display different strips of a webcomic. It's all going okay so far (as you can see), except for a few issues.

I want the default value when viewcomic.php is run to be the latest comic (i.e. the greatest value of comicID in the table), and so that when you are on the latest comic the 'next' link does not work, and on the first comic the 'previous' button does not work.

I tried an IF statement but I had trouble getting the URL within that to work. So, can anyone help me with any of these problems? Thanks in advance if you can. Here's the script at the moment, which is located at http://www.silverferret.co.uk/sc17/view ... ?comicID=1 :

Code: Select all

<?php
    $dbhost = 'localhost';
    $dbname = 'silverf_website';
    $dbuser = 'silverf_dan';
    $dbpasswd = '*******';
    $connection=mysql_connect($dbhost,$dbuser,$dbpasswd)
        or die("Could not connect to server");
    $db=mysql_select_db($dbname,$connection)
        or die("Could not select database");
    $comicID = $_GET&#1111;'comicID'];
    $query="SELECT * FROM Comic WHERE comicID=$comicID";
    $result=mysql_query($query)
        or die ("Could not execute query");
    //Get the results
    $row=mysql_fetch_array($result);
    extract($row);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Spin City 17 - <?php print($comicTitle); ?></title>
</head>
<body>
<?php print($comicTitle);?><br>
<a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID+1)); ?>">Next</a> | <a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID-1)); ?>">Previous</a>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>

Posted: Sun Jan 30, 2005 7:58 pm
by Chris Corbyn
Take a look at mysql_num_rows() then you'll be able to get the data in the last row so you can build your if statement. Also, the data in row 0 will give you the first row.

Posted: Sun Jan 30, 2005 8:01 pm
by magicrobotmonkey
that won't work. He is only selecting one row from the db at a time so mysql_num_rows will always be 1

Posted: Sun Jan 30, 2005 8:15 pm
by hunterhp
Use this to get the default value to be the most recent id

Code: Select all

<?php
    $dbhost = 'localhost';
    $dbname = 'silverf_website';
    $dbuser = 'silverf_dan';
    $dbpasswd = '*******';
    $connection=mysql_connect($dbhost,$dbuser,$dbpasswd)
        or die("Could not connect to server");
    $db=mysql_select_db($dbname,$connection)
        or die("Could not select database");
     if (!emtpy($_GET&#1111;'comicID'])) &#123;
    $comicID = $_GET&#1111;'comicID'];
    $query="SELECT * FROM Comic WHERE comicID=$comicID";
    &#125;
    else
    &#123;
    $query = "SELECT * FROM Comics ORDERBY id DESC";
    &#125; 
    $result=mysql_query($query)
        or die ("Could not execute query");
    //Get the results
    $row=mysql_fetch_array($result);
    extract($row);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Spin City 17 - <?php print($comicTitle); ?></title>
</head>
<body>
<?php print($comicTitle);?><br>
<a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID+1)); ?>">Next</a> | <a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID-1)); ?>">Previous</a>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
I haven't tested the code, but it should work. Alternatively, you can chang !empty with !isset

Posted: Sun Jan 30, 2005 9:14 pm
by thegreatone2176
they seemed to answer your orginial question but i wanted to point out that this code is very unsecure

you simply assign the variable without modifying it at all. Search the forum for thing such as str_replace addslashes and check the security section

Posted: Mon Jan 31, 2005 1:37 am
by Dan++
hunterhp wrote:Use this to get the default value to be the most recent id

Code: Select all

<?php
    $dbhost = 'localhost';
    $dbname = 'silverf_website';
    $dbuser = 'silverf_dan';
    $dbpasswd = '*******';
    $connection=mysql_connect($dbhost,$dbuser,$dbpasswd)
        or die("Could not connect to server");
    $db=mysql_select_db($dbname,$connection)
        or die("Could not select database");
     if (!emtpy($_GET&#1111;'comicID'])) &#123;
    $comicID = $_GET&#1111;'comicID'];
    $query="SELECT * FROM Comic WHERE comicID=$comicID";
    &#125;
    else
    &#123;
    $query = "SELECT * FROM Comics ORDERBY id DESC";
    &#125; 
    $result=mysql_query($query)
        or die ("Could not execute query");
    //Get the results
    $row=mysql_fetch_array($result);
    extract($row);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Spin City 17 - <?php print($comicTitle); ?></title>
</head>
<body>
<?php print($comicTitle);?><br>
<a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID+1)); ?>">Next</a> | <a href="<?php print('http://www.silverferret.co.uk/sc17/viewcomic.php?comicID='.($comicID-1)); ?>">Previous</a>
<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>
I haven't tested the code, but it should work. Alternatively, you can chang !empty with !isset
Tried both, but they don't seem to work. I get a "Could not execute query" error. It's odd, since it does look like it should work. I even tried $query='SELECT * FROM Comics ORDER BY DESC comicID'; - how very odd. Thanks anyway.
thegreatone2176 wrote:they seemed to answer your orginial question but i wanted to point out that this code is very unsecure

you simply assign the variable without modifying it at all. Search the forum for thing such as str_replace addslashes and check the security section
I realise it's not totally secure yet. For one I need to ensure that typing in strings won't mess it up, and typing in a number too high will go to the latest one.

What do you mean about modifying the variable though?

Also, I would like the 'next' link to be disabled on the latest strip and the 'previous' link to be disabled on the first, if anyone could help with that, too.

Thanks for the help so far, by the way!

Posted: Mon Jan 31, 2005 7:12 am
by feyd

Code: Select all

SELECT * FROM `Comics` ORDER BY `comicID` DESC

Posted: Mon Jan 31, 2005 12:22 pm
by Dan++
It's not working - however, I think I may be close now:

Code: Select all

<?php
    $dbhost = 'localhost';
    $dbname = 'silverf_website';
    $dbuser = 'silverf_dan';
    $dbpasswd = '*******';
    $connection=mysql_connect($dbhost,$dbuser,$dbpasswd)
        or die("Could not connect to server");
    $db=mysql_select_db($dbname,$connection)
        or die("Could not select database");
    if (empty($_GET&#1111;'comicID']))
    &#123;
        $query='SELECT MAX(comicID) FROM Comic';
    &#125;
    else
    &#123;
        $comicID = $_GET&#1111;'comicID'];
        $query='SELECT * FROM Comic WHERE comicID=$comicID';
    &#125;
    $result=mysql_query($query)
        or die ("Could not execute query");
    //Get the results
    $row=mysql_fetch_array($result);
    extract($row);
?>
I've found using a MAX clause almost works - however, the results are not quite what I wanted. The 'Query could not be executed' message is not displayed. See for yourself. It's showing most parts, but it's supposed to go to comicID=2, showing the comic entitled "Sunrise 2". I've tried out a few things but it's not working. The part you want to be looking at for the error is in the first 'if' statement.

Thanks to whoever can helped or has helped so far.

Posted: Mon Jan 31, 2005 1:23 pm
by timvw
actually, feyd's query DOES work

Code: Select all

//the most recent comic:
SELECT * FROM Comics ORDER BY comicID DESC LIMIT 1

//the requested comic:
SELECT * FROM Comics WHERE comcID=$comicid LIMIT 1

// the following comic:
SELECT * FROM Comics WHERE comicID > $comicid ORDER BY comicID ASC LIMIT 1

// the previous comic:
SELECT * FROM Comics WHERE comicID < $comicID ORDER BY comicID DESC LIMIT 1

Posted: Mon Jan 31, 2005 3:11 pm
by Dan++
timvw wrote:actually, feyd's query DOES work

Code: Select all

//the most recent comic:
SELECT * FROM Comics ORDER BY comicID DESC LIMIT 1

//the requested comic:
SELECT * FROM Comics WHERE comcID=$comicid LIMIT 1

// the following comic:
SELECT * FROM Comics WHERE comicID > $comicid ORDER BY comicID ASC LIMIT 1

// the previous comic:
SELECT * FROM Comics WHERE comicID < $comicID ORDER BY comicID DESC LIMIT 1
Well I'd agree, it should work, but this doesn't work:

Code: Select all

<?php
    $dbhost = 'localhost';
    $dbname = 'silverf_website';
    $dbuser = 'silverf_dan';
    $dbpasswd = '*******';
    $connection=mysql_connect($dbhost,$dbuser,$dbpasswd)
        or die("Could not connect to server");
    $db=mysql_select_db($dbname,$connection)
        or die("Could not select database");
    if (empty($_GET&#1111;'comicID']))
	&#123;
		$query='SELECT * FROM Comics ORDER BY comicID DESC LIMIT 1';
    &#125;
    else
    &#123;
    	$comicID = $_GET&#1111;'comicID'];
    	$query='SELECT * FROM Comic WHERE comicID=$comicID';
    &#125;
    $result=mysql_query($query)
        or die ("Could not execute query");
    //Get the results
    $row=mysql_fetch_array($result);
    extract($row);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Spin City 17 - <?php print($comicTitle); ?></title>
</head>
<body>
<?php print($comicTitle);?><br>
<a href="<?php print('?comicID='.($comicID+1)); ?>">Next</a> | <a href="<?php print('?comicID='.($comicID-1)); ?>">Previous</a>

<br>
<img src="<?php print($comicURL); ?>" alt="Spin City 17">
</body>
</html>

Posted: Mon Jan 31, 2005 3:42 pm
by John Cartwright
change

Code: Select all

or die ("Could not execute query");
to

Code: Select all

or die ("Could not execute query: ". mysql_error());

Posted: Mon Jan 31, 2005 3:43 pm
by thegreatone2176
try putting

$result=mysql_query($query) or die (mysql_error());

instead of your custom error it will show you whats going wrong

Posted: Mon Jan 31, 2005 3:56 pm
by Dan++
Aha!
Could not execute query: Table 'silverf_website.Comics' doesn't exist
What a stupid mistake. The table is actually called 'Comic' not 'Comics'. Heh. Thanks!

Oh <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>. On going to http://www.silverferret.co.uk/sc17/view ... ?comicID=1 it now says:

Could not execute query: Unknown column '$comicID' in 'where clause'

What the hell is going on? :?

Posted: Mon Jan 31, 2005 4:03 pm
by thegreatone2176
strange phenom we posted the same thign at the same time

anyway

in your query you have

Code: Select all

$query='SELECT * FROM Comic WHERE comicID=$comicID'
and your error message says $comicID not an value so you need to make it

Code: Select all

$query="SELECT * FROM Comic WHERE comicID= '$comicID' ";
that way it will read the value of the variable

Posted: Mon Jan 31, 2005 4:26 pm
by Dan++
thegreatone2176 wrote:strange phenom we posted the same thign at the same time

anyway

in your query you have

Code: Select all

$query='SELECT * FROM Comic WHERE comicID=$comicID'
and your error message says $comicID not an value so you need to make it

Code: Select all

$query="SELECT * FROM Comic WHERE comicID= '$comicID' ";
that way it will read the value of the variable
Thanks, works great now.

So here's my next problem:

I don't want the 'next' link to display on the latest comic, and I don't want the 'previous' link to display on the first comic.

What I have so far (of the relevant code):
<?php
$nextlink('<a href="?comicID=<?php print('$comicID+1'); ?>">Next</a>');
$prevlink('<a href="?comicID=<?php print('$comicID-1'); ?>">Previous</a>');
print($comicTitle.'<br>');
if ($comicID=1)
{
print($nextlink.' | Previous');
}
elseif ($comicID='SELECT * FROM Comic ORDER BY comicID DESC LIMIT 1')
{
print('Next | '.$prevlink);
}
else
{
print($nextlink.' | '.$prevlink);
}
?>
There is something wrong with it, as you can see.

I know it has something to do with the variables $prevlink and $nextlink, but I can't fix it. Is it the <?php and ?> tags within the variables? I think so...but how can I fix it?

Thanks for all your help so far, I promise that this is the last problem and then this script will be done! Thanks!