Help with PHP script


#1

Hi ppl!

I am making a website for someone, and a shopping cart has already been placed in this website, however it is mal-functioning and I don’t seem to be able to figure out why.

When you click on a particular product this is the code which it enters:

index/shop/carrinho/asp/addencomenda.php?produto=AUTO-CURA - &preco=15.47

along with the http address.

It gives me the following error:

Warning: Cannot send session cookie - headers already sent by (output started at /home/sites/site57/web/index/shop/carrinho/asp/abrirconexaoaux.php:25) in /home/sites/site57/web/index/shop/carrinho/asp/addencomenda.php on line 35

Warning: Cannot send session cache limiter - headers already sent (output started at /home/sites/site57/web/index/shop/carrinho/asp/abrirconexaoaux.php:25) in /home/sites/site57/web/index/shop/carrinho/asp/addencomenda.php on line 35
Insert into carro (produto, preco, Idencomenda,data) values (“AUTO-CURA -”,“15.47”,“ca7d14a3cc6a3d8c4cb862b53ecbc0cf”,“2004-06-24”)

I then opened this file called “addencomenda.php” which is “addorder” in english, and this is the code which it contains:

<?php 
include("abrirconexaoaux.php"); 
$ProdutName=""; 
//echo count($HTTP_POST_VARS); 
$count=0; 
$desc=""; 
if($flag == "1")  
{ 
while (list($name, $value) = each($HTTP_POST_VARS) and $count<$ncara) 
{ 
$count++; 
$auxpr=explode("&","$value"); 
if(count($auxpr)>1) 
{ 
  $desc.="$name $auxpr[0]; "; 
  $pp="$auxpr[1]"; 
 } 
else $desc.=" $value; "; 
} 
//echo $desc; 
//echo $pp; 
//exit(); 
 $ProductName="$produto, caracteristicas: $desc"; 
} 
else 
{ 
$ProductName=$produto; 
$pp=$preco; 
} 
$ProductName=trim($ProductName); 
$ProductName=addslashes($ProductName); 
$sess=session_id(); 
if($sess=="") 
{ 
session_start(); 
$sess=session_id(); 
} 
$sess=trim($sess); 
$dt=date("Y-m-d"); 
$sql="Insert into carro (produto, preco, Idencomenda,data) values ("$ProductName","$pp","$sess","$dt")"; 
//echo $sql; 

if(!mysql_query ($sql)) 
{ 
echo $sql; 
exit(); 
} 
include("fecharconexao.php"); 
header("Location: ./carrinho.php"); 
exit(); 
?> 

I need this urgently, would someone please be kind enough to look over this and find the problem… Thanks… If you need anything else I will provide you with it asap.

Thanks in advance,

Prodigal Systems Admin,

Jason Vincent[/code]


#2

Well the error is that html has been sent to the output device. SESSIONS and COOKIES work in the headers. You cannot send header info once the HTML output is being sent. There is a way to spool the entire output (headers and all) until the script finishes executing. I believe it’s a setting in the PHP.INI file, and this would allow you to “Append” Session/Header info, but it’s not a very efficient way to do it.

Typically Session Info should be one of the frist things you do. I suspect the problem in your situation is that there is an INCLUDE at the top that would be doing some output information that is the culprit.

I would suggest reading up on sessions at http://us2.php.net/manual/en/ref.session.php

Good Luck


#3

on the code you gave

if(!mysql_query ($sql)) { echo $sql; <-------- here is the problem exit(); } include("fecharconexao.php"); header("Location: ./carrinho.php"); <----------- before here exit();

#4

umm, ok, so how exactly do I fix it?

What/how should I change the code?


#5

Comment out the echo so the first thing sent is the header()


#6

Actually where you point out the error above is not necessariyly the error. Because it’s in an IF statement. It get’s echoed but the script ends. Thus the script doesn’t get to the header function.

I think more likely that one of the other INCLUDES might be echoing something first before the header() function


#7

umm, ok…

It is supposed to write that info (product, price, productID, etc) to a php document called “carrinho.php” which is the actual shopping cart

By pure experimentation, I also noticed that if I take out the session_start(); It no longer gives me the same error but instead opens a page saying only:

1 erro aquiInsert into carro (produto, preco, Idencomenda,data) values ("AUTO-CURA -","15.47","","2004-06-25")

However it does not provide a Product ID

Any ideas? all help is extremely appreciated

Thanx in advance,


#8

You are probably missing the informatoin because you removed the SESSION_START() function.

That information is probably stored in the session variable.
That is actually where I think the problem lies. The SESSION info is sent in the headers. Thus you have an INCLUDE at the top of the page you presented. Ensure NO HTML is being echoed/sent in that include. I would likely move the SESSION_START to the top of the page to eliminate that as an issue.


#9

firstly, I want to thank everyone for their continued help in this issue, it is greatly appreciated, seen that I understand very little when it comes to php, you have all helped immensely.

After following the advice, and moving session_start(); to the top of the page it solves the error, however gives me the following:

Insert into carro (produto, preco, Idencomenda,data) values ("AUTO-CURA -","15.47","ba051e0306c171b5e0a3259cfb3f33fd","2004-06-25")

before this (in portuguese therefore I did not include it, it says " 1 error present" and gives me the above quoted text.

Since now the error is rather strange, I opened the “carrinho.php” file which is the shopping cart itself in my browser and it gives me the following error:

1 error present Error in sql 1

Since it said “sql” i opened a file called “mysql.php” in the shopping cart folders in my browser and it gave me the following errors:

1 error present Warning: Supplied argument is not a valid MySQL result resource in /home/sites/site57/web/index/shop/carrinho/asp/mysql.php on line 46

Warning: Supplied argument is not a valid MySQL result resource in /home/sites/site57/web/index/shop/carrinho/asp/mysql.php on line 47

Warning: Supplied argument is not a valid MySQL result resource in /home/sites/site57/web/index/shop/carrinho/asp/mysql.php on line 49

Any ideas what might be the problem now that we have a specific problem?

All help greatly appreciated, thanx in advance[/quote]


#10

Ensure the SQL Query is properly formatted for the table and fields in that table. The error is stating it’s not getting a valid MySQL resource id. Typically that means an invalid query. If you echo the mysql_error after the query, you could see a more specific error message.