9 Replies - 1169 Views - Last Post: 27 May 2009 - 04:31 PM Rate Topic: -----

#1 nick1200   User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 02:56 PM

the title mite make this sound funny but ive bene having alook at this bit of code for a few hours

i have made a sell pokemon page and buy pokemon page


i have got the buy pokemon page pulling data from the datbase


but the sell page got to add data to the datbase i have enter data by hand and the buy page shows it so ive done a bit rite lol


i wanna make a sell page so the suer picks a slot 1 -6 and it takes what ever is in that slot

and adds it to the datbase and shows it on the buy page


so the user sells the pokemon and it end s up on the buy page

but i cant seem to get it from the user to the datbase lol

sellpokemon.php

<table border="0"
				cellpadding="0" cellspacing="0" width="107%">
				  <td width="100%"><tr>
					<td width="54" valign="top"><p align="center"><br>
					<br>
					</td>
					<td width="326" valign="top"><p>&nbsp;</p>
					
					<form name="myform" id="myform" action="" method="post">
		<p>What slot do you want to Sell?
			<select name="pokeSLOT" id="pokeSLOT" style="width:50px;">
				<option value=""></option>
				<?php for ($x=1;$x<7;$x++) { echo '<option value="'.$x.'">'.$x.'</option>'; } ?>
			</select></p>
			
					  <p>&nbsp;</p></td>
				  </tr>
					 <td><p align="center">Price You Would Like For The Pokemon  </p>
						 <p align="center">
						   <input type="int" name="cost" maxlength="30">
							   </p>
						 <p align="center">
						   <input name="submit" type="submit" value="Sell" />
						 </p>
						 <p align="left">&nbsp; </p></td>
					<p>&nbsp;</p>
					<p>&nbsp;</p>
				  
				</table>





BuyPokemon

<table border="0"
				cellpadding="0" cellspacing="0" width="107%">
				  <td width="100%"><tr>
					<td width="54" valign="top"><p align="center"><br>
					<br>
					</td>
					<td width="326" valign="top"><p>&nbsp;</p>
					
					<form name="myform" id="myform" action="" method="post">
		<p>What slot do you want to Buy For?
			<select name="pokeSLOT" id="pokeSLOT" style="width:50px;">
				<option value=""></option>
				<?php for ($x=1;$x<7;$x++) { echo '<option value="'.$x.'">'.$x.'</option>'; } ?>
			</select></p>
			
			
	
					  <p>&nbsp;</p></td>
				  </tr>
					<p>&nbsp;</p>
					<p>&nbsp;</p>
					<p>&nbsp;</p>
							<?php	
	$sql = "SELECT username, pokemon, cost, pokePIC FROM Poke_sell 
			 ORDER BY cost ASC LIMIT 190";
	$res = mysql_query($sql) or die(mysql_error());
	
	echo '<tr>';		
		$third = 1;
		
		while ($v = mysql_fetch_array($res)) {
			echo '
			<th scope="row"><p><img src="pokemon/'.$v[pokePIC].'" width="60" height="60"></p>
				<p>&pound;'.$v[cost].'</p>
				<p>'.$v[pokemon].'</p>
				<p>'.$v[username].'</p>
			<input type="button" name="Buy_Me" value="Buy Me"
				onclick="Buy_Pokemon(\''.$v[username].'\',\''.$v[pokemon].'\',\''.$v[cost].'\');"/>
			</th>';
			if (($third%3) == 0) {
				echo '</tr>';
			}
			$third++;
		}
				
	echo '</tr>';
	
?>
<script language="Javascript" type="text/javascript">
	function Buy_Pokemon(VAR, NAME, PRICE) {
	  SelectedBox = document.getElementById('pokeSLOT').value
	  UserMoney = <?php echo $pokemon->money; ?>
		
		if(PRICE > UserMoney) {
			alert("You cannot afford this pokemon, please choose another or come back at a later time.");
			return;
		}		
		if(SelectedBox == ''){
			alert("YOU MUST CHOOSE A SLOT TO BUY FOR!");
			return;
		}
		ConfirmBuy = confirm("You are about to purchase "+NAME+" for slot# "+SelectedBox+", continue?");
		
		if (ConfirmBuy) {
			document.getElementById("pokemon_buy").value = VAR;
			document.myform.submit();
		} else {
			return;
		}
	}
</script>
   
				</table>






in the datbase i have made a new table poke_sell so everything every 1 sells goes there i have added coullums

username
pokemon
Cost
time_stamp
pokePIC

im only filling in the pokemon and cost with that bit of cod ( sellpokemon)

when i need to fill in the rest i guess ?

This post has been edited by nick1200: 27 May 2009 - 02:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding Data to datbase and taking it from datbase

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 03:41 PM

Have you noticed that the more code you write, the harder it becomes to keep track of everything? That's why we have functions. They help us reduce the complexity, but even with them, things still get convoluted, which is why we have OOP. Start thinking about using an object:
<?php 
class general {
	var	$id;
	var $keyname= "id";
	var	$row;
	public	$table = array("newtable","Another_table","A3rdTable","options","parts");
	var	$dblink;
	var	$servername='localhost';
	var	$dbusername='root';
	var	$dbpassword='root';
	var	$dbname='newtest';

	function __construct() {
		$this->dblink = mysql_connect ($this->servername, $this->dbusername,$this->dbpassword) or die('CONNECTION ERROR: Could not connect to MySQL');
		mysql_select_db($this->dbname,$this->dblink) or  ('Could not open database '.mysql_error());
	}
		
	function read_table($tablenum) {
		$query = "SELECT * FROM ".$this->table[$tablenum]." where ".$this->keyname." = '".$this->id."'";
		$dbdata = mysql_query($query,$this->dblink) or die('Error reading from '.$this->table[$tablenum]." ".mysql_error());
		$this->row = mysql_fetch_array($dbdata);
	}
	
	function update_table($tablenum) {
		$query = "update ".$this->table[$tablenum]." set ";
		foreach($this->row as $key => $value) {
			if(!is_numeric($key) && $key != "id") { 
				$query .= $key." = '".$value."', ";
			}
		}
		$query = substr($query,0,-2)." where ".$this->keyname." ='".$this->id."'";
		mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
	}

	function addrecord($tablenum) {
		$query = "insert into ".$this->table[$tablenum]." (";
		$end_query = ") values (";
		foreach($this->row as $key => $value) {
			if(!is_numeric($key) && $key != "id") { 
				$query .= $key.", ";
				$end_query .= "'".$value."', ";
				}
			}
		$end_query = substr($end_query,0,-2).")";	
		$query = substr($query,0,-2).$end_query;
		mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
	}

	function display() {
		$line = "";
		foreach($this->row as $key => $value) {
			if(!is_numeric($key) && $key != "id") { 
				$line .= $value." -- ";
				}
			}
			echo $line."<br>";
		}

	function droprecord($tablenum,$dropid) {
		$query = "Delete from ".$this->table[$tablenum]." where ".$this->keyname." = '".$dropid."'";
		echo $query;
		mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
		}

}	

?>

Was This Post Helpful? 0
  • +
  • -

#3 nick1200   User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 03:46 PM

View PostCTphpnwb, on 27 May, 2009 - 02:41 PM, said:

Have you noticed that the more code you write, the harder it becomes to keep track of everything? That's why we have functions. They help us reduce the complexity, but even with them, things still get convoluted, which is why we have OOP. Start thinking about using an object:
<?php 
class general {
	var	$id;
	var $keyname= "id";
	var	$row;
	public	$table = array("newtable","Another_table","A3rdTable","options","parts");
	var	$dblink;
	var	$servername='localhost';
	var	$dbusername='root';
	var	$dbpassword='root';
	var	$dbname='newtest';

	function __construct() {
		$this->dblink = mysql_connect ($this->servername, $this->dbusername,$this->dbpassword) or die('CONNECTION ERROR: Could not connect to MySQL');
		mysql_select_db($this->dbname,$this->dblink) or  ('Could not open database '.mysql_error());
	}
		
	function read_table($tablenum) {
		$query = "SELECT * FROM ".$this->table[$tablenum]." where ".$this->keyname." = '".$this->id."'";
		$dbdata = mysql_query($query,$this->dblink) or die('Error reading from '.$this->table[$tablenum]." ".mysql_error());
		$this->row = mysql_fetch_array($dbdata);
	}
	
	function update_table($tablenum) {
		$query = "update ".$this->table[$tablenum]." set ";
		foreach($this->row as $key => $value) {
			if(!is_numeric($key) && $key != "id") { 
				$query .= $key." = '".$value."', ";
			}
		}
		$query = substr($query,0,-2)." where ".$this->keyname." ='".$this->id."'";
		mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
	}

	function addrecord($tablenum) {
		$query = "insert into ".$this->table[$tablenum]." (";
		$end_query = ") values (";
		foreach($this->row as $key => $value) {
			if(!is_numeric($key) && $key != "id") { 
				$query .= $key.", ";
				$end_query .= "'".$value."', ";
				}
			}
		$end_query = substr($end_query,0,-2).")";	
		$query = substr($query,0,-2).$end_query;
		mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
	}

	function display() {
		$line = "";
		foreach($this->row as $key => $value) {
			if(!is_numeric($key) && $key != "id") { 
				$line .= $value." -- ";
				}
			}
			echo $line."<br>";
		}

	function droprecord($tablenum,$dropid) {
		$query = "Delete from ".$this->table[$tablenum]." where ".$this->keyname." = '".$dropid."'";
		echo $query;
		mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error());
		}

}	

?>




that seems hard if u dont mind me saying lol
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 03:53 PM

It isn't. It's just scary, until you see what's going on. The class is made up of variables and functions. Debugging is easy because you can almost always easily tell which function isn't working right, so you've got a very small amount of code to deal with.

Here's a much simpler class, along with a couple of instances of it. You should be able to make sense of it easily. When you do, reread my general class above to see how it can read/write to a database.

<?php

class box {
	var $length;
	var $width;
	var $height;
	var $volume;
	
	function box_volume() {
		$this->volume = $this->length * $this->width * $this->height;	
	}
}

$bigbox = new box;
$bigbox->length = 20;
$bigbox->width = 30;
$bigbox->height = 40;
$bigbox->box_volume();
echo "Bigbox volume is ".$bigbox->volume."<br>";

$smallbox = new box;
$smallbox->length = 5;
$smallbox->width = 3;
$smallbox->height = 4;
$smallbox->box_volume();
echo "Smallbox volume is ".$smallbox->volume."<br>";

?> 

Was This Post Helpful? 0
  • +
  • -

#5 nick1200   User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 03:56 PM

View PostCTphpnwb, on 27 May, 2009 - 02:53 PM, said:

It isn't. It's just scary, until you see what's going on. The class is made up of variables and functions. Debugging is easy because you can almost always easily tell which function isn't working right, so you've got a very small amount of code to deal with.

Here's a much simpler class, along with a couple of instances of it. You should be able to make sense of it easily. When you do, reread my general class above to see how it can read/write to a database.

<?php

class box {
	var $length;
	var $width;
	var $height;
	var $volume;
	
	function box_volume() {
		$this->volume = $this->length * $this->width * $this->height;	
	}
}

$bigbox = new box;
$bigbox->length = 20;
$bigbox->width = 30;
$bigbox->height = 40;
$bigbox->box_volume();
echo "Bigbox volume is ".$bigbox->volume."<br>";

$smallbox = new box;
$smallbox->length = 5;
$smallbox->width = 3;
$smallbox->height = 4;
$smallbox->box_volume();
echo "Smallbox volume is ".$smallbox->volume."<br>";

?> 




ya i get what var does stores info so u dont have to keep typeing it lol

but the first code u made / sent looks hard id have to split the code up and put some on the sell age and some on the buy page

bit hard for a begginger lol

This post has been edited by nick1200: 27 May 2009 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 ShaneK   User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 04:04 PM

Once you get the hang of OOP you'll realize it makes it 100x easier to keep track of and modify your code.

Things like web based games and such make OOP a necessity, I highly suggest you look into actually using it.

Yours,
shane~
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 04:07 PM

No, it's all one class so you'd want to keep it together. You probably don't need all the functions it contains, but if you don't use them, they're just text, so no harm done.

The functions get a little more complicated because of the database. Note that the data is all stored in the class variable: $row, which is an array. ($this->row tells the object that it's dealing with one of its own variables and not a local function variable.)

By the way, the class by itself won't do anything. You need to call it, or generate an instance of it, which is what an object is.
Was This Post Helpful? 0
  • +
  • -

#8 nick1200   User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 04:12 PM

View PostShaneK, on 27 May, 2009 - 03:04 PM, said:

Once you get the hang of OOP you'll realize it makes it 100x easier to keep track of and modify your code.

Things like web based games and such make OOP a necessity, I highly suggest you look into actually using it.

Yours,
shane~


ya i have opp or varables in my site all ready lol to store peeps usernames and there pokemon lvl

but i hate using them lol

maybe this is to big to do lol




View PostCTphpnwb, on 27 May, 2009 - 03:07 PM, said:

No, it's all one class so you'd want to keep it together. You probably don't need all the functions it contains, but if you don't use them, they're just text, so no harm done.

The functions get a little more complicated because of the database. Note that the data is all stored in the class variable: $row, which is an array. ($this->row tells the object that it's dealing with one of its own variables and not a local function variable.)

By the way, the class by itself won't do anything. You need to call it, or generate an instance of it, which is what an object is.



well i think id need a var to get the info from the database for 1 pokemon

cus the user has 6 slots and they got to pick what slot they wanna sell

so i have added a drop don menu on that code on the sell page

so then i need to make a var to get the info for the slot they picked then id have to store that info for that slot and add it to a diffrent table poke_sell so its going from user_pokemon to poke_sell then when its added to poke sell it has to dealte from user_pokemon

then the buy page had to view everything in poke_sell and if somthing is bought it has to give the money to the user who sold it ( i have a username collum )

pretty hard :wub:
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 04:30 PM

I'm assuming that those variables would be stored in the database, in which case they'd all be part of the $row array for that object:

$this->row['slot'] would be the slot chosen by the user: $this->row['userid']

Even your drop down menu could be a function within the class that uses these values from the array!


Edit: Not hard at all, and when you need to change something later, it will be easy!

This post has been edited by CTphpnwb: 27 May 2009 - 04:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 nick1200   User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Adding Data to datbase and taking it from datbase

Posted 27 May 2009 - 04:31 PM

i better look it up lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1