azimuth Posted May 8, 2009 Share Posted May 8, 2009 My php script is supposed to update the sql db when I click a specific link but when I tried to click the link, it doesn't update the database in sql. Here's the script and the format of the sql table: <?php /************* * SQL Table * CREATE TABLE `vote_point` ( `key` int(11) NOT NULL auto_increment, `loginname` varchar(55) NOT NULL, `point` int(11) NOT NULL default '0', `last_vote1` int(11) NOT NULL default '0', `last_vote2` int(11) NOT NULL default '0', `last_vote3` int(11) NOT NULL default '0', `date` text NOT NULL, PRIMARY KEY (`key`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; * * *************/ session_start(); require( "memory.php" ); $sql = new MySQL; $sql->Connect( $CONFIG_sql_host, $CONFIG_sql_username, $CONFIG_sql_password ); $site = $_GET['site']; $link = unserialize( VOTE_LINK ); if( !isset( $site ) || !isset( $link[ $site ] ) ) header( 'Location: index.php' ); else if( !isset( $STORED_loginname ) ) votes(); else { $result = $sql->execute_query( "SELECT `last_vote".$site."` FROM `vote_point` WHERE `loginname` = '".$STORED_loginname."' LIMIT 0,1", "vote.php" ); if( $sql->count_rows() > 0 ) { $row = $sql->fetch_row(); if( ( time() - $row[ 0 ] ) > (60 * 60 * VOTE_TIME) ) $sql->execute_query("UPDATE `vote_point` SET `point` = `point` + 1 , `last_vote".$site."` = '".time()."', `date` = '".date("d.m.y H:i")."' WHERE `loginname` = '".$STORED_loginname."'", "vote.php"); votes(); } else { $sql->execute_query("INSERT INTO `vote_point` ( `loginname` , `point` , `last_vote".$site."` , `date` ) VALUES ( '".$STORED_loginname."' , 1 , '".time()."' , '".date("d.m.y H:i")."')", "vote.php"); votes(); } } function votes() { global $site, $link; if( isset( $link[ $site ] ) ) header( 'Location: '.$link[ $site ] ); else header( 'Location: index.php' ); die(); } ?> I also added this in my configuration file: define('VOTE_TIME', 24 ); define('VOTE_LINK', serialize( array( 1 => 'http://www.test1.com', 2 => 'http://www.test2.com', 3 => 'http://www.test3.com' )) ); Link to comment https://forums.phpfreaks.com/topic/157434-my-php-script-doesnt-update-mysql-database/ Share on other sites More sharing options...
trq Posted May 8, 2009 Share Posted May 8, 2009 Where is $STORED_loginname defined? Link to comment https://forums.phpfreaks.com/topic/157434-my-php-script-doesnt-update-mysql-database/#findComment-829954 Share on other sites More sharing options...
azimuth Posted May 9, 2009 Author Share Posted May 9, 2009 Here's a piece of the function file function getglobalvar($i) { global $CONFIG_save_type; if($i==1) { global $STORED_loginname,$STORED_loginpass; if($CONFIG_save_type==1) { global $_SESSION; $STORED_loginname = $_SESSION['loginname']; $STORED_loginpass = $_SESSION['loginpass']; } else { global $_COOKIE; $STORED_loginname = $_COOKIE['loginname']; $STORED_loginpass = $_COOKIE['loginpass']; } } else if($i==2) { global $STORED_userlang,$STORED_usertheme; if($CONFIG_save_type==1) { global $_SESSION; $STORED_userlang = truestr($_SESSION['userlang']); $STORED_usertheme = truestr($_SESSION['usertheme']); } else { global $_COOKIE; $STORED_userlang = truestr($_COOKIE['userlang']); $STORED_usertheme = truestr($_COOKIE['usertheme']); } } } Link to comment https://forums.phpfreaks.com/topic/157434-my-php-script-doesnt-update-mysql-database/#findComment-829962 Share on other sites More sharing options...
trq Posted May 9, 2009 Share Posted May 9, 2009 Eww, globals in functions. Anyway, your going to have to do some error checking of your own. I'm not sure what db abstraction layer your using, but generally you would check if a query succeeded or not by doing something like.... $sql = "INSERT INTO foo (bar) VALUES ('bob');"; if ($result = mysql_query($sql)) { if (mysql_affected_rows($result)) { echo "query success"; } else { echo "no rows affected"; } else { echo "query failed " . mysql_error() . $sql; } You'll need to do something similar to narrow your problem down. Link to comment https://forums.phpfreaks.com/topic/157434-my-php-script-doesnt-update-mysql-database/#findComment-829967 Share on other sites More sharing options...
neogemima Posted May 9, 2009 Share Posted May 9, 2009 Yes, and when that happens and you run into something else, echo your variable to make sure exactly what you think is being passed to the script. Link to comment https://forums.phpfreaks.com/topic/157434-my-php-script-doesnt-update-mysql-database/#findComment-830100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.