Jump to content

Loads of results, stuck in a endless loop


Jragon

Recommended Posts

Hi!

 

When I execute my code, I get:

1iPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at HTCiPhone 4 at 

 

Over and over again

 

What I need it to be like:

Product: iPhone 4 
Catagory: HTC
400$ at store 1
412$ aat store 2
ect.
[button]DELTE RECORD

 

Here is my php code:

<?php
require_once('connect.php');
include('functions.php');
if(isset($_POST['product']) && isset($_POST['cat']) && isset($_POST['store']) && isset($_POST['price'])){
$product = escape('product');
$cat = escape('cat');
$store = escape('store');
$price = escape('price');
$pid = escape('pid');

if (isset($_POST['delete']) && $product != "" && $pid != ""){
	$query = "DELETE FROM `products` where product='$product'
			  DELETE FROM `prices` when pid='$pid'";
	mysql_query($query) or die(mysql_error());
}else{
	$query = "INSERT INTO `product` VALUES(null, '$cat', '$prouct')";
	mysql_query($query);
	$query = "SELECT id FROM product WHERE catagory='$cat' product='$product'";
	while(mysql_fetch_row(mysql_query($query))){
		$pid = $row[0];
	}
	$query = "INSERT INTO `price` VALUES('$pid', '$store', '$price')";			
}
mysql_query($query);
}
?>
<form action="" method="POST">
Product name: <input type="text" name="product" />
Catagory: <select name="cat">
<?php 
$query = "SELECT id, catagory FROM `catagory`";
$result = mysql_query($query);

while($row = mysql_fetch_row($result)){
echo "<option value=\"$row[0]\">$row[1]</option>";
}
?>
</select><br />
At stores:
<?php
$query = "SELECT id,short FROM `stores`";
$result = mysql_query($query) or die(mysql_error());
while(mysql_fetch_row($result)){
echo "<input type=\"checkbox\" name=\"store\" value=\"$row[0]\" />$row[1]<br />";
}
?>
Price:<input type="text" name="price" />

</form>
<?php

$query = 'SELECT products.product, catagorys.catagory, price.price, stores.short, price.pid, products.id
FROM  `products` 
JOIN  `catagorys` ON products.cid = catagorys.id
JOIN  `price` ON price.pid = products.id
JOIN  `stores` ON price.sid = stores.id;';
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result) or die(mysql_error());
$ppid = "";
while($row = mysql_fetch_row($result)){
if($ppid != $row[4]){
	$ppid = $row[4];
	echo <<<_ENDHTML
Product: $row[0] <br />
Catagory: $row[1]<br />
_ENDHTML;
	$query = "SELECT price.price, stores.short, price.pid, products.id
	FROM  `products` 
	JOIN  `price` ON price.pid = products.id
	JOIN  `stores` ON price.sid = stores.id
	WHERE products.id =  '$row[5]'";
	echo $row[5];
	$result = mysql_query($query) or die(mysql_error());
	while(mysql_num_rows($result)){
		echo "$row[0] at $row[1]";
	}	
	echo <<<_ENDHTML

<form action="" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="product" value="$row[0]" />
<input type="hidden" name="price" value="$row[2]" />
<input type="hidden" name="store" value="$row[3]" />
<input type="hidden" name="cat" value="$row[1]" />
<input type="hidden" name="pid" value="$row[4]" />
<input type="submit" value="DELETE RECORD" /></form>
</form>
_ENDHTML;
}elseif($ppid == $row[4]){
	echo "<br />$row[2] at $row[3]";
}
}

?>

 

Here is my database dump:

-- phpMyAdmin SQL Dump
-- version 3.3.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 17, 2011 at 11:12 PM
-- Server version: 5.5.8
-- PHP Version: 5.3.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `pos`
--

-- --------------------------------------------------------

--
-- Table structure for table `catagorys`
--

CREATE TABLE IF NOT EXISTS `catagorys` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `catagory` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `catagorys`
--

INSERT INTO `catagorys` (`id`, `catagory`) VALUES
(1, 'HTC'),
(2, 'iPhone');

-- --------------------------------------------------------

--
-- Table structure for table `employees`
--

CREATE TABLE IF NOT EXISTS `employees` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `sid` int(11) NOT NULL,
  `fname` varchar(255) NOT NULL,
  `lname` varchar(255) NOT NULL,
  `pass` int(11) NOT NULL,
  `hours` int(11) NOT NULL,
  `clocked` tinyint(1) NOT NULL,
  `wage` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `employees`
--

INSERT INTO `employees` (`id`, `sid`, `fname`, `lname`, `pass`, `hours`, `clocked`, `wage`) VALUES
(1, 1, 'Rory', 'Anderson', 123456, 1, 0, 50),
(2, 1, 'Phil', 'Brown', 1337, 0, 0, 15);

-- --------------------------------------------------------

--
-- Table structure for table `price`
--

CREATE TABLE IF NOT EXISTS `price` (
  `pid` int(11) NOT NULL,
  `sid` int(11) NOT NULL,
  `price` int(11) NOT NULL,
  PRIMARY KEY (`pid`,`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `price`
--

INSERT INTO `price` (`pid`, `sid`, `price`) VALUES
(1, 31, 112),
(1, 32, 60000);

-- --------------------------------------------------------

--
-- Table structure for table `products`
--

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cid` int(11) NOT NULL,
  `product` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`id`, `cid`, `product`) VALUES
(1, 1, 'iPhone 4');

-- --------------------------------------------------------

--
-- Table structure for table `sales`
--

CREATE TABLE IF NOT EXISTS `sales` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `eid` int(11) NOT NULL,
  `pid` int(11) NOT NULL,
  `sold` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `sales`
--


-- --------------------------------------------------------

--
-- Table structure for table `stores`
--

CREATE TABLE IF NOT EXISTS `stores` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `address` varchar(255) NOT NULL,
  `state` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `tax` int(11) NOT NULL,
  `short` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=33 ;

--
-- Dumping data for table `stores`
--

INSERT INTO `stores` (`id`, `address`, `state`, `phone`, `tax`, `short`) VALUES
(31, 'Here', '100', '100000', 10, 'here'),
(32, 'There', '101', '1000001', 111, 'there');

-- --------------------------------------------------------

--
-- Table structure for table `tills`
--

CREATE TABLE IF NOT EXISTS `tills` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `sid` int(11) NOT NULL,
  `eid` int(11) NOT NULL,
  `open` int(11) NOT NULL,
  `cash` int(11) NOT NULL,
  PRIMARY KEY (`id`,`sid`,`eid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `tills`
--

 

I don't have a clue how I can get this to work

 

 

Thanks

 

JRagon

You are executing your query inside the while(...) conditional statement, so it is starting over every pass through the loop. Edit: In this statement: while(mysql_fetch_row(mysql_query($query))){

 

You would need to execute the query once, before the start of the while(){} loop, like every php book, tutorial, and code example that has ever been written.

I'm assuming there's a typo with $prouct on this line...

$query = "INSERT INTO `product` VALUES(null, '$cat', '$prouct')";

should be

$query = "INSERT INTO `product` VALUES(null, '$cat', '$product')";

 

missing the 'AND' in here...

$query = "SELECT id FROM product WHERE catagory='$cat' product='$product'";

should be...

$query = "SELECT id FROM product WHERE catagory='$cat' AND product='$product'";

 

and finally this...

while(mysql_fetch_row(mysql_query($query))){

should be this...

while($pid = mysql_fetch_row(mysql_query($query))){

 

 

You are executing your query inside the while(...) conditional statement, so it is starting over every pass through the loop. Edit: In this statement: while(mysql_fetch_row(mysql_query($query))){

 

You would need to execute the query once, before the start of the while(){} loop, like every php book, tutorial, and code example that has ever been written.

 

Awesome! Thanks guys! That's the display problems fixed. But now, I can't seem to insert stuff :S

 


<?php
require_once('connect.php');
include('functions.php');
if(isset($_POST['product']) && isset($_POST['cat']) && isset($_POST['store']) && isset($_POST['price'])){
$product = escape('product');
$cat = escape('cat');
$store = escape('store');
$price = escape('price');


if (isset($_POST['delete']) && $product != ""){
	$pid = escape('pid');
	$query = "DELETE FROM `products` where product='$product'
			  DELETE FROM `prices` when pid='$pid'";
	mysql_query($query) or die(mysql_error());
}else{
	$query = "INSERT INTO `product` VALUES(null, '$cat', '$product')";
	mysql_query($query);
	$query = "SELECT id FROM products WHERE product='$product'";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);
	$pid = $row[0];
	$query = "INSERT INTO `price` VALUES('$pid', '$store', '$price')";			
}
mysql_query($query);
}
?>
<form action="" method="POST">
Product name: <input type="text" name="product" />
Catagory: <select name="cat">
<?php 
$query = "SELECT id, catagory FROM `catagorys`";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_row($result)){
echo "<option value=\"$row[0]\">$row[1]</option>";
}
?>
</select><br />
At stores:
<?php
$query = "SELECT id,short FROM `stores`";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_row($result)){
echo "<input type=\"checkbox\" name=\"store\" value=\"$row[0]\" />$row[1]<br />";
}
?>
Price:<input type="text" name="price" />

<input type="submit" value="Submit" />

</form>
<?php

$query = 'SELECT products.product, catagorys.catagory, price.price, stores.short, price.pid, products.id
FROM  `products` 
JOIN  `catagorys` ON products.cid = catagorys.id
JOIN  `price` ON price.pid = products.id
JOIN  `stores` ON price.sid = stores.id;';
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result) or die(mysql_error());
$ppid = "";
while($row = mysql_fetch_row($result)){
if($ppid != $row[4]){
	$ppid = $row[4];
	echo <<<_ENDHTML
Product: $row[0] <br />
Catagory: $row[1]<br />
_ENDHTML;
	$query = "SELECT price.price, stores.short, price.pid, products.id
	FROM  `products` 
	JOIN  `price` ON price.pid = products.id
	JOIN  `stores` ON price.sid = stores.id
	WHERE products.id =  '$row[5]'";
	$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result)){
		echo "$$row[0] at $row[1]<br />";
	}	
	echo <<<_ENDHTML

<form action="" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="product" value="$row[0]" />
<input type="hidden" name="price" value="$row[2]" />
<input type="hidden" name="store" value="$row[3]" />
<input type="hidden" name="cat" value="$row[1]" />
<input type="hidden" name="pid" value="$row[4]" />
<input type="submit" value="DELETE RECORD" /></form>
</form>
_ENDHTML;
}elseif($ppid == $row[4]){
	echo "<br />$row[2] at $row[3]";
}
}

?>

 

I have no idea what I've done wrong.

I believe you have another typo...

 

this...

$query = "INSERT INTO `product` VALUES(null, '$cat', '$product')";

should be this...

$query = "INSERT INTO `products` VALUES(null, '$cat', '$product')";

You were missing the 's' in 'products'

<form action="" method="POST">
Product name: <input type="text" name="product" />
Catagory: <select name="cat">
<?php 
$query = "SELECT id, catagory FROM `catagorys`";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_row($result)){
echo "<option value=\"$row[0]\">$row[1]</option>";
}
?>
</select><br />
At stores:
<?php
$query = "SELECT id,short FROM `stores`";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_row($result)){
echo "<input type=\"checkbox\" name=\"store\" value=\"$row[0]\" />$row[1]<br />";
}
?>
Price:<input type="text" name="price" />

<input type="submit" value="Submit" />

</form>
<?php

$query = 'SELECT products.product, catagorys.catagory, price.price, stores.short, price.pid, products.id
FROM  `products` 
JOIN  `catagorys` ON products.cid = catagorys.id
JOIN  `price` ON price.pid = products.id
JOIN  `stores` ON price.sid = stores.id;';
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result) or die(mysql_error());
$ppid = "";
while($row = mysql_fetch_row($result)){
if($ppid != $row[4]){
	$ppid = $row[4];
	echo <<<_ENDHTML
Product: $row[0] <br />
Catagory: $row[1]<br />
_ENDHTML;
	$query = "SELECT price.price, stores.short, price.pid, products.id
	FROM  `products` 
	JOIN  `price` ON price.pid = products.id
	JOIN  `stores` ON price.sid = stores.id
	WHERE products.id =  '$row[5]'";
	$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result)){
		echo "$$row[0] at $row[1]<br />";
	}	
	echo <<<_ENDHTML

<form action="" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="product" value="$row[0]" />
<input type="hidden" name="price" value="$row[2]" />
<input type="hidden" name="store" value="$row[3]" />
<input type="hidden" name="cat" value="$row[1]" />
<input type="hidden" name="pid" value="$row[4]" />
<input type="submit" value="DELETE RECORD" /></form>
</form>
_ENDHTML;
}elseif($ppid == $row[4]){
	echo "<br />$row[2] at $row[3]";
}
}

 

From line 28

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.