Jump to content

[SOLVED] Practical PHP and MySQL Help


GreenUser

Recommended Posts

Hi all, 

 

I am learning PHP with a book's help, currently working on a shopping cart.  While typing up the practice lessons I have encountered a few problems and can't figure it out.  I have a functions page created that has an undefined variable, not sure how to define it, but I thought it gets defined after someone has placed an order.  Here is the error message and the script:

 

Undefined variable: final in C...\\functions.php on line 23

 

<?php
function pf_validate_number($value, $function, $redirect) {
	if(isset($value) == TRUE) {
		if(is_numeric($value) == FALSE) {
	$error = 1;
}
	if($error == 1) {
 		header("Location: " . $redirect);
}
	else {
		$final = $value;
}
}
	else {
	if($function == 'redirect') {
	header("Location: " . $redirect);
}
	if($function == "value") {
		$final = 0;
}
}

return $final;             /**  (line 23)  **/

}
?>

 

Which I thinks leads to the problem with my products page.  After I log in, I want to view my products but it refreshes the page, and gives me these errors:

 

Undefined index:  id in C:\\...\\products.php on line 5

Undefined index:  id in C:\\...\\products.php on line 9

mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\\...\\products.php on                                         

      line 11

 

<?php
require("db.php");
require("functions.php");

$validid = pf_validate_number($_GET['id'], "redirect", $config_basedir . "index.php");   /** line 5  **/

require("header.php");

$prodcatsql = "SELECT * FROM products WHERE cat_id = " . $_GET['id'] . ";";   /** line 9 **/
$prodcatres = mysql_query($prodcatsql);
$numrows = mysql_num_rows($prodcatres);   /** line 11 **/

if($numrows == 0)
{
	echo "<h1>No Products</h1>";
	echo "There are no products in this category.";
}

else
{

	echo "<table cellpadding='10'>";

	while($prodrow = mysql_fetch_assoc($prodcatres))
	{
		echo "<tr>";
			if(empty($prodrow['image'])) {
		echo "<td><img src='./productimages/dummy.jpg' alt='" . $prodrow['name'] . "'></td>";
	}
	else {
		echo "<td><img src='./productimages/" . $prodrow['image'] . "' alt='" . $prodrow['name'] . "'></td>";
	}

	echo "<td>";
	echo "<h2>" . $prodrow['name'] . "</h2>";
	echo "<p>" . $prodrow['description'];
	echo "<p><strong>OUR PRICE: £" . sprintf('%2f', $prodrow['price']) . "</strong>";
	echo "<p>[<a href='addtobasket.php?id=" . $prodrow['id'] . "'>but</a>]";
	echo "</td>";
	echo "</tr>";

	}

	echo "</table>";

	}
	require("footer.php");
?>

 

In short, I want it to display my products page, instead I am taken to the index page.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/109063-solved-practical-php-and-mysql-help/
Share on other sites

Well, there are many problems. I will be honest, I have no idea what you were trying to do with that function, but the logic just wasn't there. There were a whole bunch of unnecessary variables that did absolutely nothing and far too many if and else statements.

 

Anyway, I've re-written the code for you. Let me know if it works and it what you are looking to do.

 

Take care.

 

<?php
function pf_validate_number($id) {
if(isset($id) && is_numeric($id)) {
	return true;
} else {
	return false;
}
}
?>

 

<?php
require("db.php");
require("functions.php");

$valid_id = pf_validate_number($_GET['id']);

// If ID is valid
if ($valid_id == TRUE) {

	require("header.php");

	$id = $_GET['id'];	

	$prodcatsql = "SELECT * FROM products WHERE cat_id = '$id'";
	$prodcatres = mysql_query($prodcatsql) or die(mysql_error() . "<pre>$query</pre>");
	$numrows = mysql_num_rows($prodcatres);	

	if ($numrows < 1) {

		echo "<h1>No Products</h1>";
		echo "There are currently no products in this category.";

	} else {

		echo "<table cellpadding='10'>";

		while($prodrow = mysql_fetch_array($prodcatres)) {
			echo "<tr>";		
			if(empty($prodrow['image'])) {
				echo "<td><img src='./productimages/dummy.jpg' alt='" . $prodrow['name'] . "'></td>";
			} else {
				echo "<td><img src='./productimages/" . $prodrow['image'] . "' alt='" . $prodrow['name'] . "'></td>";
			}			
			echo "<td>";
			echo "<h2>" . $prodrow['name'] . "</h2>";
			echo "<p>" . $prodrow['description'];
			echo "<p><strong>OUR PRICE: £" . sprintf('%2f', $prodrow['price']) . "</strong>";
			echo "<p>[<a href='addtobasket.php?id=" . $prodrow['id'] . "'>but</a>]";
			echo "</td>";
			echo "</tr>";
		}

		echo "</table>";

	}

	require("footer.php");

// if ID in invalid
} else {	
	header("Location: index.php");		
}
?>

Thanks eRott,

 

The code you rewrote for me works without error,  but the new page is not refreshing.  Since the errors went away I guess they were not involved in my current problem. 

 

I do not know what the functions page is for, looking ahead in the book the author has me add more code to it.  Let's see how it goes when the file is finished.

 

The link to the products page is in this file, no errors are reported by it, this is required in the header file I have written.

 

<h1>Product Categories</h1>
<ul>
<?php

$catsql = "SELECT * FROM categories;";
$catres = mysql_query($catsql);

while($catrow = mysql_fetch_assoc($catres))
{
	echo "<li><a href='" . $config_basedir . "products.php?=" . $catrow['id'] . "'>" . $catrow['name'] . "</a></li>";
}
?>
</ul>

 

The link just refreshes the page.  I don't mean to run anyone around, I don't know where to start.  Other than I wish I picked up a different book. :)

 

In regards to this line:

 

echo "<li><a href='" . $config_basedir . "products.php?=" . $catrow['id'] . "'>" . $catrow['name'] . "</a></li>";

 

What is the value of your variable $config_basedir? Also, there should be a variable name after the page name and in between the question mark and equals symbol. So it should look something like:

 

echo "<li><a href='products.php?id=" . $catrow['id'] . "'>" . $catrow['name'] . "</a></li>";

Huzzah!  That missing 'id' fixed it up.  The errors that accompanied my problem went away as well. 

 

Thanks eRott you rock. 

 

$config_basedir = "http://localhost/shoppingcart/";

 

Anyway that's the variable.  Annoying because I don't think my 'something' knows to grab my index.php as my main page.  I have to concatenate index.php in every time, not sure why it's this way. 

 

Uh, anyway problem is solved!  And I suppose I get one of those green check marks. :)

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.