Jump to content

My first script using MySQL - *Frustrated*


Akkari

Recommended Posts

Hi there everyone. Thanks for taking the time to take a look at this.

 

Basically what I'm trying to do is make a simple script that allows users to enter a URL, expiry time and view limit and then get a mydomain.com/links/get.php?id=AUTOLINKID. That loads their specific URL and expires in time. This is my first attempt to use mysql. :)

 

So in short, it's kinda a very very basic URL expiry service...lol.

 

So here's the PHP page that gives me all the trouble:

 

<?php
// Establish database connection
mysql_connect("localhost", "db", "pass") or die(mysql_error());
mysql_select_db("akkari_links") or die(mysql_error());

// If there is no link ID, that saved us a lot of effort; Kick'em out!
if (!@$_GET['id']) die ('No download ID supplied!');
$dl_id = mysql_real_escape_string($_GET['id']);

// Get the database row that corresponds to the link ID. 
$result = mysql_query("SELECT * FROM expire
WHERE id='$dl_id'") or die(mysql_error()); 

// No comment 
$array = mysql_fetch_array($result) or die(mysql_error());

// Otherwise, there is an id and FOR NOW, we'll assume it's valid.
if ($array['expires'] > time() && $array['viewcount'] < $array['viewcap']){
// Looks a little compicated, but really just fetching the URL using iframes that's all...lol.
$array['path'] = $path;
echo "<!DOCTYPE html>
<html>
    <head>
<meta name=\"robots\" content=\"noindex\">
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
    </head>
    <body>
        <div id=\"root\">
            <iframe src=\"$path\">
                Your browser does not support inline frames.
            </iframe>
        </div>
<style>
@charset \"utf-8\";

body {
    margin: 0px;
    padding: 0px;
}

/* iframe's parent node */
div#root {
    position: fixed;
    width: 100%;
    height: 100%;
}

/* iframe itself */
div#root > iframe {
    display: block;
    width: 100%;
    height: 100%;
    border: none;
}
</style>
    </body>
</html>";
// Increase view count by one.
$array['viewcount'] = $viewcount;
$viewcount++;
// Now store new value in database.
$array['viewcount'] = $viewcount;
mysql_query("UPDATE expire SET viewcount='$viewcount' WHERE id='$dl_id'") or die(mysql_error()); 

}
else{
// Time limit or view cap exceeded, throw 'em out.
echo $array['errormessage'];
}
mysql_close();
?>

 

 

Now problems time:

 

1. When you visit get.php?id=VALIDID, nothing happens. Just a white screen. I wish there was some kind of error :(

 

2. the viewcount never gets incremented in the database for some reason.

 

Expected questions:

 

iframe code?

 

Haven't written it myself but worked perfectly well for me in other scripts so I just copied it.

 

Why not use header function?

 

I don't the real URL to stay hidden. Header will just make a redirect. And anyway, I tried replacing iframe code with header() and it still throws a white page at me :(. If you have another solution to this, please point it out!

 

I thought it may also be helpful to state that the whole code executes successfully. Tried to echo "done!"; at the end of the file and it echoed.

 

Thank you very much for the support! :D

Link to comment
https://forums.phpfreaks.com/topic/204587-my-first-script-using-mysql-frustrated/
Share on other sites

First, to get any error messages, add these two lines of code to the very top of your scripts

error_reporting(E_ALL);
ini_set('display_errors', '1');

 

This line of code looks like it is backwards:

$array['path'] = $path;
// Shouldn't that be
$path = $array['path'];

 

This is way too complicated (and partially backwards as well)

// Increase view count by one.
$array['viewcount'] = $viewcount; // SHOULDN'T THIS BE TURNED AROUND?
$viewcount++;
// Now store new value in database.
$array['viewcount'] = $viewcount;
mysql_query("UPDATE expire SET viewcount='$viewcount' WHERE id='$dl_id'") or die(mysql_error()); 

 

You can simplify and correct that with:

// Increase view count by one.
$viewcount = ++$array['viewcount'];
mysql_query("UPDATE expire SET viewcount='$viewcount' WHERE id='$dl_id'") or die(mysql_error()); 

Yeah, you have your assignment statements backwards.  Remember: you're putting the value of the right operand in the left operand.

 

Also, using the '@' symbol with $_GET to squelch errors only to use it in a conditional to catch an error is ass backwards logic.  Simply use isset.

Hey thanks a lot everyone.

 

Just changing the "backwards" statements did the trick. So indeed it was the weekend then, the reason for this  :P

 

lol, I'm embarrassed. Everything else is fine and of course, the script threw no errors at me at all so I just couldn't figure out what was going on.

 

Again, thanks again! :D

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.