harlequeen Posted July 22, 2011 Share Posted July 22, 2011 Hi I have a series of 4 pages which need to have the variable $weekNo (which is set in page 1 by the user - me) passed to them. $weekNo is the name of a field which is being generated in the early pages of this series. I'm doing this through hidden fields in various forms that are required. When I get to the 3rd page I can't seem to pass that value on. I have echoed it out on page 3, then there is the result of a mysql query which is in while loop, bringing me back varying number of rows, but typically 13 or 14. I want to include a hidden field here so that when I submit the values from the form, it will be sent to page 4 where I want the UPDATE statement to include the $weekNo variable so that the statement updates the correct column. I currently have the hidden field within the while loop which I'm sure is incorrect as it's giving me this error. Fatal error: [] operator not supported for strings in /usr/local/psa/home/vhosts/llanelli-ladies-darts.co.uk/httpdocs/checkresults4.php on line 15 Can someone explain to me how I can do this please and if my update statement would be correct. Apologies if my code is long winded and/or strange as I'm learning. Here is my code from page 3 <?php //database connection include'myconnect.php'; $weekNo=$_GET['weekNo']; $divNo=$_GET['divNo']; echo"Hello $weekNo"; $sql=mysql_query("SELECT teams.teamName, teams.team_id, teams.totalPoints, teams.gamesWon, contacts.division FROM teams LEFT JOIN contacts ON teams.teamName = contacts.teamName WHERE contacts.division='$divNo' "); while(@$row=mysql_fetch_array($sql)) { //Form generated from database calling teams identified by Division number in query above. ?> <table width="30%" align="center" border="1"> <form name="inputpoints" method="GET" action="checkresults4.php"> <tr> <td width=40%> <input type="char" name='team_id[]'size="2" value="<? echo $row['team_id'];?>"></td><td> <input type="text" name='teamName[]' size="36" value="<? echo $row['teamName']; ?>"></td> <input type="hidden" name= 'totalPoints[]' size="3" value="<? echo $row['totalPoints'];?>"></td> <input type="hidden" name= 'gamesWon[]' size="2" value="<? echo $row['gamesWon'];?>"></td> <input type="hidden" name= 'gamesWon[]' size="2" value="<? echo $row['weekNo'];?>"></td> <td> <SELECT name='Points[]'> <OPTION value=> </OPTION> <OPTION value=1>1</OPTION> <OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION> <OPTION value=4>4</OPTION> <OPTION value=5>5</OPTION> <OPTION value=6>6</OPTION> <OPTION value=7>7</OPTION> <OPTION value=0>0</OPTION><?php echo $row['Points'];?>"> </select> </td> <?php } ?> </table> <input type="text" size="3" name="weekNo" value="$weekNo"/> <center><input type="submit" value="submit" action="submit"></center> </form> <?php and here is the code for the action page (page 4) //Receiving information from form in teaminputtest.php $team_id[]=$_GET['team_id']; $teamName[]=$_GET['teamName']; $Points[]=$_GET['Points']; $totalPoints[]=$_GET['totalPoints']; $gamesWon[]=$_GET['gamesWon']; $played[]=$_GET['played']; $weekNo[]=$_GET['weekNo']; echo"$weekNo";?><br><? //Looping through each item in the array to get the info foreach ($_GET['team_id'] as $row=>$name) { $team_id = $name; $teamName = $_GET['teamName'][$row]; $Points = $_GET['Points'][$row]; $totalPoints = $_GET['totalPoints'] [$row]; $gamesWon = $_GET['gamesWon'] [$row]; $played = $_GET['played'] [$row]; $weekNo = $_GET['weekNo']; //printing each item to the screen echo "$name "; echo "$teamName "; echo "$Points<br>"; echo "$totalPoints<br>"; }// end for each loop // Count how many teams there are in the division $size=count($_GET['team_id']); echo "$size <br>"; //Set the counter to zero, then loop through while the counter is less than the count figure $i=0; while ($i < $size) { //Call the information from the GET strings passed from previous page. $team_id=$_GET['team_id'] [$i]; $Points=$_GET['Points'] [$i]; $totalPoints=$_GET['totalPoints'] [$i]; $gamesWon=$_GET['gamesWon'] [$i]; $played = $_GET['played'] [$row]; $weekNo = $_GET['weekNo']; //If the team wins add 1 point for winning and count 1 game won. If there are points given, even if its '0' count 1 game played If ($Points >3) { $weekNo=$Points+1; $gamesWon=$gamesWon+1; } else { $Points=$Points; $gamesWon=$gamesWon; } // checking if points are equal to "" if ($Points != NULL) { echo "Points"; $played=$played+1; } else { $played=$played; } //Add up stored total points and this week numbers points $totalPoints=$totalPoints + $Points; echo "Total Points:$totalPoints <br>"; $weekNo = "weekNo"; //Update the database $query="UPDATE teams SET Points = '$Points', $weekNo ='$Points', totalPoints = '$totalPoints', gamesWon = '$gamesWon', played = '$played' WHERE team_id = '$team_id' "; //If there is an erro, let me know mysql_query($query) or die ("Error in query: $query"); //echo to screen the team ID Number //echo "$team_id "; //echo to screen the Points allocated ++$i; }//end while loop print "$Points <br/><br/><em>Updated!</em><br/><br/>"; mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/ Share on other sites More sharing options...
Maq Posted July 22, 2011 Share Posted July 22, 2011 Why don't you use sessions? Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246299 Share on other sites More sharing options...
Psycho Posted July 22, 2011 Share Posted July 22, 2011 I agree that session variables would be better. But, I think the problem is here on page 4: $weekNo[]=$_GET['weekNo']; You are assigning the passed value into an array. But, then you try to reference it as a string echo"$weekNo"; Unless there is some reason to be using an array, you should be using $weekNo=$_GET['weekNo']; Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246313 Share on other sites More sharing options...
harlequeen Posted July 22, 2011 Author Share Posted July 22, 2011 Thanks for the replies. I am not using a session variable because I need to set the weekNo from an input form. I changed the code you suggested, as it shouldn't be an array, but I still get the same issue. However, I seem to be a step closer to what I want. I changed my code to: <input type="hidden" size="5" name="weekNo" value=<?php echo $_GET["weekNo"]; ?><td> and that has allowed me to echo out the variable. I put it after the while loop finishes and it is echoing out properly. My update statement is causing me issues now. As the previous pages have created a field called wk_22 in my table, I want to be able to assign that same value to part of the update statement as shown below. $weekNo needs to be a field so that I can post $Points to it. $query="UPDATE teams SET Points = '$Points', $weekNo ='$Points', totalPoints = '$totalPoints', gamesWon = '$gamesWon', played = '$played' WHERE team_id = '$team_id' "; I have no idea how to do this, can someone tell me how please. Hope someone can help. Harlequeen Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246372 Share on other sites More sharing options...
darkfreaks Posted July 22, 2011 Share Posted July 22, 2011 you can still use a session to pass a GET OR POST value //call session session_start(); //put $GET contents into a session $_SESSION['weekkno'] = $_GET['weekno']; creating a MYSQL field alter table teams add column Weekno (field type here IE varchar|Int) that should work. Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246380 Share on other sites More sharing options...
xyph Posted July 22, 2011 Share Posted July 22, 2011 Don't use sessions to pass data that should be passed through GET or POST. If the user visits a page that sets similar variables in another tab, they can mess with data being used in the original tab. Don't use sessions as a way to make variables 'global' between pages. If you absolutely must, you have to use a second token, passed along via GET or POST (in EVERY link or form you want the data to persist through), that references that specific token. That way, if they visit a page that may alter the persistent data in another tab and there's no token passed, a new token is generated and the original persistent data won't be overwritten. Even this isn't foolproof, as a user may follow a link with the token to a new page, which may or may not alter the persistent data. Any links followed in the original tab may not have the expected persistent data you wanted. Your smartest bet is to pass the variables along in hidden post fields, or through the query string. If you MUST use sessions, use a secondary token, and be aware that the data could change if the user manages to duplicate that token in multiple tabs, intentional or not. Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246386 Share on other sites More sharing options...
darkfreaks Posted July 22, 2011 Share Posted July 22, 2011 also i failed to read that you have the field already created my bad. you should look into using the PHP OCI function if you are going to use a PHP variable as a MYSQL column. http://www.php-es.com/function.oci-bind-by-name.html Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246389 Share on other sites More sharing options...
xyph Posted July 22, 2011 Share Posted July 22, 2011 You're suggesting they use Oracle functions for a MySQL database? Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246391 Share on other sites More sharing options...
darkfreaks Posted July 22, 2011 Share Posted July 22, 2011 but yeah after really thinking about it the safest way would be to pass the variables through hidden input fields. that will work. sessions are too risky unless you secure them with some sort of unique token. Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246394 Share on other sites More sharing options...
Psycho Posted July 23, 2011 Share Posted July 23, 2011 but yeah after really thinking about it the safest way would be to pass the variables through hidden input fields. that will work. sessions are too risky unless you secure them with some sort of unique token. Passing the value in clear text on each page transition is less risky than storing the value in a session value on the server? It would then only pass in clear-text one time (when sent from the client to the server) as opposed to many times back and forth. Don't use sessions to pass data that should be passed through GET or POST. If the user visits a page that sets similar variables in another tab, they can mess with data being used in the original tab. Don't use sessions as a way to make variables 'global' between pages. If you absolutely must, you have to use a second token, passed along via GET or POST (in EVERY link or form you want the data to persist through), that references that specific token. That way, if they visit a page that may alter the persistent data in another tab and there's no token passed, a new token is generated and the original persistent data won't be overwritten. Even this isn't foolproof, as a user may follow a link with the token to a new page, which may or may not alter the persistent data. Any links followed in the original tab may not have the expected persistent data you wanted. Your smartest bet is to pass the variables along in hidden post fields, or through the query string. If you MUST use sessions, use a secondary token, and be aware that the data could change if the user manages to duplicate that token in multiple tabs, intentional or not. "Don't use sessions as a way to make variables 'global' between pages.". That is the whole point of session values. You can set them and have them persist for the entire session. As to the "issues" you are referring to where you want to set the similar values for different pages - you just set different session values. It's not complicated at all. For instance, I have built applications where there are several different screens where data can be paginated and I give the user the option to change the number of records to be displayed per page. Some settings should apply to "similar" records while others should not. So, I simply create a different session array for each: $_SESSION['client']['records_per_page'] would be used for any pages displaying clients, $_SESSION['staff']['records_per_page'] would be used for any pages displaying staff, etc. Quote Link to comment https://forums.phpfreaks.com/topic/242654-passing-variable/#findComment-1246403 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.