Jump to content

A Simple PHP Code that I cannot do?! >_<


Divide

Recommended Posts

Alright. I'll explain this to the best of my ability. I've fixed up an old mod from 2007, that allows members to automatically 'become' subscribed to the Subscriber usergroup.

Everything works fine there..

 

But I run a game, with a different table inside the Mysql, and I've been trying to make it so that when they Subscribe, their forum AND their game accounts are both upgraded to the ID "7" usergroup get what I'm saying?

 

Anyways.. Here's the table for "players"

-- 
-- Table structure for table `players`
-- 

CREATE TABLE `players` (
  `user` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL default '',
  `group_id` int(10) default '4',
  `owner` int(5) unsigned NOT NULL,
  `owner_username` varchar(255) default NULL,
  `sub_expires` int(5) unsigned default '0',
  `combat` int(10) default '3',
  `skill_total` int(10) default '3',
  `x` int(5) unsigned default '213',
  `y` int(5) unsigned default '452',
  `fatigue` int(10) default '0',
  `kills` int(10) default '0',
  `deaths` int(10) default '0',
  `combatstyle` tinyint(1) default '0',
  `block_chat` tinyint(1) unsigned default '0',
  `block_private` tinyint(1) unsigned default '0',
  `block_trade` tinyint(1) unsigned default '0',
  `block_duel` tinyint(1) unsigned default '0',
  `cameraauto` tinyint(1) unsigned default '0',
  `onemouse` tinyint(1) unsigned default '0',
  `soundoff` tinyint(1) unsigned default '0',
  `showroof` tinyint(1) default '0',
  `autoscreenshot` tinyint(1) default '0',
  `combatwindow` tinyint(1) default '0',
  `haircolour` int(5) unsigned default '2',
  `topcolour` int(5) unsigned default '8',
  `trousercolour` int(5) unsigned default '14',
  `skincolour` int(5) unsigned default '0',
  `headsprite` int(5) unsigned default '1',
  `bodysprite` int(5) unsigned default '2',
  `male` tinyint(1) unsigned default '1',
  `skulled` int(10) unsigned default '0',
  `pass` varchar(255) NOT NULL,
  `creation_date` int(10) unsigned NOT NULL default '0',
  `creation_ip` varchar(15) NOT NULL default '0.0.0.0',
  `login_date` int(10) unsigned default '0',
  `login_ip` varchar(15) default '0.0.0.0',
  `playermod` tinyint(1) unsigned default '0',
  `loggedin` tinyint(1) default '0',
  `banned` tinyint(1) default '0',
  `muted` tinyint(1) default '0',
  `id` int(10) unsigned NOT NULL auto_increment,
  `online` tinyint(1) unsigned zerofill default '0',
  `world` int(10) default '1',
  `qp` int(10) NOT NULL,
  `showonhighscores` int(1) NOT NULL default '1',
  PRIMARY KEY  (`id`),
  KEY `showonhighscores` (`showonhighscores`),
  KEY `user` (`user`,`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1182 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC COMMENT='InnoDB free: 9216 kB' AUTO_INCREMENT=1182 ;

 

AND..

 

Here's the code for the subscription

 

<?php

define('PUN_ROOT', './');
require PUN_ROOT.'include/common.php';

if($pun_config['s_enabled'] != '1') {
message('Subscriptions are currently disabled.');
}

$user_id = isset($_GET['user']) ? intval($_GET['user']) : $pun_user['id'];

$result = $db->query('SELECT * FROM '.$db->prefix.'users WHERE id='.$user_id) or error('Unable to get user info', __FILE__, __LINE__, $db->error());
$sub_user = $db->fetch_assoc($result);

if($sub_user['group_id'] != PUN_MEMBER && $sub_user['group_id'] != $pun_config['s_new_gid']) {
message('Only members are allowed to subscribe.');
}

if(isset($_GET['tx'])) {
$tx_token = trim($_GET['tx']);
$req = 'cmd=_notify-synch&tx='.$tx_token.'&at='.$pun_config['s_auth_token'];

$ctx = stream_context_create(array('http' => array(
	'method' => 'POST',
	'header' => 'Content-Type: application/x-www-form-urlencoded'."\r\n".'Content-Length: '.strlen($req)."\r\n",
	'content' => $req
)));
$lines = file('http://www.paypal.com/cgi-bin/webscr', FILE_IGNORE_NEW_LINES, $ctx);
if(array_shift($lines) != 'SUCCESS') {
	message('Unknown tx_id. Please contact an admin.');
}
$ppresult = array();
foreach($lines as $line) {
	list($key, $value) = explode('=', $line);
	$ppresult[urldecode($key)] = urldecode($value);
}

$result = $db->query('SELECT * FROM '.$db->prefix.'paypal_subscriptions WHERE txn_id=\''.$db->escape($ppresult['txn_id']).'\'') or error('Unable to fetch paypal subscription info', __FILE__, __LINE__, $db->error());
if($db->num_rows($result)) {
	message('This txn_id has already been processed. Please contact an admin.');
}

$db->query('INSERT INTO '.$db->prefix.'paypal_subscriptions(txn_id, receiver_email, payer_email, payment_status, payment_amount, payment_currency, time, user_id, payer_ip) VALUES(\''.$db->escape($ppresult['txn_id']).'\', \''.$db->escape($ppresult['receiver_email']).'\', \''.$db->escape($ppresult['payer_email']).'\', \''.$db->escape($ppresult['payment_status']).'\', \''.$db->escape($ppresult['payment_gross']).'\', \''.$db->escape($ppresult['mc_currency']).'\', '.time().', '.intval($sub_user['id']).', \''.get_remote_address().'\')') or error('Unable to insert paypal subscription info', __FILE__, __LINE__, $db->error());

if($ppresult['receiver_email'] != $pun_config['s_paypal_email']) {
	message('The email the payment was sent to did not match. Please contact an admin.');
}
if($ppresult['payment_status'] != 'Completed') {
	message('The payment was not fully completed. If you payed using e-Cheque rather than instant payment it can take up to 9 days to clear. Please wait for the payment to clear, then if you are not upgraded contact an admin.');
}
if($ppresult['payment_gross'] < $pun_config['s_price']) {
	message('The wrong amount was payed. Please contact an admin.');
}
if($ppresult['mc_currency'] != $pun_config['s_currency']) {
	message('An invalid currency was used. Please contact an admin.');
}

$length = $pun_config['s_length'] * 86400;

if($sub_user['group_id'] == $pun_config['s_new_gid']) {
	$db->query('UPDATE '.$db->prefix.'users SET sub_expires=sub_expires+'.$length.' WHERE id='.intval($sub_user['id'])) or error('Unable to update subscription expire time', __FILE__, __LINE__, $db->error());
}
else {
	$db->query('UPDATE '.$db->prefix.'users SET group_id='.intval($pun_config['s_new_gid']).', invites=2, sub_expires='.($pun_config['s_length'] > 0 ? (time() + $length) : '0').' WHERE id='.intval($sub_user['id'])) or error('Unable to update group id', __FILE__, __LINE__, $db->error());
}

redirect('index.php', 'Payment successful. Thank you');

}
else {
$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / Subscription';
require PUN_ROOT.'header.php';

?>
<div class="block">
<h2><span>Subscribe to <?php echo pun_htmlspecialchars($pun_config['o_board_title']); ?></span></h2>
<div class="box">
	<div class="inbox" style="text-align:center">
		<div><b>This subscription is for <a href="profile.php?id=<?php echo $sub_user['id']; ?>"><?php echo pun_htmlspecialchars($sub_user['username']); ?></a>.</b></div>
		<div><?php echo $sub_user['group_id'] == $pun_config['s_new_gid'] ? '<p>This will allow you to extend your subscription for another '.$pun_config['s_length'].' days.' : $pun_config['s_description']; ?></div>
		<!-- START PAYPAL PAYMENT FORM -->
		<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
			<div class="inform" style="margin-top:10px">
				<input type="hidden" name="cmd" value="_xclick">
				<input type="hidden" name="business" value="<?php echo pun_htmlspecialchars($pun_config['s_paypal_email']); ?>">
				<input type="hidden" name="item_name" value="<?php echo pun_htmlspecialchars($pun_config['o_board_title']); ?> Subscription">
				<input type="hidden" name="amount" value="<?php echo pun_htmlspecialchars($pun_config['s_price']); ?>">
				<input type="hidden" name="no_shipping" value="1">
				<input type="hidden" name="return" value="<?php echo pun_htmlspecialchars('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?user='.$sub_user['id']); ?>">
				<input type="hidden" name="no_note" value="1">
				<input type="hidden" name="currency_code" value="<?php echo pun_htmlspecialchars($pun_config['s_currency']); ?>">
				<input type="hidden" name="bn" value="PP-BuyNowBF">
				<input type="image" src="img/paypal.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
				<p><b>Note</b>: After payment please let paypal redirect you back here, otherwise your status will not automatically get updated.</p>
			</div>
		</form>
		<!-- END PAYPAL PAYMENT FORM -->
<?php

if($pun_config['s_length'] > 0 && $sub_user['group_id'] != $pun_config['s_new_gid']) {
	echo "\t\t\t".'<p>This subscription will last for '.$pun_config['s_length'].' days, then you will be returned to the member usergroup.</p>'."\n";
}

?>
	</div>
</div>
</div>
<?php

require PUN_ROOT.'footer.php';
}

 

 

 

 

 

As you can tell, in the code.. it already sets the "users" to the proper usergroup just fine, but I want to make it so that IT ALSO sets their IN GAME characters to group ID 7 (which is the group id for subscriber) and put on the sub_expire, just like the users table.

 

Users table is posted below, if you do need it.

 

NOTE - The 'owner' field inside the "players" database is the same thing as the 'id' field in the "users" database.

 

-- 
-- Table structure for table `users`
-- 

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `group_id` int(10) unsigned NOT NULL default '4',
  `username` varchar(200) NOT NULL default '',
  `password` varchar(40) NOT NULL default '',
  `email` varchar(50) NOT NULL default '',
  `title` varchar(50) default NULL,
  `realname` varchar(40) default NULL,
  `url` varchar(100) default NULL,
  `jabber` varchar(75) default NULL,
  `icq` varchar(12) default NULL,
  `msn` varchar(50) default NULL,
  `aim` varchar(30) default NULL,
  `yahoo` varchar(30) default NULL,
  `location` varchar(30) default NULL,
  `use_avatar` tinyint(1) NOT NULL default '0',
  `signature` text,
  `disp_topics` tinyint(3) unsigned default NULL,
  `disp_posts` tinyint(3) unsigned default NULL,
  `email_setting` tinyint(1) NOT NULL default '1',
  `save_pass` tinyint(1) NOT NULL default '1',
  `notify_with_post` tinyint(1) NOT NULL default '0',
  `show_smilies` tinyint(1) NOT NULL default '1',
  `show_img` tinyint(1) NOT NULL default '1',
  `show_img_sig` tinyint(1) NOT NULL default '1',
  `show_avatars` tinyint(1) NOT NULL default '1',
  `show_sig` tinyint(1) NOT NULL default '1',
  `timezone` float NOT NULL default '0',
  `language` varchar(25) NOT NULL default 'English',
  `style` varchar(25) NOT NULL default 'Oxygen',
  `num_posts` int(10) unsigned NOT NULL default '0',
  `last_post` int(10) unsigned default NULL,
  `registered` int(10) unsigned NOT NULL default '0',
  `registration_ip` varchar(15) NOT NULL default '0.0.0.0',
  `last_visit` int(10) unsigned NOT NULL default '0',
  `admin_note` varchar(30) default NULL,
  `activate_string` varchar(50) default NULL,
  `activate_key` varchar( default NULL,
  `sub_expires` int(10) unsigned NOT NULL default '0',
  `invites` varchar(10) NOT NULL default '1',
  `country_code` int(50) unsigned NOT NULL default '1',
  `invited_by` varchar(255) NOT NULL,
  `country` varchar(40) default NULL,
  `birthday` varchar(10) NOT NULL default '0-0-0',
  `rep_minus` int(11) unsigned default '0',
  `rep_plus` int(11) unsigned default '0',
  `reputation_enable` smallint(6) default '1',
  `reputation_enable_adm` tinyint(1) unsigned default '1',
  PRIMARY KEY  (`id`),
  KEY `users_registered_idx` (`registered`),
  KEY `users_username_idx` (`username`()
) ENGINE=MyISAM AUTO_INCREMENT=7980 DEFAULT CHARSET=utf8 AUTO_INCREMENT=7980 ;

Link to comment
https://forums.phpfreaks.com/topic/179924-a-simple-php-code-that-i-cannot-do-_/
Share on other sites

Post the code which you added to update the second table, and the mysql_error() (or in your case, $db->error()) that is generated.

 

Eh, I'm kinda retarded when it comes to trying to do the querys for PHP..

 

But i'll post it anyways... ** the thing i added in was the players db.

 

if($sub_user['group_id'] == $pun_config['s_new_gid']) {
	$db->query('UPDATE '.$db->prefix.'users SET sub_expires=sub_expires+'.$length.' WHERE id='.intval($sub_user['id'])) or error('Unable to update subscription expire time', __FILE__, __LINE__, $db->error());
	$db->query('UPDATE '.$db->prefix.'players SET sub_expires=sub_expires+'.$length.' WHERE id='.intval($sub_user['id'])) or error('Unable to update subscription expire time', __FILE__, __LINE__, $db->error());
}
else {
	$db->query('UPDATE '.$db->prefix.'users SET group_id='.intval($pun_config['s_new_gid']).', invites=2, sub_expires='.($pun_config['s_length'] > 0 ? (time() + $length) : '0').' WHERE id='.intval($sub_user['id'])) or error('Unable to update group id', __FILE__, __LINE__, $db->error());
	$db->query('UPDATE '.$db->prefix.'players SET group_id='.intval($pun_config['s_new_gid']).', invites=2, sub_expires='.($pun_config['s_length'] > 0 ? (time() + $length) : '0').' WHERE id='.intval($sub_user['id'])) or error('Unable to update group id', __FILE__, __LINE__, $db->error());
}

redirect('index.php', 'Payment successful. Thank you');

 

 

I know I'm doing something wrong. >_< (the full file code is posted above in first post)

You see, that line of error your script outputs is the same for both tables. If either query generates an error, the same error message will appear.

 

The second query you added probably generated the error, therefore calling the "or error('Unable to update group id', __FILE....." part.

 

Change the lines to:

...or error ('Unable to update group id in users'....
...or error ('Unable to update group id in players'....

 

At least this error will be marginally more useful, since you will be able to conclude which query is generating the error. I'm guessing the error now will be "Unable to update group id in players".

 

However, I was not asking for this line of error output in my previous post. What I'm asking for is the error generated by MySQL, probably returned by the $db->error() method in your script.

 

Try adding:

... or error($db->error().": Unable to update group id...

to both queries.

 

Then, copy the error generated and post it here if you need further help.

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.