Jump to content

[SOLVED] Problem using variables


Lakisman

Recommended Posts

I save a value from a query to a variable inside an if statment. But then i want to use that variable to other if statements but it doesnt work

 

here is the code

 

 

if(isset($_POST['Submit']))
{
   
$temp = $_POST['temp']; 

$query = "SELECT id  FROM htmlCode WHERE tempName= '$temp' ";
$result = mysql_query($query) or die('Error : ' . mysql_error()); 
$row = mysql_fetch_array($result, MYSQL_ASSOC); 

$tempid = $row['id'];
}

 

Now i want to use tempid outsite the if statement like this

 

if(isset($_POST['save2']))
{
   
   echo $tempid;

   $query1 = "UPDATE pageTitle SET titleText = 'hello'". "WHERE id = '$tempid'";
    mysql_query($query1) or die('Error ,query failed');
  
}

 

 

I lose the value at the second if statement.

 

How can i keep that value and use it everywhere in my page?

 

thank you

Link to comment
https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/
Share on other sites

Are both these if() statements on the same page? And do they both equate to true?

 

Yes. I first push the submit button and the first if is executed and then i push the save button of the other form so the second if executed. I need to use the result of the first query to the second if in the second query but i cant. why?


if(isset($_POST['Submit']))
{
   
$temp = $_POST['temp']; 

$query = "SELECT id  FROM htmlCode WHERE tempName= '$temp' ";
$result = mysql_query($query) or die('Error : ' . mysql_error()); 
$row = mysql_fetch_array($result, MYSQL_ASSOC); 

$tempid = $row['id'];
-------------------------------------------------
echo "The set value is ".$tempid;
-------------------------------------------------
}

Now i want to use tempid outsite the if statement like this


if(isset($_POST['save2']))
{
-------------------------------------------------
echo "The set value was ".$tempid;
-------------------------------------------------
  
   echo $tempid;
-------------------------------------------------
echo "Now the value is ".$tempid;
-------------------------------------------------

   $query1 = "UPDATE pageTitle SET titleText = 'hello'". "WHERE id = '$tempid'";
    mysql_query($query1) or die('Error ,query failed');
  
}


Variable values are not preserved between each run of you script unless you do something to store them -- use a hidden field in a form, store them in a database or a flat file or use a session variable.

 

I would use a session variable.

 

At the beginning of your script put the line

<?php
session_start();
?>

 

In your first "if" statement, put the line:

<?php
$_SESSION['tempid'] = $tempid;
?>

 

In your second "if" statement, put the line:

<?php
$tempid = $_SESSION['tempid'];
?>

 

Ken

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.