Problem with database submit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wee493
Forum Newbie
Posts: 22
Joined: Sat Sep 06, 2008 3:05 pm

Problem with database submit

Post by wee493 »

I'm running a site that creates tiny urls. I want for users to be able to go to a link like http://www.mysite.com/create.php?url=http://google.com

I already have the get variable setup and working. I'm trying to discover why the url is not properly being submitted into the databse. I've been looking over the script for hours and i cant seem to find the problem :(

Any help will be very helpful!

Code: Select all

 
<?
 
include_once 'master_config.inc.php';
include_once 'db.class.inc.php';
include_once 'HtmlTemplate.class.php';
 
$page=new HtmlTemplate("templates/index.htm");
 
/* $rnd_url=chr(rand(97,122)).rand(0,9).chr(rand(65,90)).chr(rand(97,122)).rand(0,9);
$newurl = $_GET['newurl'];
if (empty($newurl)) {
    $newurl = $rnd_url;
} */
 
$db=new db($DB_LOCALHOST,$DB_DATABASE,$DB_USERNAME,$DB_PASSWORD);
$db1=new db($DB_LOCALHOST,$DB_DATABASE,$DB_USERNAME,$DB_PASSWORD);
 
// take care of register global
foreach($_POST AS $key => $value) { ${$key} = $value; }
foreach($_POST AS $key => $value) { ${$key} = $value; }
 
$url=$_GET["url"];
 
if($_GET["url"]){
    $page->Set("url",$_GET["url"]);
    // check up again DB for url URL
    if($db->query("SELECT * FROM url WHERE url='".$_GET["url"]."'")){
        // url exist
        $db->fetch();
        $mini_url=$db->row["url"];
        $page->Set("mini_url","$SITE_URL/$mini_url");
        $page->Set("message","This URL has been shortened before, here it is again:");
    } else {
        // url does not exist, create new mini url          
        // generate random mini url and check against url database for existence
        do {
            
        $rnd_url=chr(rand(97,122)).rand(0,9).chr(rand(65,90)).chr(rand(97,122)).rand(0,9);
            
        } while ($db->query("SELECT id FROM url WHERE url='$newurl'"));
        if(!mysql_query("INSERT INTO url (url, url) VALUES ('$rnd_url','$url')")){
            // insert failed
            $page->Set("message","Database INSERT Failed.  Please contact Webmaster.");
        } else {
            $page->Set("mini_url","$SITE_URL/$rnd_url");
            $page->Set("message","The WiURL URL is:");
        }
    }
    $page->Set("result",$page->CreatePage("templates/result.htm"));
    
} else {
    // redirect
    if(is_dir($show)){
        header("location: $SITE_URL/$show");
    } else {
        // check up against DB for URL
        // if adding .gif or .jpg, then still process redirect, but ignore .gif or .jpg
        $ext=substr($show,-4,4);
        if($ext=='.gif') $show=str_replace('.gif','',$show);
        if($ext=='.jpg') $show=str_replace('.jpg','',$show);
        if($db->query("SELECT url FROM url WHERE url='$show'")){
            // url exist
            $db->fetch();
            header('location: '.$db->row["url"]);
        } else {
            // invalid short URL, dispaly homepage
        }
    }
}
 
echo $url;
echo "  ";
echo "Your URL is http://wiurl.com/";
echo $rnd_url;
 
 
?>
 
Post Reply