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
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?

Link to comment
Share on other sites


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


Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.