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 Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/ 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? Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/#findComment-242612 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. Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/#findComment-242635 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 Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/#findComment-243013 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? Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/#findComment-243015 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? Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/#findComment-243316 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'); } Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/#findComment-243320 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 Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/#findComment-243352 Share on other sites More sharing options...
Lakisman Posted May 2, 2007 Author Share Posted May 2, 2007 thank you very much Link to comment https://forums.phpfreaks.com/topic/49496-solved-problem-using-variables/#findComment-243394 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.