telt-kliks-op-url

Gesponsorde koppelingen

PHP script bestanden

  1. telt-kliks-op-url

« Lees de omschrijving en reacties

sql:
CREATE TABLE 'links' (
'id' INT NOT NULL AUTO_INCREMENT ,
'name' VARCHAR( 100 ) NOT NULL ,
'link' VARCHAR( 100 ) NOT NULL ,
'kliks' INT NOT NULL ,
PRIMARY KEY ( 'id' )
);

index.php:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
// Kliks on links?
// how many kliks are there on this link?
//
// Author: Jeroen Boersma.
// Email: Pianoplayer[@]planet.nl
//
// Files: Index.php
//        Admin.php
//
// Don't delete this header!


$error_no_link="<b>Error:</b> No such name with link in database, Please make sure you entered the right name!"; // make this your own error.
$error_no_get_data="Nothing in Url"; // make this also your own error.
$error_no_kliks_update="There is some problem with counting the kliks."; // make this also your own error.

if(isset($_GET['name'])){

   $name=$_GET['name'];

      // connection with database
      $db=mysql_connect("localhost","root","");
      mysql_select_db("test",$db);

      // sql
      $sql="SELECT name,link,kliks FROM links WHERE name='$name' LIMIT 0,1";
      $res=mysql_query($sql);

     // is there any link with this name?
     $num_rows=mysql_num_rows($res);
     if($num_rows==1){
     // yes there is.
        $link=mysql_fetch_array($res);
        
        $new_number_of_kliks=$link['kliks']+1;
        $sql="UPDATE links SET kliks='$new_number_of_kliks' WHERE name='$name'";
        $res=mysql_query($sql);
        if(!$res) die($error_no_kliks_update);
        
           header("Location: " . $link['link']);
     }

     else{
     // no there isn't.
        echo $error_no_link;
     }

     mysql_close();
}

else{
   echo $error_no_get_data;
}


?>


Admin.php:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
// Kliks on links?
// how many kliks are there on this link?
//
// Author: Jeroen Boersma.
// Email: Pianoplayer[@]planet.nl
//
// Files: Index.php
//        Admin.php
//
// Don't delete this header!

// errors

$error_no_link_in_database="There are no links in the database"; // make this your own error.


$self=$_SERVER['PHP_SELF'];
// connection with database
$db=mysql_connect("localhost","root","");
mysql_select_db("test",$db);

// some functions
Function DeleteLink($id){
   $sql="DELETE FROM links WHERE id='$id'";
   $res=mysql_query($sql);
      if($res) echo "Your link is deleted.";
      if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem.";
}

Function
NewLink($name,$link){
   $sql="INSERT INTO links (name,link) VALUES ('$name','$link')";
   $res=mysql_query($sql);
   if($res) echo "New link submitted.";
   if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem.";
}

Function
UpdateLink($id,$name,$link){
   $sql="UPDATE links SET id='$id',name='$name',link='$link' WHERE id='$id'";
   $res=mysql_query($sql);
   if($res) echo "Link is updated";
   if(!$res) echo "<b>Error:</b> Try aggain please. There is a problem.";
}

Function
ShowUpdateForm($id){
global $self;
   $sql="SELECT * FROM links WHERE id='$id' LIMIT 0,1";
   $res=mysql_query($sql);
  
   $num_rows=mysql_num_rows($res);
  
   if($num_rows==1){
  
      $link=mysql_fetch_array($res);

     $name=$link['name'];
     $link=$link['link'];
echo<<<UPDATEFORM
<form action=$self method=POST>
<input type=hidden name=id value=$id />
 <table>
  <tr><td>Name</td><td><input type=text name=name value="$name" /></td></tr>
  <tr><td>Link</td><td><input type=text name=url value="$link" /></td></tr>
  <tr><td colspan=2><input type=submit value=Change name=link /></td></tr>
 </table>
</form>
UPDATEFORM
;

   }

   else{
      echo "This link isn't in the database, or there is a problem.";
   }
}


// nu kijken wat er moet gebeuren.
if(isset($_POST['link']) and $_POST['link']=="Insert"){
   NewLink(addslashes($_POST['name']),$_POST['url']);
}

elseif(isset($_POST['link']) and $_POST['link']=="Update"){
   ShowUpdateForm($_POST['id']);
}

elseif(isset($_POST['link']) and $_POST['link']=="Delete"){
   DeleteLink($_POST['id']);
}

elseif(isset($_POST['link']) and $_POST['link']=="Change"){
   UpdateLink($_POST['id'],addslashes($_POST['name']),$_POST['url']);
}


$sql="SELECT * FROM links ORDER BY name";
$res=mysql_query($sql);

   $num_rows=mysql_num_rows($res);
  
   if($num_rows>=1){
   // table
   echo "<table>";
   echo "<tr><td><b>Name:</b></td><td><b>Link:</b></td><td><b>Number of kliks:</b></td><td></td></tr>";

      // row(s)
      while($link=mysql_fetch_array($res)){
         echo "<tr><td>" . $link['name'] . "</td><td>" . $link['link'] . "</td><td>" . $link['kliks'] . "</td><td><form action=" . $_SERVER['PHP_SELF'] . " method=POST><input type=hidden name=id value=" . $link['id'] . " /><input type=submit value=Update name=link /> <input type=submit value=Delete name=link /></form></td></tr>";
      }

   echo "</table><hr size=1 />";
   }

   else{
      echo $error_no_link_in_database;
   }

echo <<<NEWFORM
<form action=$self method=POST />
<table>
 <tr><td>Name:</td><td><input type=text name=name /></td></tr>
 <tr><td>Link:</td><td><input type=text name=url /></td></tr>
 <tr><td colspan=2><input type=submit name=link value=Insert /></td></tr>
 </tr>
</table>
</form>
NEWFORM
;

mysql_close();
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.