• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] Update values in database

Kavvson

Gdy boli cie glowa wez
Joined
Jun 25, 2008
Messages
1,177
Reaction score
72
Location
Poland
Archez wrote me a script. I cant figure out how to make 2 tables = 2 arrays . Can someone help?

PHP:
<?php
	
	## Add credits to me, plx.
	## Archez (http://otland.net/members/archez)
	
	$mysqli = new mysqli('','','','');
	
	## Random field
	## It's needed. :X
	$field = '`premend` = 0';
	
	## The second value (first value => second value)
	## must be equal to the database field.
	
	$array = array(
	'Name' => 'name',
	'Account ID' => 'account_id',
	'World ID' => 'world_id',
	'Group ID' => 'group_id',
	'Level' => 'level');
	
	if(!isset($_REQUEST['do']))
	{
		header('Location: ?do=index');
	}
	
	if($_REQUEST['do'] == 'index')
	{
		echo '<form action="?do=set" method="post">';
		echo '<table border="1" cellpadding="4" cellspacing="1" width="50%">';
		echo '<tr><td>Character name/ID:</td><td><input type="text" name="name" /></td>';
		echo '<td><input type="submit" value="Search" /></td></tr>';
		echo '</table>';
		echo '</form>';
	}
	
	if($_REQUEST['do'] == 'set')
	{
		
		if(!isset($_REQUEST['name']))
		{
			header('Location: ?do=index');
		}
		
		if(!is_numeric($_REQUEST['name']))
		{
			$check = $mysqli->query('SELECT * FROM `players` WHERE `name` = "'.strip_tags($_POST['name']).'"')->fetch_assoc();
		}
		else
		{
			$check = $mysqli->query('SELECT * FROM `players` WHERE `id` = "'.strip_tags($_POST['name']).'"')->fetch_assoc();
		}
		
		if(!$check)
		{
			if(is_numeric($_REQUEST['name']))
			{
				die('The system couldn\'t find the player with ID: <b>'.strip_tags($_REQUEST['name']).'</b>');
			}
			else
			{
				die('The system couldn\'t find the player: <b>'.strip_tags($_REQUEST['name']).'</b>');
			}
		}
		
		echo '<form action="?do=update" method="post">';
		echo '<table border="1" cellpadding="4" cellspacing="1" width="50%">';
		
		foreach($array as $a => $b)
		{
			echo '<tr><td width="20%">'.$a.':</td><td><input type="text" name="'.$b.'" value="'.$check[$b].'" /></td>';
		}
		
		echo '<tr><td><input type="submit" value="Update" /></td><td><input type="reset" value="Clear" /></td></tr>';
		
		echo '</table>';
		echo '</form>';
	}
	
	if($_REQUEST['do'] == 'update')
	{
		if(!$_POST)
		{
			header('Location: ?do=index');
		}
		else
		{
			$query .= 'UPDATE `players` SET ';
			
			foreach($array as $c => $d)
			{
				$query .= '`'.strip_tags($d).'` = "'.strip_tags($_POST[$d]).'", ';
			}
			
			$query .= $field.' WHERE `name` = "'.strip_tags($_POST['name']).'"';
			
			$execute = $mysqli->query($query);
			
			if($execute)
			{
				echo 'Done!';
			}
			else
			{
				echo 'There was an error during the updating process!';
			}
		}
	}
	
?>
 
Back
Top