Jump to content

a href problems


acmumph

Recommended Posts

I'm using the code from Chapter 12 in Beginning PHP4 to display my data. For some reason, the hyperlinks that are suppose to sort columns do not work neither does the "View Record" function. The "Next" button also fails to load the next set of data.  I also set register_global=on in the php.ini. Below is the code:

<?php
include "./common_db.inc";
function list_records() {
   global $default_dbname, $user_tablename;
   global $default_sort_order, $default_order_by, $records_per_page;
   global $sort_order, $order_by, $cur_page;
   global $PHP_SELF;
   
   $link_id = db_connect($default_dbname);
   if(!$link_id) error_message(sql_error());

   $query = "SELECT count(*) FROM $user_tablename";

   $result = mysql_query($query);
   if(!$result) error_message(sql_error());
      
   $query_data = mysql_fetch_row($result);
   $total_num_user = $query_data[0];
   if(!$total_num_user) error_message('No User Found!');
   $page_num = $cur_page + 1;

   $total_num_page = $last_page_num 
                   = ceil($total_num_user/$records_per_page);
   
   html_header();
   
   echo "<CENTER><H3>$total_num_user records found. Displaying the page
                     $page_num out of $last_page_num.</H3></CENTER>\n";
   
   if(empty($order_by)) {
      $order_by_str = "ORDER BY $default_order_by";
      $order_by = $default_order_by;
   }
   else $order_by_str = "ORDER BY $order_by";
   if(empty($sort_order)) {
      $sort_order_str = $org_sort_order = $default_sort_order;
      $sort_order = 'DESC';
   }
   else {
      $sort_order_str = $org_sort_order = $sort_order;
      if($sort_order == 'DESC') $sort_order = 'ASC';
      else $sort_order = 'DESC';
   }

   if(empty($cur_page)) {
      $cur_page = 0;
   }
     $limit_str = "LIMIT ". $cur_page * $records_per_page . 
                                     ", $records_per_page";
   $query = "SELECT project, route, config FROM $user_tablename
                                  $order_by_str $sort_order_str $limit_str";
   
   $result = mysql_query($query);   
   if(!$result) error_message(sql_error());
?>

<DIV ALIGN="CENTER">
<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">
   <TR>
      <TH WIDTH="25%" NOWRAP>
         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=route"; ?>">
         Route
         </A>
      </TH>
      <TH WIDTH="25%" NOWRAP>
         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=project"; ?>">
         Project
         </A>
      </TH>
      <TH WIDTH="25%" NOWRAP>
         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=config"; ?>">
            Config
         </A>
      </TH>
      <TH WIDTH="25%" NOWRAP>Action</TH>
   </TR>
<?php

   while($query_data = mysql_fetch_array($result)) {
      $route = $query_data["route"];
      $project= $query_data["project"];
      $config = $query_data["config"];
      echo "<TR>\n";
      echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$route</TD>\n";
      echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$project</TD>\n";
      echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$config</TD>\n";
      echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">
            <A HREF=\"javascript:open_window('$PHP_SELF?action=view_record&
		project=$project');\">View Record</A></TD>\n";
      echo "</TR>\n";
   }
?>
</TABLE>
</DIV>
<?php      
   echo "<BR>\n";
   echo "<STRONG><CENTER>";
   if($page_num > 1) {
      $prev_page = $cur_page - 1;

      echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&
  order_by=$order_by&cur_page=0\">[Top]</A>";

      echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&
  order_by=$order_by&cur_page=$prev_page\">[Prev]</A> ";
   }
   if($page_num <  $total_num_page) {
      $next_page = $cur_page + 1;
      $last_page = $total_num_page - 1;

      echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&
  order_by=$order_by&cur_page=$next_page\">[Next]</A> ";

      echo "<A HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&
  order_by=$order_by&cur_page=$last_page\">[bottom]</A>";
   }

   echo "</STRONG></CENTER>"; 
   html_footer();
}

function view_record() {
   global $default_dbname, $user_tablename;
   global $project;
   global $PHP_SELF;
   
   if(empty($project)) error_message('Empty User ID!');
   
   $link_id = db_connect($default_dbname);
   
   if(!$link_id) error_message(sql_error());

   $query = "SELECT route, project, config, 
                    opfieldc, opfieldrd, mod FROM $user_tablename
                    WHERE project = '$project'";
   $result = mysql_query($query);
   
   if(!$result) error_message(sql_error());
      
   $query_data = mysql_fetch_array($result);
   $route = $query_data["route"];
   $project = $query_data["project"];
   $config = $query_data["config"];
   $mod = $query_data["mod"];
   

   html_header();
   echo "<CENTER><H3>
         Record for User No.$route - $project($config)
         </H3></CENTER>";

?>


   </TR>
</TABLE>
</DIV>
<?php

   }
   
   html_footer();   

switch($action) {
   case "view_record";
      view_record();
   break;
   default: 
      list_records();
   break;
}
?>

 

Appreciate any insight you can offer

Link to comment
Share on other sites

I also set register_global=on

 

^^^ Don't do that. Turn them back off.

 

Register_globals was the worst security hole that was deliberately added to any programming language.

 

Any php coding that you learn that is dependent on register_globals will leave you 8 years behind in php code development.

 

You should be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that all the errors that your code is producing will be reported and displayed. This will help you find the variables in it that are dependent on register_globals.

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.