Jump to content

Fatal error


mtgriffiths

Recommended Posts

Hi All,

 

I am having a problem when trying to get my shopping cart working. The error i am getting isL

 

Fatal error: Call to undefined function: additem() in /var/www/html/m.griffiths/ECOMM/ShoppingCart.php on line 45

 

The code around and for the shopping cart is:

 

if(isset($_GET['action'])){
switch($_GET["action"])
{
	case "add_item":
	{
		$newq=$_POST["Quantity"];
		additem(mysql_real_escape_string($_GET["id"]), $newq);
		ShowCart();
		break;
	}

 

Can anyone help me?

 

Thanks in advance

 

Matthew

Link to comment
Share on other sites

Sure. Here is all of the code for the shopping cart.

If anymore script from other pages is needed. I will post it as soon as possible.

Thanks again

 

<?php
Ini_set('error_reporting', E_ALL);
session_start();
if (!array_key_exists('first_name', $_SESSION)) {
header("Location: login.php");
}
else {
?>
<?php	require('mysql_connect.php');?>
<html>
<STYLE type="text/css">
A:link{color:Blue;text-decoration:none}
A:visited{color:Blue;text-decoration:none}
A:active{color:Blue;text-decoration:none}
A:hover{color:Blue;text-decoration:none}
</STYLE>
<style type="text/css">
<!--
.style1 {
font-family: Calibri;
font-weight: bold;
color: #FFFFFF;
}
.style2 {font-family: Calibri}
.style4 {color: #FFFFFF}
.style6 {font-family: Calibri; font-weight: bold; }
-->
</style>

<title> The Car Company </title>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
<body>
<div align="center">
  <table width="100%" border="0" height="100%">
    <tr BGCOLOR="#0099FF">
    
          <?php
  
if(isset($_GET['action'])){
switch($_GET["action"])
{
	case "add_item":
	{
		$newq=$_POST["Quantity"];
		additem(mysql_real_escape_string($_GET["id"]), $newq);
		ShowCart();
		break;
	}
	case "update_item":
	{
		UpdateItem(mysql_real_string_escape($_GET["id"]), mysql_real_string_escape($_GET["qty"]));
		ShowCart();
		break;
	}
	case "remove_item":
	{
		RemoveItem(mysql_real_string_escape($_GET["id"]));
		ShowCart();
		break;
	}

	case "remove_all_items":
	{
		RemoveAllItems();
		ShowCart();
		break;
	}

	case "replace_items":
	{
		ReplaceItem(mysql_real_string_escape($_GET["id"]), mysql_real_string_escape($_GET["qty"]));
		ShowCart();
		break;
	}
	default:
	{
		ShowCart();
		break;
	}

}
}else{
echo ' i am here there is a problem with the URL GET function ';
} ?>

     
  <?php
function RemoveAllItems()
   
      {
         // Uses an SQL delete statement to remove ALL items from
   
          // the users cart
      global $dbServer, $dbUser, $dbPass, $dbName;
     // Get a connection to the database
   
          $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
  
          mysql_query("DELETE FROM cart WHERE cookieId > 0");
  
      }

function additem($car_id, $qty)
{
	// Will check whether or not this item
	// already exists in the cart table.
	// If it does, the UpdateItem function
	// will be called instead

	global $dbServer, $dbUser, $dbPass, $dbName;

	// Get a connection to the database
	$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

	// Check if this item already exists in the users cart table
	$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and car_id = $car_id");
	$row = mysql_fetch_row($result);
	$numRows = $row[0];

	if($numRows == 0)
	{

		// This item doesn't exist in the users cart,
		// we will add it with an insert query

		@mysql_query("insert into cart(cookieId, car_id, qty) values('" . GetCartId() . "', $car_id, $qty)");
	}
	else
	{
		// This item already exists in the users cart,
		// we will update it instead
		UpdateItem($car_id, $qty);
	}
}

function UpdateItem($car_id, $qty)
{
	// Updates the quantity of an item in the users cart.
	// If the qutnaity is zero, then RemoveItem will be
	// called instead

	global $dbServer, $dbUser, $dbPass, $dbName;

	// Get a connection to the database
	$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

	if($qty == 0)
	{
		// Remove the item from the users cart
		RemoveItem($car_id);
	}
	else
	{
		$result=mysql_query("select qty from cart where cookieId = '" . GetCartId() . "' and car_id = $car_id");
		$row=mysql_fetch_array($result);
		$newq=$row['qty']; // so this is getting the value from the sql query result and assigning it to the newq variable
		$newq=$newq+$qty; // the calculation done here
		mysql_query("update cart set qty = $newq where cookieId = '" . GetCartId() . "' and car_id = $car_id");
	}
}

function RemoveItem($car_id)
{
	// Uses an SQL delete statement to remove an item from
	// the users cart

	global $dbServer, $dbUser, $dbPass, $dbName;

	// Get a connection to the database
	$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

	mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and car_id = $car_id");
}

function ReplaceItem($car_id,$qty)
{
	//here is where we are going to replace the values
	global $dbServer, $dbUser, $dbPass, $dbName;

	// Get a connection to the database
	$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
	mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and car_id = $car_id");
}



function ShowCart()
{
	// Gets each item from the cart table and display them in
	// a tabulated format, as well as a final total for the cart

	global $dbServer, $dbUser, $dbPass, $dbName;

	// Get a connection to the database
	$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

	$totalCost = 0;
	$result = mysql_query("select * from cart inner join car_details on cart.car_id = car_details.car_id where cart.cookieId = '" . GetCartId() . "' order by car_details.Model asc");
	$sql_num_rows = mysql_num_rows($result);

	?>
      <td height="68"><p align="center" class="style1"><font size="+3"><img src="Banner.jpg"></font></p>      </td>
    </tr>
<tr BGCOLOR="#0099FF">
      <td><div align="center" class="style1">
      <a href="Home.php">Home</a> ---- 
      <a href="Main.php">Car Search</a>----
      <a href="ShoppingCart.php">Shopping Cart</a>
      <a href="logout.php">Logout</a>----
      <a href="change_password.php">Change Password</a>
      <span class="style4"> ---- </span>Contact</span></div></td>
    </tr>
    <tr bordercolor="#0099FF">
      <td><p align="center"><span class="style6">
      <font color="#0099FF">Your Personal Shopping Cart:</font>
            <script language="JavaScript">

		function UpdateQty(item)
		{
			itemId = item.name;
			newQty = item.value;

document.location.href = 'ShoppingCart.php?action=replace_items&id='+itemId+'&qty='+newQty;
		}

	    </script>
  </span>
        <body bgcolor="#ffffff">
<form method="get" name="frmCart" class="style2">
	  <table width="100%" cellspacing="0" cellpadding="0" border="0">
            <tr>
              <td width="13%" height="25" bgcolor="#0099FF"><font size="2" color="white">   <b>Qty</b> </font> </td>
              <td width="17%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Manufacturer</b> </font> </td>
              <td width="13%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Model</b> </font> </td>
              <td width="13%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Colour</b> </font> </td>
              <td width="13%" bgcolor="#0099FF"><font size="2" color="white"><b>Price</b></font></td>
              <td width="17%" height="25" bgcolor="#0099FF"><font size="2" color="white"><b>Amount in Stock</b></font></td>
              <td width="14%" height="25" bgcolor="#0099FF"><font size="2" color="white"> <b>Remove?</b> </font> </td>
            </tr>
            <?php

		while($row = mysql_fetch_array($result))
		{
			// Increment the total cost of all items
			$totalCost += ($row["qty"] * $row["Price"]);
			?>
            <tr>
              <td width="13%" height="25"><font size="2" color="black"><?php $qty0 += $row["qty"]; ?>
               <input type="text" name="<?php echo $row["car_id"]; ?>" onChange="UpdateQty(this)" value= "<?php echo $row["qty"] ?> " size="3"/>               </td>
              <td width="17%" height="25"><font size="2" color="black"> <?php echo $row["Manufacturer"]; ?> </font> </td>
              <td width="13%" height="25"><font size="2" color="black"> <?php echo $row["Model"]; ?></font> </td>
              <td width="13%" height="25"><font size="2" color="black"> <?php echo $row["Colour"]; ?></font> </td>
              <td width="13%"><font size="2" color="black">£<?php echo number_format($row["Price"], 2, ".", ","); ?> </font></td>

              
              <?php 
//$stock = mysql_query("UPDATE car_database.car_details SET Stock_Level = (Stock_Level - $qty)WHERE car_details.car_id = $row[car_id]"); 
		  
		  ?>
              
              <td width="17%" height="25"><font size="2" color="black">
		  <?php echo $row["Stock_Level"]; ?></font></td>
              <td width="14%" height="25"><font size="2" color="black"> <a href="ShoppingCart.php?action=remove_item&id=<?php echo $row["car_id"]; ?>">Remove</a></font> </td>
            
            <?php
		}

		// Display the total
		?>
            <tr>
              <td colspan="7"><hr size="1" color="#0099FF" noshade>              </td>
            </tr>
            <tr>
              <td colspan="2"><font size="2" color="black"> <a href="main.php"><< Continue Searching</a></font></td>
              <td width="13%" colspan="1"><div align="center"></div></td>
              <td> </td>
              <td><font size="2" color="black"><b>Total Cars: <?php echo "$sql_num_rows" ?></b></font></td>
              <td> </td>
              <td><font size="2" color="black">
              
              <a href="ShoppingCart.php?action=remove_all_items">Empty Cart</a></font></td>
            </tr>
            <tr>
              <td> </td>
              <td> </td>
              <td> </td>
              <td> </td>
              <td><font size="2" color="black"><b>Total Quantity: <?php echo "$qty0" ?></b></font></td>
              <td> </td>
            </tr>
            <tr>
              <td> </td>
              <td> </td>
              <td> </td>
              <td> </td>
              <td><font size="2" color="black"><b>Total: £<?php echo number_format($totalCost, 2, ".", ","); ?></b></font></td>
              <td> </td>
            </tr>
          </table>
    </form>
</body>
		</html>
		<?php
}

?><br>
        </p>
      <p>      </p></td>
    </tr>
    <tr BGCOLOR="#0099FF">
      <td><div align="center" class="style1">The Car Company</div></td>
    </tr>
  </table>
</div>
</body>
</html>
<?php } ?>

  

Link to comment
Share on other sites

Your using the functions, then creating the functions, it doesn't make sense.

 

Example

 

<?php

sayHello(); // UNDEFINED FUNCTION BECAUSE FUNCTION WAS CREATED AFTER YOU USED THE FUNCTION 

function sayHello(){

echo "hello";

}

function sayHello(){

echo "hello";

}


sayHello(); // OUTPUT: HELLO
?>

Link to comment
Share on other sites

The problem is not that the function definition is located after it is referenced (php is a parsed/executed language, so function definitions can be located anywhere in the source code - as long as they are not conditionally defined), the problem is that the function definition(s) is(are) located inside of a conditional statement. The definition won't exist until the code in the conditional statement is executed.

 

Either put the function definitions near the start of the file (proper/common programming convention or if you really want, put them at the end of the file, outside of the conditional {} statements) or put them in an include/require file and include that near the start of the file.

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.