Jump to content

[SOLVED] Sometimes variable is there and sometimes not!


mike12255

Recommended Posts

I got a peice of code which in basic is just a function with a query in it. Sometimes the variable $amount is non existant though because the the user does not have the correct session. This is a simple placement error of my if statment im sure, i just cant tell were i should move the else statment, or the if statment. Anyone got ideas?

 

<?php
function getqty(){

$cartContent = array();

$sid = session_id();

$pid = $_GET['p'];
$pid = mysql_real_escape_string($pid)
$sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'";

if ($result = mysql_query($sql)){

while($row = mysql_fetch_array($result)){
	$amount = $row['ct_qty'];
}
}else{
	$amount = "0";
}
return $amount;
}?>

Link to comment
Share on other sites

Not sure if this make sense but.

 

Say the session is empty or wrong. Then your:

if ($result = mysql_query($sql))

 

Is going to return nothing. And so your

return $amount;

will return empty?

 

Maybe put in a if ct_session_id == EMPTY  or NULL then set a value for $amount.

 

if session != empty do SQL else return $amount = 0;

 

Not sure if that is right or makes sense?

 

Link to comment
Share on other sites

I tried the to things below, just getting the same error.

 

Notice: Undefined variable: amount in /home/schoolw1/public_html/nifty/library/cart-functions.php on line 98

You currently have of Medium Impression Kit in your cart,

 

** Line 98 is the return amount line

 

<?php
function getqty(){
   
   $cartContent = array();

   $sid = session_id();
   
   $pid = $_GET['p'];
   $pid = mysql_real_escape_string($pid)
   $sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'";
   

if(!empty($sid)){
$result = mysql_query($sql);
   
   while($row = mysql_fetch_array($result)){
      $amount = $row['ct_qty'];
   }else{
$amount = "0";
}
   return $amount;
}?>

 

<?php
function getqty(){
   
   $cartContent = array();

   $sid = session_id();
   
   $pid = $_GET['p'];
   $pid = mysql_real_escape_string($pid)
   $sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'";
   

$result = mysql_query($sql) or die (mysql_error());
   
   while($row = mysql_fetch_array($result)){
      $amount = $row['ct_qty'];
   return $amount;
}?>

Link to comment
Share on other sites

I just preset $amount and it seems if the amount of rows is non existant it dosnt overwrite the variable:

 

$amount = 0;


$result = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_array($result)){
	$amount = $row['ct_qty'];
}

return $amount;
}

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.