Jump to content

my php script doesn't update mysql database


azimuth

Recommended Posts

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'
))
);

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']);
	}		
}
}

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.

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.