Jump to content

using data recived from a while loop.


Renlok

Recommended Posts

im making a game and i have usershops where the user can chnge the price of the things in there inventory, but i cant make it work.

 

the input form is:

$yourshopheader =<<<EOD
<form name="stock" method="post" action="action/userstock.php?id=$shopid">
<p><table border="1" width="100%" id="table1">
	<tr>
		<td>Item</td>
		<td>Qty</td>
		<td>Price</td>
		<td>Remove</td>
	</tr>
EOD;
echo $yourshopheader;

$query = "SELECT * FROM usershopitems WHERE shopid='$shopid' ORDER by id DESC";
$shopitems = mysql_query($query);
while($itemarray = mysql_fetch_array($shopitems))
{
$shopitemid = $itemarray['itemid'];
$shopprice = $itemarray['price'];
$shopqty = $itemarray['qty'];
$itemtypequery = mysql_query("SELECT * FROM itemnames WHERE id='$shopitemid'");
$itemtypedata = mysql_fetch_array($itemtypequery);
$itemimage = $itemtypedata['image'];
$itemname = $itemtypedata['name'];
$items =<<<EOD
	<tr>
		<td><img src="http://www.roeonline.co.uk/images/$itemimage"><br>$itemname</td>
		<td>$shopqty</td>
		<td><input type="text" name="price[]" size="7" value="$shopprice"></td>
		<td>Remove</td>
	</tr>
EOD;
echo  $items;

}
echo   "</table>
	<input type=\"submit\" name=\"submit\" value=\"Update Stock\">
	</form>";

 

and the code which doesnt work is:

<?php
ob_start();
session_start();
$shopid = $_GET['id'];
$item_price = $_POST['price'];
//database and user stuff
if (!isset($_COOKIE['mysite_username'])) die("You are not logged in<br>Please login first before viewing this page");

include("/home/renlok/public_html/includes/config.php");
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

	$username = $HTTP_COOKIE_VARS['mysite_username'];
	$query = "SELECT * FROM users WHERE md5='$username'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);

$find_items2 = mysql_query("SELECT * FROM usershopitems WHERE shopid='$shopid' ORDER by id DESC");
while ($find_items = mysql_fetch_array($find_items2))
{
$id = $find_items['id'];
$price = $find_items['price'];

if ($price[$shopitemid] > "200000")
{
	die("Please do not price items more than 200000 points.");
}
if ($price[$shopitemid] < "0")
{
	die("Please do not price items below 0 points.");
}
if ($price[$shopitemid] != $price)
{
	mysql_query("UPDATE usershopitems SET price='$item_price' WHERE id='$id' AND shopid='$shopid'");
}
}

$_SESSION['message'] = "Stock has been updated";
header("Location: yourshop.php");

?>

 

the only thing that happen when you try and run this is when the header finction brings you back to the correct page, it thins your logged off untill you refresh the page. and it usally sets the item price to 0

Link to comment
https://forums.phpfreaks.com/topic/36554-using-data-recived-from-a-while-loop/
Share on other sites

You have...

while ($find_items = mysql_fetch_array($find_items2))
{
$id = $find_items['id'];
$price = $find_items['price'];    //<--- this is an integer

if ($price[$shopitemid] > "200000")   //<----  this gets treated like an array.
{
	die("Please do not price items more than 200000 points.");
}

 

 

Did you mean?

{
$id = $find_items['id'];
$price[$id] = $find_items['price'];    //<--- array assignment

if ($price[$shopitemid] > "200000")   
{
	die("Please do not price items more than 200000 points.");
}

 

Or even...

 

{
$id = $find_items['id'];
$price = $find_items['price'];    

if ($price > "200000")   // <-- straight up int ?
{
	die("Please do not price items more than 200000 points.");
}

also change

$row = mysql_fetch_array($result);

to

$row = mysql_fetch_assoc($result);

 

WHY!?!?!

 

mysql_fetch_array() is preferable to mysql_fetch_assoc() as it's more flexible.  Just use it with the additional constant of MYSQL_ASSOC...

 

$row = mysql_fetch_array($result, MYSQL_ASSOC);

 

Regards

Huggie

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.