473,473 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

My $_POST variable works only for the first page.

3 New Member
I have written a form in with radio buttons the name is set to orderby and the value is set to KundeVorName and the next value is KundeNachName and it goes so on. I wanna modify my query according this values. It works well for the first page but at the second it looses its value and gives me an error message. When i hard code it like "Select * from Kunde ORDER BY KundeVorName" it works for all pages when i use my variable $orderby it works just for the first page. here is my code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. A:link {text-decoration: none}
  5. A:visited {text-decoration: none}
  6. A:active {text-decoration: none}
  7. A:hover {text-decoration: underline overline; color: red;}
  8. table.second { margin-top:20px; margin-bottom:0px;}
  9. </style>
  10. <title>Implementing Paging with next and prev</title>
  11. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  12. </head>
  13.  
  14. <body bgcolor="#99CC99">
  15.  
  16. <table width="60%" align="center" bgcolor="#009966"><tr>
  17. <td><a href="insert.php">Veri Gir</a></td>
  18. <td><a href="query.php">Veri Görüntüle</a></td>
  19. <td><a href="search.php"> Veri Ara </a></td>
  20. <td><a href="update.php"> Veri Güncelle </a></td>
  21. <td><a href="logout.php"> Ç?k?? </a></td>
  22. </tr></table>
  23. <?php
  24. include 'connect.php';
  25. $orderby = $_POST['orderby'];
  26. $type = $_POST['type'];
  27.  
  28.     if (isset($orderby)) {
  29.         echo "orderby is set\n";
  30.     } else {
  31.         echo "orderby is not set\n";
  32.     }
  33.     if (isset($type)) {
  34.         echo "type is set\n";
  35.     } else {
  36.         echo "type is not set\n";
  37.     }
  38.  
  39. // how many rows to show per page
  40. $rowsPerPage = 5;
  41.  
  42. // by default we show first page
  43. $pageNum = 1;
  44.  
  45. // if $_GET['page'] defined, use it as page number
  46. if(isset($_GET['page']))
  47. {
  48.     $pageNum = $_GET['page'];
  49. }
  50.  
  51. // counting the offset
  52. $offset = ($pageNum - 1) * $rowsPerPage;
  53.  
  54.  
  55. $query = " SELECT * FROM Kunde ORDER BY $orderby" .
  56.          " LIMIT $offset, $rowsPerPage";
  57. $result = mysql_query($query) or die('Error, query failed');
  58.  
  59. // start of the query *******************************************************************************
  60. ?>
  61. <div><table class='second' border='1'><tr><td>ID</td><td>?sim Soyad</td><td>Firma</td><td>S?n?f</td><td>Ürünler</td><td>Adres</td><td>?ehir</td><td>Ülke</td><td>Türü</td><td>?? Telefonu</td><td>Fax</td><td>Cep</td><td>Mail</td><td>Web</td><td>Tarih</td></b></tr>
  62. <?php
  63. while($row = mysql_fetch_array($result))
  64. {
  65. ?>
  66. <tr><td><?php echo $row['KundeID']; ?> </td>
  67. <td> <?php echo $row['KundeVorName']."&nbsp;".$row['KundeNachName']; ?> </td>
  68. <td> <?php echo $row['KundeFirma']; ?> </td>
  69. <td> <?php echo $row['KundeKategorie']; ?> </td>
  70. <td> <?php echo $row['KundeProdukte']; ?> </td>
  71. <td> <?php echo $row['KundeAdresse']; ?> </td>
  72. <td> <?php echo $row['KundeOrt']; ?> </td>
  73. <td> <?php echo $row['KundeLand']; ?> </td>
  74. <td> <?php echo $row['KundeArt']; ?> </td>
  75. <td> <?php echo $row['KundeFestnetz']."&nbsp;"; ?> </td>
  76. <td> <?php echo $row['KundeFax']."&nbsp;"; ?> </td>
  77. <td> <?php echo $row['KundeMobile']."&nbsp;"; ?> </td>
  78. <td> <?php $mail = $row['KundeMail']; ?><a href="mailto:<?php echo "$mail"; ?>"><?php echo $row['KundeMail']."&nbsp;"; ?> </a></td>
  79. <td> <?php $web = $row['KundeWeb']; ?> <a href="http://<?php echo "$web"; ?>" Target="blank"> <?php echo $row['KundeWeb']."&nbsp;"; ?></a> </td>
  80. <td> <?php echo $row['KundeRegDatum']."&nbsp;"; ?> </td></tr>
  81. <?php } ?>
  82. </table>
  83. <?php
  84. // end of the query *******************************************************************************
  85. // how many rows we have in database
  86. $query   = "SELECT COUNT(KundeID) AS numrows FROM Kunde ";
  87. $result  = mysql_query($query) or die('Error, query failed');
  88. $row     = mysql_fetch_array($result, MYSQL_ASSOC);
  89. $numrows = $row['numrows'];
  90.  
  91. // how many pages we have when using paging?
  92. $maxPage = ceil($numrows/$rowsPerPage);
  93.  
  94. // print the link to access each page
  95. $self = $_SERVER['PHP_SELF'];
  96. $nav = '';
  97. for($page = 1; $page <= $maxPage; $page++)
  98. {
  99.     if ($page == $pageNum)
  100.     {
  101.         $nav .= " $page ";   // no need to create a link to current page
  102.     }
  103.     else
  104.     {
  105.         $nav .= " <a href=\"$self?page=$page\">$page</a> ";
  106.     }        
  107. }
  108.  
  109. // creating previous and next link
  110. // plus the link to go straight to
  111. // the first and last page
  112.  
  113. if ($pageNum > 1)
  114. {
  115.     $page = $pageNum - 1;
  116.     $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
  117.  
  118.     $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  119. else
  120. {
  121.     $prev  = '&nbsp;'; // we're on page one, don't print previous link
  122.     $first = '&nbsp;'; // nor the first page link
  123. }
  124.  
  125. if ($pageNum < $maxPage)
  126. {
  127.     $page = $pageNum + 1;
  128.     $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  129.  
  130.     $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  131. else
  132. {
  133.     $next = '&nbsp;'; // we're on the last page, don't print next link
  134.     $last = '&nbsp;'; // nor the last page link
  135. }
  136.  
  137. // print the navigation link
  138. echo $first . $prev . $nav . $next . $last;
  139.  
  140. // and close the database connection
  141.  
  142. ?>
  143. </body>
  144. </html>
  145.  
[Please use CODE tags when posting source code. Thanks! --pbmods]
I get orderby is not set ,type is not set and query error messages.
How can I fix my problem with the variables?
May 30 '07 #1
3 2311
pbmods
5,821 Recognized Expert Expert
Heya, Mesut. Welcome to TSDN!

Um... that's a lot of code you just dumped there. And most of it really isn't relevant to your problem at all.

You might want to save your POST variables in the session. Take a look at this thread.
May 30 '07 #2
Mesut
3 New Member
Heya, Mesut. Welcome to TSDN!

Um... that's a lot of code you just dumped there. And most of it really isn't relevant to your problem at all.

You might want to save your POST variables in the session. Take a look at this thread.

I am really a newbie, i did not get the answer i have modified most of the scripts now i a m stucked up, i dont know how to modify
Expand|Select|Wrap|Line Numbers
  1. foreach($_POST as $k => $v)
  2. {
  3. $_SESSION[$k] = $v;
  4. }
  5.  
[Please use CODE tags when posting source code. Thanks! --pbmods]
variable...??
Anyway thats an amazing fast answer, you are great!!
May 30 '07 #3
pbmods
5,821 Recognized Expert Expert
I am really a newbie, i did not get the answer
We've all been there. No problem ^_^

First off, you need to make one change:
Expand|Select|Wrap|Line Numbers
  1. //  Add this line, or else nothing will work properly:
  2. session_start();
  3.  
  4. foreach($_POST as $k => $v)
  5. {
  6. $_SESSION[$k] = $v;
  7. }
  8.  
Alrightey. What this code does is run through $_POST and copy the values in $_POST to $_SESSION.

To be more verbose, for each element in $_POST, we assign the key (e.g., 'orderby') to $k and the value (e.g., 'KundeVorName') to $v. Then we set $_SESION[$k] to $v ($_SESSION['orderby'] = 'KundeVorName').

The end result is that anything that was in $_POST is now in $_SESSION, and $_SESSION (unlike $_POST) is persistent between page loads.

Put that script by where you assign the value of $orderby and then change your statement from
Expand|Select|Wrap|Line Numbers
  1. $orderby = $_POST['orderby'];
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. $orderby = $_SESSION['orderby'];
  2.  
May 30 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at...
15
by: zorro | last post by:
greetings... I'm wondering what more advanced coders would think ot this: $_POST = clean($_POST); and now I can use POST directly: $sql= "select * from T1 where myvar='$_POST' " ;
8
by: Simon Rigby | last post by:
Hello folks, Ive seen lots of recommendations to not try to do what Im trying to do but let me put it this way and hopefully someone can tell if this is a good idea or not (or an alternative). ...
12
by: Todd Michels | last post by:
Hi all, I am trying to send data from a form and insert it into a MSSQL DB. When I submit the data I get: Warning: mssql_query() : message: The name "Todd" is not permitted in this context....
9
by: shreedhan | last post by:
Hi I'm recenlty studying PHP myself and I have got a problem in passing variables to one php script from another i have following code as index.php <?php $name=HREEDN ?> <html> <head>
2
by: keeps21 | last post by:
I have a script that recieves an id number via the address bar when a link is clicked. ie . index.php?id=1 if the link was for the story whose ID is 1. My script checks if a user is logged in,...
4
by: mtuller | last post by:
I have a page that submits data to a database, and I want to make it so that if the page is refreshed, it doesn't submit the information again. I am trying to use unset the variables so that if the...
10
by: Mason Barge | last post by:
I have a standard POST form consisting of two types of input: text input and textarea. The form downloads current settings from a mysql database. The user can update the information by modifying...
0
by: Gilles Ganault | last post by:
Hello I use a POST variable named "status" to show different screens while keeping everything in the same script. The problem is that in the Print screen, I need to empty this variable so the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.