Jump to content

IF statements or similar


chet139

Recommended Posts

Hi,

 

Most of may pages require checking if a set of results have been returned as a result of a query. If so I would like it to perform some sort of action such as display a table with some contents inside it.

 

The simple problem I have is, is that if no records are returned I would like it to simply say so. However what is happening at the moment is only the top header of the table I have created appears of no records are returned as a result of the query, im not to sure  why as i put it all inside a an IF statement. Heres the code extract below it is quite big but im sure you guys ill be able to figure out what I need to ammend. Im sure it cant be to hard but im quite new and therefore stuck.

 

<?php
require_once ('dbcon.html');//connect to db
$query = "select productId, productMake, productModel, productType, stockQuantity
				from product";

			$where = array();
			if(!empty($_POST['productId'])) {$where[] = "productId = '" . $_POST['productId'] . "' ";}
			if(!empty($_POST['productMake'])) {$where[] = "productMake = '" . $_POST['productMake'] . "' ";}
			if(!empty($_POST['productModel'])) {$where[] = "productModel = '" . $_POST['productModel'] . "' ";}
			if(!empty($_POST['productType'])) {$where[] = "productType = '" . $_POST['productType'] . "' ";}
			if(!empty($_POST['supplierId'])) {$where[] = "supplierId = '" . $_POST['supplierId'] . "' ";}
			if(sizeOf($where) > 0){
		    $query .= " WHERE " . implode(" and ",$where);
			}

$result = @mysql_query($query);//runs query

if ($result)
{
//display records
 echo'<table border = "1" width = "600" cellpadding = "1" >
<tr>
		<td align = "left"><b>Product ID</b></td>
 	<td align = "left"><b>Product Make</b></td>
 	<td align = "left"><b>Product Model</b></td>
 	<td align = "left"><b>Product Type</b></td>
 	<td align = "left"><b>Current Stock Level</b></td>
	</tr>';
	//fetch and print all records
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
		echo
		'
		<tr>
		 				<td align="left"><b><a href="prodDetails.html?productId='.$row['productId'].'"> '. $row['productId'] . '</a></b></td>
				 		<td align="left">' . $row['productMake'] . '</td>
				 		<td align="left">' . $row['productModel'] . '</td>
				 		<td align="left">' . $row['productType'] . '</td>
				 		<td align="left">' . $row['stockQuantity'] . '</td>
		</tr>
		';
	}
	echo '</table>';
mysql_free_result($result);
} else {
		echo'<p class="error"> Product details could not be accessed</p>';
		echo '<p>' .mysql_error().'<br />
		<br />Query: ' . $query . '</p>';
	}
mysql_close();
?>

 

So as per the code above the table headers shown above the WHILE loop are still displayed if no records are returned... i dont want them to be if nothing is returned.

 

If you need more info just ask. Thanks alot!

Link to comment
https://forums.phpfreaks.com/topic/105758-if-statements-or-similar/
Share on other sites

Hello, you might want to use mysql_num_rows(), it will count the rows(records) available, and return how many there are. if there are ZERO then before the loop put an IF statement if() with the string holding mysql_num_rows(); http://php.net/mysql_num_rows

 

I'm sorry if it was a bit complicated.

off topic but ive never seen a query like that in my life does it work like that mate intrested......

 

i think that the varable names are double posting, if you try that query with an insert your get injection man i am sure........

<?php
require_once ('dbcon.html');//connect to db
$query = "select productId, productMake, productModel, productType, stockQuantity
				from product";

			$where = array();
			if(!empty($_POST['productId'])) {$where[] = "productId = '" . $_POST['productId'] . "' ";}
			if(!empty($_POST['productMake'])) {$where[] = "productMake = '" . $_POST['productMake'] . "' ";}
			if(!empty($_POST['productModel'])) {$where[] = "productModel = '" . $_POST['productModel'] . "' ";}
			if(!empty($_POST['productType'])) {$where[] = "productType = '" . $_POST['productType'] . "' ";}
			if(!empty($_POST['supplierId'])) {$where[] = "supplierId = '" . $_POST['supplierId'] . "' ";}
			if(sizeOf($where) > 0){
		    $query .= " WHERE " . implode(" and ",$where);
			}

$result = @mysql_query($query);//runs query
?>

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.