Return function doesnt work :(


#1

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


#2

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…


#3

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:

#4

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'

#5

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 :((


#6
  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)


#7

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…


#8

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]


#9

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


#10
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…


#11

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


#12

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