Jump to content

[SOLVED] Help needed ASAP!


hass1980

Recommended Posts

Hi,

 

Im getting two errors coming from the admin site of the website. Products.php and catergories.php

 

Fatal error: Call to undefined function products() in C:\wamp\www\cart\admin\products.php on line 21

 

Fatal error: Call to undefined function categories() in C:\wamp\www\cart\admin\categories.php on line 23

 

I have also added the site as a zip file if you need the additional files!

 

Categories.php

 

<?php
// Include MySQL class
require_once('../inc/mysql.class.php');
// Include database connection
require_once('../inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Include functions
require_once('inc/category.inc.php');
// Include functions
require_once('inc/product.inc.php');
// Start the session
session_start();

if(!isset($_SESSION['AdminID']))
{
     return header("location: myaccount.php?action=login");
     exit;
}

$array['categories'] = 'selected';
siteHeader($array);
echo categories();
siteFooter();
?>

 

products.php

 

<?php
// Include MySQL class
require_once('../inc/mysql.class.php');
// Include database connection
require_once('../inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Include functions
require_once('inc/product.inc.php');
// Start the session
session_start();

if(!isset($_SESSION['AdminID']))
{
     return header("location: myaccount.php?action=login");
     exit;
}

$array['products'] = 'selected';
siteHeader($array);
echo products();
siteFooter();
?>


 

[attachment deleted by admin]

Link to comment
Share on other sites

Try replacing

 

// Include functions
require_once('inc/functions.inc.php');
// Include functions
require_once('inc/category.inc.php')
// Include functions
require_once('inc/product.inc.php');

 

with:

 

// Include functions
require_once('admin/inc/functions.inc.php');
// Include functions
require_once('admin/inc/category.inc.php');
// Include functions
require_once('admin/inc/product.inc.php');

 

 

Link to comment
Share on other sites

from the directory Categories.php and products.php are in, navigate to the 'inc' folder, open function.inc.php and products.inc.php and locate the functions in question .. make sure they're there, don't just assume it 'cause somebody told you so.

Link to comment
Share on other sites

heres the functions.inc.php

 


<?php
function GetSQLValueString($theValue, $theType='', $theDefinedValue = "", $theNotDefinedValue = "") 
{
   $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;	
   switch ($theType) 
   {
       case "text":
           $theValue = ($theValue != "") ? "'" . $theValue . "'" : "''";
           break;    
       case "long":
       case "int":
           $theValue = ($theValue != "") ? intval($theValue) : 0;
           break;
       case "double":
           $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "''";
           break;
       case "date":
           $theValue = ($theValue != "") ? "'" . $theValue . "'" : "''";
           break;
       case "defined":
           $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
           break;
   }

   return $theValue;
}




function siteHeader($array='')
{
   if(!is_array($array))
       $array = array();
   echo '
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <title>PHP Shopping Cart Demo &#0183; Bookshop</title>
       <LINK REL=StyleSheet HREF="../css/style.css" TYPE="text/css"  />
       <script src="../js/ajax.js"></script>
       <script src="../js/common.js"></script>
   </head>
   <body>

   <div id="wrapper">
      
   <div id="header"></div>

   <div id="leftcolumn">
       <ul id="nav">';
   if(isset($_SESSION['AdminID']))
   {
       echo '  <a href="index.php" class="'.$array['index'].'">Home</a>
               <a href="categories.php" class="'.$array['categories'].'">Category</a>
               <a href="products.php" class="'.$array['products'].'">Products</a>
               <a href="myaccount.php?action=myaccount" class="'.$array['myaccount'].'">My Account</a>
               <a href="myaccount.php?action=logout">Logout</a>';
   }
   else 
   {
       echo '  <a href="myaccount.php?action=login">Login</a>';
   }
   echo '</ul>
   </div>
   <div id="rightcolumn">';
}

function siteFooter()
{
   echo '
   </div>
   <div id="footer"><p> </p><p> </p>
   </div>

   </body>
   </html>';
}


function adminHeads()
{
   $head = '<h1>Adminstration Panel</h1><p>Your Shopping Cart</p>';
   if(isset($_SESSION['AdminID']) && $_SESSION['AdminID']>0)
       $head .= '<p align="right"><a href="categories.php?action=view">Categories</a> / <a href="products.php?action=view">Products</a> / <a href="myaccount.php?action=myaccount">My account</a> / <a href="myaccount.php?action=logout">Logout</a></p>';

   return $head;
}

function myAccount()
{
   global $db;
   $content = '';

   switch ($_POST['action'])
   {
      	case 'login':
      	        loginUser();
      		       break;
      		       
      	case 'myaccount':  
      	        updateUser();     	 
      		       break;
   }

   If(isset($_GET['msg']))
       $content = "<div class='message' align='center'><br>".$_GET['msg']."</div>";

   switch ($_GET['action'])
   {
      	case 'login':
      	        $content .= "
<form method='POST' onsubmit='return loginCheck()' id='tableclass'>
<input type='hidden' name='action' value='login'>
<h1>Login</h1>
<table cellpadding='0' cellspacing='0' width='100%' border='0' align='center'>
   <tr valign='middle' height='30'>
       <td align='right' width='50%'>Username : </td>
       <td align='left' width='50%'><input type='text' name='username' id='username' value=''></td>
   </tr>
   <tr valign='middle' height='30'>
       <td align='right'>Password : </td>
       <td align='left'><input type='password' name='password' id='password' value=''></td>
   </tr>
   <tr valign='middle' height='30'>
       <td colspan='2' align='center'>
           <input type='submit' value='     Login     ' name='btn_lgn'>
           <input type='reset' value='     Cancel     ' name='btn_cel'>
       </td>
   </tr>
</table>
<br>
</form>";       	 
      		       break;
      		       
      	case 'myaccount':
      	        $customers_sql    = "SELECT * FROM ".ADMIN_TABLE." WHERE AdminID=".$_SESSION['AdminID'];
               $customers_result = $db->query($customers_sql);
               $customers_row    = $customers_result->fetch();
      	        $content .= "
<form method='POST' id='tableclass' name='registerform'>
<input type='hidden' name='action' value='myaccount'>
<h1>Myaccount</h1>
<table cellpadding='0' cellspacing='0' width='100%' border='0' align='center'>
   <tr valign='middle' height='30'>
       <td align='right'>Username : </td>
       <td align='left'><input type='text' name='username' id='username' value='".$customers_row['Username']."'></td>
   </tr>
   <tr valign='middle' height='30'>
       <td align='right'>Password : </td>
       <td align='left'><input type='password' name='password' id='password' value='".$customers_row['Password']."'></td>
   </tr>
   <tr valign='middle' height='30'>
       <td align='right'>Re-enterPassword : </td>
       <td align='left'><input type='password' name='password1' id='password1' value='".$customers_row['Password']."'></td>
   </tr>
   <tr valign='middle' height='30'>
       <td colspan='2' align='center'>
           <input type='button' value='     Update Details     ' name='btn_reg' onclick='myaccountAdminCheck()'>
       </td>
   </tr>
</table>
<br>
</form>";       	 
      		       break;
      		       
      	case 'logout':
      	        session_unregister('admin_id');       	        
      		       break;
      		       
      	default:
      	        return header("Location: index.php");
      		       break;
   }
   if((isset($_SESSION['AdminID']) && $_SESSION['AdminID']>0 && ($_REQUEST['action']=='login')) || (!isset($_SESSION['AdminID']) && ($_REQUEST['action']=='myaccount' || $_REQUEST['action']=='logout')))
   {
       return header("Location: index.php");
       exit;
   }

   return $content;
}

function loginUser()
{
   global  $db;

   $username = GetSQLValueString($_POST['username'],'text');
   $password = GetSQLValueString($_POST['password'],'text');

   $customers_sql    = "SELECT * FROM ".ADMIN_TABLE." WHERE Username=$username AND Password=$password";
   $customers_result = $db->query($customers_sql);
   if($customers_result->size()>0)
   {
       $customers_row = $customers_result->fetch();
       $_SESSION['AdminID'] = $customers_row['AdminID'];

       return header("Location: products.php?action=view&msg=Successfully LoggedIn");
   }
   else 
   {
       return header("Location: myaccount.php?action=myaccount&msg=Invalid Login Details");     
   }
   exit;
}

function updateUser()
{
   global  $db;

   $customer_id = $_SESSION['AdminID'];

   $username = GetSQLValueString($_POST['username'],'text');
   $password = GetSQLValueString($_POST['password'],'text');

   $customer_sql = "UPDATE ".ADMIN_TABLE." SET Username=$username,Password=$password WHERE AdminID='$customer_id'";
   $db->query($customer_sql);

   return header("Location: myaccount.php?action=myaccount&msg=Details Updated");
   exit;
}
?>

 

products.inc.php

 


<?php
function products()
{
   global $db;
   $content = '';

   switch ($_POST['action'])
   {
      	case 'add':
      	        addProduct();
      		       break;
      	case 'edit':
      	        updateProduct();
      		       break;
   }
   $content = '';
   If(isset($_GET['msg']))
       $content = "<div class='message' align='center'><br>".$_GET['msg']."</div>";

   switch ($_GET['action'])
   {       		       
      	case 'edit':
      	case 'add':
      	        (!isset($_GET['id'])) ? $head='Add Product' : $head='Edit Product';
      	        (!isset($_GET['id'])) ? $button='Add Product' : $button='Update Product';
      	        $product_row = array();
      	        if(isset($_GET['id']))
      	        {
          	        $product_sql    = "SELECT * FROM ".PRODUCT_TABLE." WHERE ProductID=".$_GET['id'];
                   $product_result = $db->query($product_sql);
                   $product_row    = $product_result->fetch();

                   $image = "
                             <tr valign='top' height='30'>
                                 <td align='right'>Current Image : </td>
                                 <td align='left'><img type='file' src='../images/product/".$_GET['id'].'.'.$product_row['ProductImage']."' width='110' height='90'></td>
                             </tr>";

      	        }
      	        $content .= "
<form method='POST' id='tableclass' name='registerform' onsubmit='return checkProduct()' enctype='multipart/form-data'>
<input type='hidden' name='action' id='action' value='".$_GET['action']."'>
<h4>Product Management - $head</h4>
<table cellpadding='0' cellspacing='0' width='100%' border='0' align='center'>
   <tr valign='middle' height='30'>
       <td align='right'>Product Name : </td>
       <td align='left'><input type='text' name='name' id='name' value='".$product_row['ProductName']."'></td>
   </tr>
   <tr valign='top' height='30'>
       <td align='right'>Description : </td>
       <td align='left'><textarea name='description' id='description' cols='50' rows='5'>".$product_row['ProductDesc']."</textarea></td>
   </tr>
   <tr valign='middle' height='30'>
       <td align='right'>Category : </td>
       <td align='left'>".category($product_row['CatID'])."</td>
   </tr>
   $image
   <tr valign='middle' height='30'>
       <td align='right'>Image : </td>
       <td align='left'><input type='file' name='image' id='image' value='".$product_row['ProductImage']."'></td>
   </tr>
   <tr valign='middle' height='30'>
       <td align='right'>Price : </td>
       <td align='left'><input type='text' name='price' id='price' value='".$product_row['ProductPrice']."'></td>
   </tr>
   <tr valign='middle' height='30'>
       <td colspan='2' align='center'>
           <input type='submit' value='     $button     ' name='btn_reg' onclick=''>
       </td>
   </tr>
</table>
<br>
</form>";       	 
      		       break;
      		       
      	case 'delete':
  	        	deleteProduct($_GET['id']);  	        
	           break;
      		       
      	case 'view':
      	default:
      	        $categories_sql    = "SELECT * FROM ".PRODUCT_TABLE." AS PR,".CATEGORY_TABLE." AS CA WHERE PR.CatID=CA.CatID ORDER BY PR.ProductName ASC";
               $categories_result = $db->query($categories_sql);
               $content .= "
               <h4>Product Management</h4>
               <div align='center'>
               <div align='right'><a href='products.php?action=add' class='links'>Add New Product</a>     </div>
               <table cellpadding='0' cellspacing='0' width='98%' border='0' align='center' class='tableborder2'>
                   <tr valign='middle' height='30'>
                       <td align='left' class='listbg'>Product Name</td>
                       <td align='left' class='listbg'>Category</td>
                       <td align='center' width='50%' class='listbg'>Action</td>
                   </tr>";
               if($categories_result->size()>0)
               {
                   $i = 1;
                   while ($categories_row = $categories_result->fetch())
                   {
                       $content .= "
                       <tr valign='middle' height='30' class='list1bg' onmouseover='this.className=\"list2bg\"' onmouseout='this.className=\"list1bg\"'>
                           <td align='left' class='normaltext'>".$categories_row['ProductName']."</td>
                           <td align='left' class='normaltext'>".$categories_row['CatName']."</td>
                           <td align='center' class='normaltext'><a href='products.php?action=edit&id=".$categories_row['ProductID']."' class='links'>Edit</a> / <a href='products.php?action=delete&id=".$categories_row['ProductID']."' onclick='return window.confirm(\"Do you really want to delete this product ?\")' class='links'>Delete</a></td>
                       </tr>";
                       $i++;
                   }
               }
               else 
               {
                   $content .= "
                   <tr valign='middle' height='30'>
                       <td colspan='10' class='message' align='center'>No Products Present</td>
                   </tr>";
               }
               $content .= "</table></div><br>";
      		       break;
   }

   return $content;
}

function category($catid=0)
{
   global $db;

   $categories_sql    = "SELECT * FROM ".CATEGORY_TABLE." WHERE 1 ORDER BY CatName ASC";
   $categories_result = $db->query($categories_sql);
   $categories        = '<select name="cat_id" id="cat_id">';
   if($categories_result->size()>0)
   {
       while ($categories_row = $categories_result->fetch())
       {
           $cat_id   = $categories_row['CatID'];
           $cat_name = $categories_row['CatName'];

           ($cat_id==$catid) ? $selected='selected' : $selected='';

           $categories .= "<option value='$cat_id' $selected>$cat_name</option>";
       }
   }
   $categories .= '</select>';

   return $categories;
}

function addProduct()
{    
   global $db;

   $name        = GetSQLValueString($_POST['name'],'text');
   $description = GetSQLValueString($_POST['description'],'text');
   $cat_id      = GetSQLValueString($_POST['cat_id'],'text');
   $price       = GetSQLValueString($_POST['price'],'text');

   $products_sql    = "INSERT INTO ".PRODUCT_TABLE."(CatID,ProductName,ProductDesc,ProductPrice) VALUES($cat_id,$name,$description,$price)";
   $products_result = $db->query($products_sql);
   $products_id     = $products_result->insertID();

   if($_FILES['image']['size']>0 && strstr($_FILES['image']['type'],'image'))
   {       
       $tmpname   = $_FILES['image']['tmp_name'];
       $filename  = $_FILES['image']['name'];
       $filenames = explode('.',$filename);
       $ext       = $filenames[count($filenames)-1];

       copy($tmpname,"../images/product/$products_id.$ext");

       $ext = GetSQLValueString($ext,'text');
       $products_sql = "UPDATE ".PRODUCT_TABLE." SET ProductImage=$ext WHERE ProductID='$products_id'";
       $db->query($products_sql);       
   }

   return header("Location: products.php?action=view&msg=Product Details Added");
   exit;
}

function updateProduct()
{    
   global $db;

   $products_id = $_GET['id'];

   $name        = GetSQLValueString($_POST['name'],'text');
   $description = GetSQLValueString($_POST['description'],'text');
   $cat_id      = GetSQLValueString($_POST['cat_id'],'text');
   $price       = GetSQLValueString($_POST['price'],'text');

   $products_sql = "UPDATE ".PRODUCT_TABLE." SET CatID=$cat_id,ProductName=$name,ProductDesc=$description,ProductPrice=$price WHERE ProductID='$products_id'";
   $db->query($products_sql);

   if($_FILES['image']['size']>0 && strstr($_FILES['image']['type'],'image'))
   {
       $product_sql    = "SELECT * FROM ".PRODUCT_TABLE." WHERE id=".$_GET['id'];
       $product_result = $db->query($product_sql);
       $product_row    = $product_result->fetch();
       @unlink('../images/'.$product_row['id'].'.'.$product_row['image']);

       $tmpname   = $_FILES['image']['tmp_name'];
       $filename  = $_FILES['image']['name'];
       $filenames = explode('.',$filename);
       $ext       = $filenames[count($filenames)-1];

       copy($tmpname,"../images/product/$products_id.$ext");

       $ext = GetSQLValueString($ext,'text');
       $products_sql = "UPDATE ".PRODUCT_TABLE." SET ProductImage=$ext WHERE ProductID='$products_id'";
       $db->query($products_sql);       
   }

   return header("Location: products.php?action=view&msg=Product Details Updated");
   exit;
}

function deleteProduct($pid=0,$cid=0)
{
   global $db;

   if($cid!=0 && $pid==0)
   {
       $products_sql    = "SELECT * FROM ".PRODUCT_TABLE." WHERE CatID='$cid'";
       $products_result = $db->query($products_sql);
       if($products_result->size()>0)
       {
           while ($products_row = $products_result->fetch())
           {
               $pid .= ",".$products_row['ProductID'];
           }
       }
   }

   $product_sql    = "SELECT * FROM ".PRODUCT_TABLE." WHERE ProductID IN ($pid)";
   $product_result = $db->query($product_sql);
   while($product_row = $product_result->fetch())
   {
       @unlink('../images/product/'.$product_row['ProductID'].'.'.$product_row['ProductImage']);
   }

   $products_sql    = "DELETE FROM ".PRODUCT_TABLE." WHERE ProductID IN ($pid)";
   $db->query($products_sql);

   return header("Location: products.php?action=view&msg=Product Deleted");
   exit; 
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.