0
\$\begingroup\$

I have a project from my college. I'm making a site which will display different quotations each time the page is loaded. I use the following code:

$con = mysql_connect("localhost","root","");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xlsx_db", $con);

$result = mysql_query("SELECT * FROM sheet1 ORDER BY RAND() LIMIT 1");

if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}

$row = mysql_fetch_array($result);
echo "<b>Quote: </b>";
echo $row['quote']."<br>";
echo $row['by']."<br>";
?>

All works fine. But my teacher told me to make the text link-able. So that if anyone copy that link of the quote, that case, he can pest the code on the browser and can see that quote again.

Could anyone please help me with this task? Any kind of help is really appreciated.

\$\endgroup\$
2
1
\$\begingroup\$

Your link should look like this:

<a href="http://www.yoursite.com/quotes?q_id=123">Permalink</a>

In your php code, you can grab the selected quote id like this:

$q_id = $_GET['q_id'];

To check if the q_id has been set:

if (array_key_exists('q_id', $_GET)) {
    // the q_id was specified
}

To check that the q_id is numeric (important to do before plugging it into a mysql query):

if (is_numeric($_GET['q_id'])) {
    // q_id is a number
}

To fetch that single quote:

$result = mysql_query("SELECT * FROM sheet1 WHERE q_id = " . $q_id);

Deprecation of mysql_ functions

PHP functions that start with mysql_ have been deprecated as of PHP 5.5.0. If you are in a position to do so, please consider updating your code to use the MySQLi or PDO extensions instead.

\$\endgroup\$
1
  • \$\begingroup\$ +1 I prefer this as a refresh will still produce a random quote and a link to return to that page. \$\endgroup\$
    – Waygood
    Jul 12 '13 at 14:06
0
\$\begingroup\$

you'd need an ID for each quote in the database and add ?id=1245 after your domain and page

if($_GET['id']){
$result = mysql_query("SELECT * FROM sheet1 WHERE id ".$_GET['id']."");
} else {
    $result = mysql_query("SELECT * FROM sheet1 ORDER BY RAND() LIMIT 1");
}
\$\endgroup\$
2
  • \$\begingroup\$ www.yoursite.com/quotes?id=;DROP TABLE sheet1;-- \$\endgroup\$
    – Joe Frambach
    Jul 12 '13 at 14:08
  • \$\begingroup\$ I use PDO prepared statement i kinda forgot about that \$\endgroup\$
    – Kevin Roy
    Jul 12 '13 at 14:18
0
\$\begingroup\$

Here's a full solution from @eggyals comment

Assuming quote_id is the index on the sheet1 table

<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xlsx_db", $con);
//get quote from url
$quote_id=intval(urldecode($_GET['quote_id']));
// if not set get a random one
if($quote_id==0)
{
    $result = mysql_query("SELECT * FROM sheet1 ORDER BY RAND() LIMIT 1") or die('Could not run query: ' . mysql_error());
    $row = mysql_fetch_array($result);
    $get=$_GET;
    $get['quote_id']=$row['quote_id'];
    // jump to page with link in url, so it can be copied
    header("Location:".$_SERVER['PHP_SELF'].'?'.http_build_query($get));
    exit();
}
// get the specified quote
$result = mysql_query("SELECT * FROM sheet1 WHERE quote_id=".intval($quote_id)." LIMIT 1") or die('Could not run query: ' . mysql_error());
$row = mysql_fetch_array($result);
echo "<b>Quote: </b>";
echo $row['quote']."<br>";
echo $row['by']."<br>";
?>
\$\endgroup\$
2
  • \$\begingroup\$ Hi, thank you very much for the answer. Having a problem. My code returning with an id that is 0. I set the column "quote_id" as index on mysql database. but still show it is 0. and also I'm facing problem of "undefined index: quote_id" \$\endgroup\$
    – Provash Shoumma
    Jul 12 '13 at 17:38
  • \$\begingroup\$ in that case you should $quote_id=0; if(isset($_GET['quote_id'])) $quote_id=intval(urldecode($_GET['quote_id'])); BUT Joe Frambach's answer is better! \$\endgroup\$
    – Waygood
    Jul 15 '13 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.