Return function doesnt work :(

Hello everybody, first of all sorry for my bad english :slight_smile:

So, i have a script page admin.php which includes functions.php

When i try to recall the value of a variable from functions.php it will not work.

[php]echo "

Adauga Categorie: "; if(isset($_POST['nume_categorie'])){ adauga_categorie($_POST['nume_categorie']); }[/php]

This will call the function wich i have in functions.php
[php]
function adauga_categorie($nume_categorie){
if(empty($nume_categorie)){
echo ā€œIntrodu numele categoriei pe care vrei sa o adaugiā€;
}else{
$introdu = mysql_query(ā€œINSERT INTO categorie VALUES(ā€™ā€™,ā€™$nume_categorieā€™)ā€);
if(@$introdu){
$mesaj = ā€œCategoria $nume_categorie a fost adaugata in baza de date.ā€;
return $mesaj;
}
}
}[/php]

This should return the variable $mesaj.

But when i try to get the variable $mesaj in admin.php it will not work, i get $mesaj empty after declaring it global.

[php]global $mesaj;
if(isset($mesaj)){
echo "


$mesaj
";
}[/php]

Simplified i need to pop out a message after the function was done in a message box.

This script will be used just by me, this is why I havent securised him yet.

Thanx

Did you actually INCLUDE the functions.php file in the other? You can create functions and put them into a file called functions.php. But, they do not exist unless you INCLUDE them in the live file. Like this:
include(ā€œfunctions.phpā€);

Note that this can be include(ā€œfolder/functions.phpā€); or some other folder if neededā€¦

Since we only saw a very small part of your code, I will assume this is the problemā€¦

ofcourse i did that, its all working but the global variable is not working
this is the admin.php just the first part, i have a long script i cant post it all here
[php]<?php
define(ā€˜accesā€™,ā€˜1ā€™);
include(ā€˜functii.phpā€™);
echo ā€œ

ā€;
global $mesaj;
if(isset($mesaj)){
echo "

$mesaj
";
}

echo ā€œ

ā€;
echo " "; if(isset($_POST['nume_categorie'])){ adauga_categorie($_POST['nume_categorie']); }[/php]

and this is the function.php (functii.php)

[php]<?php
if(!defined(ā€˜accesā€™)){
echo ā€œAccesul interzisā€;
}else{
require_once(ā€˜config.phpā€™);

function adauga_categorie($nume_categorie){
if(empty($nume_categorie)){
echo ā€œIntrodu numele categoriei pe care vrei sa o adaugiā€;
}else{
$introdu = mysql_query(ā€œINSERT INTO categorie VALUES(ā€™ā€™,ā€™$nume_categorieā€™)ā€);
if(@$introdu){
$mesaj = ā€œCategoria $nume_categorie a fost adaugata in baza de date.ā€;
return $mesaj;
}
}
}

function lista_categorii(){
$categorii = mysql_query(ā€œSELECT * FROM categorie ORDER BY id DESCā€);
if(mysql_num_rows($categorii )!= 0){
while($cat = mysql_fetch_array($categorii)){
$id = $cat[ā€˜idā€™];
$nume_cat = $cat[ā€˜nume_catā€™];
echo ā€œ\n$nume_cat\nā€;
}
}else{
echo ā€œ\nAdauga o categorie mai intai\nā€;
}
}
ā€¦
[/php]

and here is the apache error logā€¦ no errors but one with favicon.ico that is missing ā€¦

[sup][Wed Mar 21 00:36:13 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:39:17 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:39:35 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:40:03 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:40:20 2012] [error] [client 127.0.0.1] PHP Notice: Undefined variable: mesaj in C:\Program Files (x86)\EasyPHP-5.3.9\www\admin.php on line 6
[Wed Mar 21 00:40:20 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:40:25 2012] [error] [client 127.0.0.1] PHP Notice: Undefined variable: mesaj in C:\Program Files (x86)\EasyPHP-5.3.9\www\admin.php on line 6, referer: http://127.0.0.1/admin.php
[Wed Mar 21 00:40:25 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:41:05 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:41:27 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:41:31 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:41:56 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:43:02 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:43:05 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:45:11 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:46:51 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 00:53:21 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico
[Wed Mar 21 01:13:06 2012] [error] [client 127.0.0.1] File does not exist: C:/Program Files (x86)/EasyPHP-5.3.9/www/favicon.ico[/sup]

Anyone any ideas, has this maybe something to do with register_globals ?

Adauga Categorie:

to me it looks like you got a fishy sql statement

INSERT INTO `categorie` VALUES('','$nume_categorie'

I take the first field is the id?
try using

INSERT INTO `categorie` (nume_cat) VALUES('$nume_categorie'

yes the first field is the auto increment & primary id , it doesnt need a value.
My script is working fine the variable in function is working ive tried it.
Whats not working is to get the varible out from the function.

I think i will try it with sessions. I`m sick of it :((

  1. if this is the full script
    than $mesaj is already in the global scope, so there is no need to globalize it.
  2. you donā€™t show how $mesaj gets defined

if this is supposed to be an old register globals enabled script, than DONT DO THAT
and use $mesaj = isset($_GET['mesaj']) ? $_GET['mesaj'] : NULL;
which will get the url parameter mesaj into a variable otherwise set it as null (can use ===NULL or empty($mesaj) for testing)
or

 if(isset($_GET['mesaj'])) $mesaj=$_GET['mesaj'];

if you dont want mesaj to be set if the parameter isnt there (use isset($mesaj) for testing)

Whoaā€¦ You mentioned sessions! You mean you are trying to pass a variables across pages??? Canā€™t do that. You must pass variables by using files, session variables, posting with forms or arguments. NOT by variablesā€¦ Global variables mean global to functions and PHP code, not pagesā€¦ PHP is only server-side and need to pass it thereā€¦ So, use session variablesā€¦

variable $mesaj gets defined in the function after succesfully adding records to db.

[php]function adauga_categorie($nume_categorie){
if(empty($nume_categorie)){
echo ā€œIntrodu numele categoriei pe care vrei sa o adaugiā€;
}else{
$introdu = mysql_query(ā€œINSERT INTO categorie VALUES(ā€™ā€™,ā€™$nume_categorieā€™)ā€);
if(@$introdu){
$mesaj = ā€œCategoria $nume_categorie a fost adaugata in baza de date.ā€;
return $mesaj;

}
}
}
[/php]

i dont want to get the variable through url $_GET i already thought about it but its to easy :smiley:
i have done this before and has worked, ive just declared the variable global and then i could use it in the main script.

I should get the message just when the variable is set:

[php]
if(isset($mesaj)){
echo "


$mesaj
";
}[/php]

the variable is globalized just in the mainscript (admin.php which includes functii.php)

If i put echo in the function.php it wil work, so i know that my function are working, now i`m trying to get the message from the echo into a variable so i can use it in admin.php

[php]function adauga_categorie($nume_categorie){
if(empty($nume_categorie)){
echo ā€œIntrodu numele categoriei pe care vrei sa o adaugiā€;
}else{
$introdu = mysql_query(ā€œINSERT INTO categorie VALUES(ā€™ā€™,ā€™$nume_categorieā€™)ā€);
if(@$introdu){
echo ā€œCategoria $nume_categorie a fost adaugata in baza de date.ā€;
}
}
}
[/php]

Yes, i did that before ive got the variable from function.php to script.php i used just return and global and ive worked around with. now is not working anymore, strange thing :-??

function.php was included through include() in script.phpā€¦ like nowā€¦ functii.php is included in admin.php

now i`m trying to get the message from the echo into a variable so i can use it in admin.php

What does that mean? Perhaps itā€™s my English! Echo is used to ā€œPRINTā€ something to the page being displayed. It doesnā€™t move into a variableā€¦

Also, this code:
$introdu = mysql_query(ā€œINSERT INTO categorie VALUES(ā€™ā€™,ā€™$nume_categorieā€™)ā€);
should be:
$introdu = mysql_query(ā€œINSERT INTO categorie (field-nume_categorie) VALUES($nume_categorie)ā€);
Normally you INSERT with either table (field-name) VALUES (values) or
table VALUES (values for ALL fields)
My guess with what you shared is the quotes around the table namesā€¦

I think I see what yer saying now
let me give you a test script so you can understand whatā€™s happening

[code]<?php
function one()
{
global $mesaj;
$mesaj=ā€˜Hello Worldā€™;
}

one();
echo 'One: '. $mesaj.PHP_EOL; // This Works
unset($mesaj);
function two()
{
$mesaj=ā€˜Hello Worldā€™;
return $mesaj;
}
two();
echo 'Two: '. $mesaj.PHP_EOL; // Does not work
$mesaj=two();
echo 'Two: '. $mesaj.PHP_EOL; // This Works

?>
[/code]

You have to call the function before the isset

Thanx man it worked, the problem was, i called the function before if(isset())

Thank you all for your answers, i`m so glad now :X

Sponsor our Newsletter | Privacy Policy | Terms of Service