yukafluck Posted March 20, 2009 Share Posted March 20, 2009 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 More sharing options...
Yesideez Posted March 20, 2009 Share Posted March 20, 2009 The easiest would probably to increment at the start of the page being called. The best way (to count actual clicks) is to pass the links through a script and increase a counter going by the URL passed. Link to comment https://forums.phpfreaks.com/topic/150393-solved-trying-to-update-a-field/#findComment-789840 Share on other sites More sharing options...
yukafluck Posted March 20, 2009 Author Share Posted March 20, 2009 what type of code should be used, through a form or through a function? I usually do my coding through trial and error, and I'm all out of ideas. Link to comment https://forums.phpfreaks.com/topic/150393-solved-trying-to-update-a-field/#findComment-789894 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 it easy 1 min. Link to comment https://forums.phpfreaks.com/topic/150393-solved-trying-to-update-a-field/#findComment-789907 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 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"); } } ?> Link to comment https://forums.phpfreaks.com/topic/150393-solved-trying-to-update-a-field/#findComment-789915 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 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()); ?> Link to comment https://forums.phpfreaks.com/topic/150393-solved-trying-to-update-a-field/#findComment-789926 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 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"); } } ?> Link to comment https://forums.phpfreaks.com/topic/150393-solved-trying-to-update-a-field/#findComment-789949 Share on other sites More sharing options...
yukafluck Posted March 28, 2009 Author Share Posted March 28, 2009 Thanks, that worked aweseome Link to comment https://forums.phpfreaks.com/topic/150393-solved-trying-to-update-a-field/#findComment-795812 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.