Jump to content

[SOLVED] Trying to update a field


yukafluck

Recommended Posts

Complete Newbie.

 

Have a table with a field called viewed for a used car site that I have (I'm one of the salesman).

 

trying to get it so that when someone either clicks on a link to go to a page on my site or visits a page on my site, that it updates the field by one.

 

Not sure how to do it or where to start.

 

I have have a page that lists all the vehicles (vehicles_list.php) and then one that shows the details for the vehicle (vehicle_details.php).

 

any help would be appreciated or direction to a thread that already shows the answer.

Link to comment
https://forums.phpfreaks.com/topic/150393-solved-trying-to-update-a-field/
Share on other sites

example only.

 

get the drift.

 

<?php

echo"<a href='".$_SERVER['PHP_SELF']."'?cmd=".md5('cars').">Cars Link</a>";
echo"<br>";
echo"<a href='".$_SERVER['PHP_SELF']."'?cmd=".md5('vans').">Vans Link</a>";
echo"<br>";
echo"<a href='".$_SERVER['PHP_SELF']."'?cmd=".md5('truck').">Truck Link</a>";


if($_GET['cmd']==md5('cars')){

$sql="UPDATE link_stats SET car_stats=car_stats+1 WHERE id='1'";
    $res=mysql_query($sql)or die(mysql_error());

    if($res){
    	
    	header("location: the_page_there_going_from_cars_link");
    }
    
}

if($_GET['cmd']==md5('vans')){

$sql="UPDATE link_stats SET vans_stats=van_stats+1 WHERE id='0'";
    $res=mysql_query($sql)or die(mysql_error());
    
       if($res){
    	
    	header("location: the_page_there_going_from_vans_link");
    }
}



if($_GET['cmd']==md5('truck')){


$sql="UPDATE link_stats SET truck_stats=truck_stats+1 WHERE id='0'";
    $res=mysql_query($sql)or die(mysql_error());
    
    
   if($res){
    	
    	header("location: the_page_there_going_from_truck_link");
    }
}

?>

database.php

<?php

$connect_database=mysql_connect("localhost","username","password")
or die("Databse connection problam".mysql_error());

$sql="CREATE DATABASE website_links";
$res=mysql_query($sql)or die(mysql_error());

$sql2="USE website_links";
$res2=mysql_query($sql2)or die(mysql_error());

$sql3="CREATE TABLE link_stats(
stat_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
car_stats INT DEFAULT '0' NOT NULL,
van_stats INT DEFAULT '0' NOT NULL,
truck_stats INT DEFAULT '0' NOT NULL
)";

$res3=mysql_query($sql3)or die(mysql_error());

?>

Here a full working example.

 

every time you press a link,

you get redirected to Google.com,

 

the database will update according to the link pressed.

 

hope you understand all the best redarrow.

 

fully tested.

 

ps. you set the header to the location off the page the link is suppose to goto.

 

 

database.php

<?php

$connect_database=mysql_connect("localhost","username","password")
or die("Databse connection problam".mysql_error());

$sql="CREATE DATABASE website_links";
$res=mysql_query($sql)or die(mysql_error());

$sql2="USE website_links";
$res2=mysql_query($sql2)or die(mysql_error());

$sql3="CREATE TABLE link_stats(
stat_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
car_stats INT DEFAULT '0' NOT NULL,
van_stats INT DEFAULT '0' NOT NULL,
truck_stats INT DEFAULT '0' NOT NULL
)";

$res3=mysql_query($sql3)or die(mysql_error());


$sql4="INSERT INTO link_stats(car_stats,van_stats,truck_stats) VALUES('1','1','1')";

$res5=mysql_query($sql4)or die(mysql_error());


if($res5){

echo" DATABASE CREATED THANK YOU!";
}

?>

 

 

 

website_stats.php

 

<?php


$connect_database=mysql_connect("localhost","username","password")
or die("Databse connection problam".mysql_error());

$res=mysql_select_db("website_links",$connect_database);

echo"<a href='".$_SERVER['PHP_SELF']."?cmd=".md5('cars')."'>Cars Link</a>";
echo"<br>";
echo"<a href='".$_SERVER['PHP_SELF']."?cmd=".md5('vans')."'>Vans Link</a>";
echo"<br>";
echo"<a href='".$_SERVER['PHP_SELF']."?cmd=".md5('truck')."'>Truck Link</a>";


if($_GET['cmd']==md5('cars')){

$sql="UPDATE link_stats SET car_stats=car_stats+1 WHERE stat_id='1'";
    $res=mysql_query($sql)or die(mysql_error());

    if($res){
    	
    	header("location: http://www.google.com");
    }
    
}

if($_GET['cmd']==md5('vans')){

$sql="UPDATE link_stats SET van_stats=van_stats+1 WHERE stat_id='1'";
    $res=mysql_query($sql)or die(mysql_error());
    
       if($res){
    	
    		header("location: http://www.google.com");
    }
}



if($_GET['cmd']==md5('truck')){


$sql="UPDATE link_stats SET truck_stats=truck_stats+1 WHERE stat_id='1'";
    $res=mysql_query($sql)or die(mysql_error());
    
    
   if($res){
    	
    		header("location: http://www.google.com");
    }
}

?>

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.