0

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.

4
Contributors
10
Replies
13
Views
6 Years
Discussion Span
Last Post by hielo
0

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)');
}
0

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.

0

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

0

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.

0

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!

0

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?

0

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.

0

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

0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.