Lakisman Posted May 1, 2007 Share Posted May 1, 2007 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 Quote Link to comment Share on other sites More sharing options...
aebstract Posted May 1, 2007 Share Posted May 1, 2007 how can $_POST['save2'] and $_POST['Submit'] both be set? Wouldn't that be two different submit buttons = two different forms? Quote Link to comment Share on other sites More sharing options...
Lakisman Posted May 1, 2007 Author Share Posted May 1, 2007 how can $_POST['save2'] and $_POST['Submit'] both be set? Wouldn't that be two different submit buttons = two different forms? yes is two different forms. Quote Link to comment Share on other sites More sharing options...
Lakisman Posted May 2, 2007 Author Share Posted May 2, 2007 Someone tell me please. I believe for you is the solution is simple Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 Are both these if() statements on the same page? And do they both equate to true? Quote Link to comment Share on other sites More sharing options...
Lakisman Posted May 2, 2007 Author Share Posted May 2, 2007 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? Quote Link to comment Share on other sites More sharing options...
jitesh Posted May 2, 2007 Share Posted May 2, 2007 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'); } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 2, 2007 Share Posted May 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
Lakisman Posted May 2, 2007 Author Share Posted May 2, 2007 thank you very much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.