Hello,

I have a variable that I want to pass into my include file but not sure how to do this

say i have this in my index.php file

<?php
$new_id = $row['id'];
include('main.php')
?>

How would my $new_id get passed into my main.php file?

I'm trying to insert information into a database that is in my main.php

mysql_query('INSERT INTO comments (id) VALUES ($new_id)')

but my $new_id in main.php can not recognize the variable.

Recommended Answers

All 10 Replies

are you trying to execute the sql statement from WITHIN a function in main.php? If so, in your function you need to declare $new_id:

function(){
global $new_id;

...
mysql_query('INSERT INTO comments (id) VALUES ($new_id)');
}

I tried global $new_id but it doesn't give me anything that I wanted.

Let me try to explain better so you can understand my problem.


In my index.php, I have a while loop that grabs the id from mysql database. Then I want it to store into $new_id. Then in my include file, I want my $new_id to be stored in another table that I set up.

Right now the id in my $new_id from index.php does not get passed on into the include file.

Why dont you write mysql query in index.php itself.
Or post your code, lets see what u r trying with code.

I have a while loop that grabs the id from mysql database

If that variable is within a function, then at the beginning of that function declare it global. If the problem persists, post your code.

I don't have my code with me on this computer, but my code is pretty much what I have up there.

I don't have a function in my main.php

my global doesn't work because in my index.php I have set up a while loop for my sql database

$row = ['id']

will have 1, 2, 3, etc...

and so when I set

$new_id = $row = ['id']

my $new_id will have the same thing. I did echo it out in my index.php before my include file so I know it works.

But when I put $new_id in my main.php (which I'm trying to include in my index.php). It won't output the variable when I try echo. I did global, and I'm not getting 1,2,3, etc.

I need to pass this variable between files. If that doesn't make sense. I'll post up my code when I get a chance.

I'm not using function in my mysql insert in main.php.

Thanks for the help guys!

the problem is that what you are describing makes sense and should work. Thus, the other thing to take into to account is your ACTUAL coding? Did you mistype something? That is something we can only determine by looking at your code.

As an example, what you posted $new_id = $row = ['id'] does NOT look right. My guess would be that you meant $new_id = $row['id'] , and if that is the case, then do you also have the wrong syntax in your actual code?

If in index.php you are getting all variables then something must be in main.php.
What is the coding in main.php?
If u dnt have code then post any rough steps.

Thanks for responding guys

But I THINK that I got it to work. Not sure, because I'm getting another error now

I'm getting an error saying "trying to get property of non-object... on line 42
INSERT INTO comments (products_comments,user,products_id,userip,date_created) VALUES('why','','','127.0.0.1','1288681162')"

if(checkValues($_REQUEST['value']))
	{
		$userip = $_SERVER['REMOTE_ADDR'];
		echo "INSERT INTO comments (products_comments,user,products_id,userip,date_created) VALUES('".checkValues($_REQUEST['value'])."','$session->username','".$products_id."','".$userip."','".strtotime(date("Y-m-d H:i:s"))."')";
		
		mysql_query("INSERT INTO comments (products_comments,user,products_id,userip,date_created) VALUES('".checkValues($_REQUEST['value'])."','$session->username','".$products_id."','".$userip."','".strtotime(date("Y-m-d H:i:s"))."')");
	
		$result = mysql_query("SELECT *,
		UNIX_TIMESTAMP() - date_created AS TimeSpent FROM comments order by id desc limit 1");

It looks like its not inserting $session->username and $products_id

Member Avatar for diafol

Hmm OOP and nOOb - tricky. SOrry man, but your queries are really messy. Try the SET method as opposed to VALUE, perhaps that will give you a better result. You can echo out the query and post it into the SQL box in phpmysql - that usually gives more info as to what the problem is.

trying to get property of non-object.

Most likely it is referring to $session->username . Did you remember to create a $session object first? You would need to post your session class definition to see if you are using getter methods (instead of public variables) or static variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.