Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-26-2004, 03:57 PM   PM User | #1
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
Making a Linktracker

What I am going to do is pass information through the HTTP Addess. I am using variables $des,$cat, and $link

$des = name of site
$link = address of site
$cat = equals category of site

I am wanting to see if field des has something that matches $des and if it does, add one to field hits

If field des has nothing like $des, then enter the info and set it to one, where if clicked again, it will add one.

Here is the code I have so far
PHP Code:
<?php

$cat 
$_GET['cat'];
$des $_GET['des'];
$link $_GET['link'];

$conn mysql_connect("localhost","sdpeele","8590481");

$db mysql_select_db("db");

$result MYSQL_QUERY("SELECT * FROM linktracker WHERE des='$des'");

$sql mysql_fetch_array($result);
$des $sql['des'];

if(
$des){
    
$result MYSQL_QUERY("UPDATE linktracker SET `hits`=`hits`+1 WHERE `des`='$des'");
}
elseif(!
des){
    
$result MYSQL_QUERY("INSERT INTO `linktracker`(`id`,`des`,`cat`,`href`,`hits`) VALUES('NULL','$cat','$des','$link','1')");
    
}
?>
Thanks for any help....
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 01-26-2004, 04:09 PM   PM User | #2
coffeedemon
New Coder

 
Join Date: Oct 2003
Location: AZ
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
coffeedemon is an unknown quantity at this point
Re: Making a Linktracker

PHP Code:
<?php

$cat 
$_GET['cat'];
$des $_GET['des'];
$link $_GET['link'];

$conn mysql_connect("localhost","sdpeele","8590481");

$db mysql_select_db("db");

$result MYSQL_QUERY("SELECT * FROM linktracker WHERE des='$des'");

$sql mysql_fetch_array($result);
$resultsNum mysql_numrows($result# returns number of results

# if resultsNum is greater or equal to 1
if($resultsNum >= 1){
    
$result MYSQL_QUERY("UPDATE linktracker SET `hits`=`hits`+1 WHERE `des`='$des'")or die(mysql_error)); # if query fails return error
}
else
{
# if resultsNum is not greater then or equal to 1 then it must be 0
    
MYSQL_QUERY("INSERT INTO `linktracker`(`id`,`des`,`cat`,`href`,`hits`) VALUES('NULL','$cat','$des','$link','1')")or die(mysql_error)); # if query fails return error
    
}
?>
__________________
PHP | FreeBSD | MySQL |
Check documentation... check documentation again and actually read it..do it... doesn't work ask.

Last edited by coffeedemon; 01-26-2004 at 04:11 PM..
coffeedemon is offline   Reply With Quote
Old 01-26-2004, 04:13 PM   PM User | #3
fimi
New Coder

 
Join Date: Dec 2003
Posts: 80
Thanks: 0
Thanked 0 Times in 0 Posts
fimi is an unknown quantity at this point
i don't have your answer but i would make another field called link_id and assign an id to the link so that
when you do:
$result = MYSQL_QUERY("SELECT * FROM linktracker WHERE des='$des'");

it would be:
$result = MYSQL_QUERY("SELECT * FROM linktracker WHERE link_id='$l_id'");


matching numbers are the easiest and fastest, and using your method if and only if one char is diff it will be added into mysql...

its just a sugesstion,
fimi
fimi is offline   Reply With Quote
Old 01-26-2004, 05:02 PM   PM User | #4
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
Here is my code, thanks to coffeedemon
PHP Code:
<?php

$cat 
$_GET['cat'];
$des $_GET['des'];
$link $_GET['link'];

$conn mysql_connect("localhost","sdpeele","8590481");

$db mysql_select_db("db",$conn);

$result MYSQL_QUERY("SELECT * FROM linktracker WHERE des='$des'");

$sql mysql_fetch_array($result);
$resultsNum mysql_num_rows($result);

if(
$resultsNum >= 1){
    
$result MYSQL_QUERY("UPDATE linktracker SET `hits`=`hits`+1 WHERE `des`='$des'") or die(mysql_error());
}
else
{
    
MYSQL_QUERY("INSERT INTO `linktracker`(`id`,`des`,`cat`,`href`,`hits`) VALUES('NULL','$cat','$des','$link','1')") or die(mysql_error());
    
}
?>
Now, it is inserting the proper data, but its not adding one to hits. Its creating the row over and over again not adding 1 to 'hits' if it is more than one. If you dont understand, see this - http://www.net-riches.com/pics/db.JPG -

Thanks for all help...
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 01-26-2004, 07:46 PM   PM User | #5
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
You could just as well replace all this
PHP Code:
$result MYSQL_QUERY("SELECT * FROM linktracker WHERE des='$des'");

$sql mysql_fetch_array($result);
$resultsNum mysql_num_rows($result);

if(
$resultsNum >= 1){
    
$result MYSQL_QUERY("UPDATE linktracker SET `hits`=`hits`+1 WHERE `des`='$des'") or die(mysql_error());
}
else
{
    
MYSQL_QUERY("INSERT INTO `linktracker`(`id`,`des`,`cat`,`href`,`hits`) VALUES('NULL','$cat','$des','$link','1')") or die(mysql_error());
    

with
PHP Code:
$result mysql_query("REPLACE linktracker SET `hits`=(`hits`+1) WHERE `des`='" $des "'") or die(mysql_error()); 
Replace will check if the record exists and update it or insert a new record. 'des' should then have a unique index or should be the primary key
http://www.mysql.com/doc/en/REPLACE.html
raf is offline   Reply With Quote
Old 01-26-2004, 07:50 PM   PM User | #6
fimi
New Coder

 
Join Date: Dec 2003
Posts: 80
Thanks: 0
Thanked 0 Times in 0 Posts
fimi is an unknown quantity at this point
Quote:
Originally posted by raf
You could just as well replace all this
PHP Code:
$result MYSQL_QUERY("SELECT * FROM linktracker WHERE des='$des'");

$sql mysql_fetch_array($result);
$resultsNum mysql_num_rows($result);

if(
$resultsNum >= 1){
    
$result MYSQL_QUERY("UPDATE linktracker SET `hits`=`hits`+1 WHERE `des`='$des'") or die(mysql_error());
}
else
{
    
MYSQL_QUERY("INSERT INTO `linktracker`(`id`,`des`,`cat`,`href`,`hits`) VALUES('NULL','$cat','$des','$link','1')") or die(mysql_error());
    

with
PHP Code:
$result mysql_query("REPLACE linktracker SET `hits`=(`hits`+1) WHERE `des`='" $des "'") or die(mysql_error()); 
Replace will check if the record exists and update it or insert a new record. 'des' should then have a unique index or should be the primary key
http://www.mysql.com/doc/en/REPLACE.html
LOL, i really like this.... i didn't know about REPLACE. 6 lines turned into 1
thx for the tip

Last edited by fimi; 01-26-2004 at 07:54 PM..
fimi is offline   Reply With Quote
Old 01-26-2004, 07:50 PM   PM User | #7
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
In my table 'id' is my primary key.

I'm kinda confused raf, can you show me the complete code and not just what to replace?

Thanks
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 01-26-2004, 08:08 PM   PM User | #8
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
PHP Code:
$cat $_GET['cat'];
$des $_GET['des']; // no need for this intermediate variable
$link $_GET['link'];

/*these should go inside an include. from here */
$conn mysql_connect("localhost","sdpeele","8590481");

$db mysql_select_db("db",$conn);
/*to here */
$result mysql_query("REPLACE linktracker SET `hits`=(`hits`+1) WHERE `des`='" $des "'") or die(mysql_error()); 
You see, it could actualy be just 2 lines of code (one for the include and one for the query.)

Now, replace need to know which record to look for to decide if it exists.
If all values from 'des' are unique, then you can create a unique index on it. Or else you need to use the id (so the querystring should contain the id value then)
Since each link can only point to one site, there shoukd be at least one 'no duplicate values' column inside your table. No ?
raf is offline   Reply With Quote
Old 01-26-2004, 08:31 PM   PM User | #9
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
Now, I'm really confuses.

The replace function throws me for a loop.

All I want to do is -> Check for matching value ($des), if matching value exists add one to 'hits' if matching value does not exist, then create the field.

Errr, sorry if I am being a pain, buts thats kinda confusing.

Thanks for the help...
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 01-26-2004, 08:37 PM   PM User | #10
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Not that I want to alarm anyone, but shouldn't we NOT give out our username and password???
bcarl314 is offline   Reply With Quote
Old 01-26-2004, 08:48 PM   PM User | #11
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
Yes, but my host is pretty secure. You have to login into the control panel, then login to database management, and then login to phpmyadmin, (all three have different usernames and passwords)
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 01-26-2004, 08:55 PM   PM User | #12
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally posted by SDP2006
Now, I'm really confuses.

The replace function throws me for a loop.
How could that result in a loop? It will just execute the query once. If you get a loop, then it must be some othr code around the one you posted
Quote:
Originally posted by SDP2006
All I want to do is -> Check for matching value ($des), if matching value exists add one to 'hits' if matching value does not exist, then create the field.
Check the link in my previous post and you'll see it does exactly that.

And bcarl314 is right about the user and pwd: All we need to do is take an account on the same host and we can manipulate your db. I don't see why we would need phpmyadmin.
raf is offline   Reply With Quote
Old 01-26-2004, 08:57 PM   PM User | #13
fimi
New Coder

 
Join Date: Dec 2003
Posts: 80
Thanks: 0
Thanked 0 Times in 0 Posts
fimi is an unknown quantity at this point
Quote:
Originally posted by bcarl314
Not that I want to alarm anyone, but shouldn't we NOT give out our username and password???
test it in your server, see if you can use it :
/*these should go inside an include. from here */
$conn = mysql_connect("localhost","sdpeele","8590481");

lol, that point is that it dosent matter, they do it on their own risk

Last edited by fimi; 01-26-2004 at 08:59 PM..
fimi is offline   Reply With Quote
Old 01-26-2004, 08:59 PM   PM User | #14
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
So....
PHP Code:
$cat $_GET['cat'];
$des $_GET['des']; // no need for this intermediate variable
$link $_GET['link'];

/*these should go inside an include. from here */
$conn mysql_connect("localhost","sdpeele","8590481");

$db mysql_select_db("db",$conn);
/*to here */
$result mysql_query("REPLACE linktracker SET `hits`=(`hits`+1) WHERE `des`='" $des "'") or die(mysql_error()); 
will do all i need it to do?
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 01-26-2004, 09:06 PM   PM User | #15
fimi
New Coder

 
Join Date: Dec 2003
Posts: 80
Thanks: 0
Thanked 0 Times in 0 Posts
fimi is an unknown quantity at this point
AND...

Quote:
Originally posted by raf
Now, replace need to know which record to look for to decide if it exists.
If all values from 'des' are unique, then you can create a unique index on it. Or else you need to use the id (so the querystring should contain the id value then)
Since each link can only point to one site, there shoukd be at least one 'no duplicate values' column inside your table. No ?
fimi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:03 AM.


Advertisement
Log in to turn off these ads.