2015-04-24 43 views
1

下面我有一些代碼從一個名爲「tally_point」PHP if語句 - 資源ID發佈

什麼我想是檢索一個名爲列中的值表中檢索數據「tpt_id」

在另一個表中,有一個名爲「tally_point_type」的表,它也具有主鍵'tpt_id'。

我想要做的是從tally_point_type行中獲取'tpt_name'值來打印。目前,我可以使「訂單詳細信息」鏈接工作,但$ tpt_name值打印出一個資源ID#值。

我knwo我接近,但不能完全弄清楚如何得到這個工作。

<?php 

    $pointstype = $row['tpt_id']; 

    $type = '<td align="center">'; 

    if($pointstype > '0') { 

    $query = "SELECT tpt_name 
    FROM tally_point_type 
    WHERE'" . $row['tpt_id'] . "'=$pointstype"; 

    $tpt_name = mysql_query($query); 
    $type .='<strong>' . $tpt_name . '</strong></td></tr>'; 
    } 
    else { 
    $type .='<strong><a href="view-ind-order.php?id=' . $pointsitem . '">Order Details</a></strong></td></tr>'; 
    } 
    echo $type; 
    ?> 

全部代碼在這裏:

 <?php # index.php 

     require_once ('./includes/config.inc.php'); 

     $page_title = 'Title'; 
     include ('includes/header.html'); 

     if (!isset($_SESSION['admin_int_id'])) { 

      $url = 'http://' . $_SERVER['HTTP_HOST'] 
      . dirname($_SERVER['PHP_SELF']); 
      // Check for a trailing slash. 
      if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')) { 
       $url = substr ($url, 0, -1); // Chop off the slash. 
      } 
      $url .= '/login.php'; 

     ob_end_clean(); // Delete the buffer. 
     header("Location: $url"); 
     exit(); // Quit the script. 
     } 

     if ((isset($_GET['id'])) && (is_numeric($_GET['id']))) 
      { // Accessed through view_users.php 
      $id = $_GET['id']; 

     } elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))) 
      { // Form has been submitted. 
      $id = $_POST['id']; 
     } else { // No valid ID, kill the script. 
      echo '<h1 id="mainhead">Page Error</h1> 
      <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; 
      include ('./includes/header.html'); 
      exit(); 
     } 

     require_once ('/database.php'); // Connect to the db. 

     $display = 1000; 

     if (isset($_GET['np'])) { // Already been determined. 
      $num_pages = $_GET['np']; 
     } else { // Need to determine. 

      $query = "SELECT COUNT(*) FROM tally_point, users WHERE tally_point.users_id = users.users_id ORDER BY tally_points_entry_date DESC"; 
      $result = @mysql_query ($query); 
      $row = @mysql_fetch_array ($result, MYSQL_NUM); 
      $num_records = $row[0]; 

      if ($num_records > $display) { // More than 1 page. 
       $num_pages = ceil($num_records/$display); 
      } else { 
       $num_pages = 1; 
      } 

     } 

     if (isset($_GET['s'])) { 
      $start = $_GET['s']; 
     } else { 
      $start = 0; 
     } 

     $link1 = "{$_SERVER['PHP_SELF']}?sort=lna"; 
     $link2 = "{$_SERVER['PHP_SELF']}?sort=fna"; 
     $link3 = "{$_SERVER['PHP_SELF']}?sort=dra"; 

     if (isset($_GET['sort'])) { 

      switch ($_GET['sort']) { 
       case 'lna': 
        $order_by = 'tally_points_in ASC'; 
        $link1 = "{$_SERVER['PHP_SELF']}?sort=lnd"; 
        break; 
       case 'lnd': 
        $order_by = 'tally_points_in DESC'; 
        $link1 = "{$_SERVER['PHP_SELF']}?sort=lna"; 
        break; 
       case 'fna': 
        $order_by = 'total ASC'; 
        $link2 = "{$_SERVER['PHP_SELF']}?sort=fnd"; 
        break; 
       case 'fnd': 
        $order_by = 'total DESC'; 
        $link2 = "{$_SERVER['PHP_SELF']}?sort=fna"; 
        break; 
       case 'dra': 
        $order_by = 'tally_points_entry_date ASC'; 
        $link3 = "{$_SERVER['PHP_SELF']}?sort=drd"; 
        break; 
       case 'drd': 
        $order_by = 'tally_points_entry_date DESC'; 
        $link3 = "{$_SERVER['PHP_SELF']}?sort=dra"; 
        break;  
       default: 
        $order_by = 'tally_points_entry_date DESC'; 
        break; 
      } 

      $sort = $_GET['sort']; 

     } else { // Use the default sorting order. 
      $order_by = 'tally_points_entry_date DESC'; 
      $sort = 'dra'; 
     } 


     $query = "SELECT ta.tally_points_in, ta.order_id, ta.total, ta.tpt_id , DATE_FORMAT(ta.tally_points_entry_date, '%d-%m-%Y') AS dr, ta.users_id 
        FROM tally_point AS ta 
        WHERE ta.users_id=$id 
        ORDER BY 
     ".$order_by." LIMIT ".$start.", ".$display; 

     $result = @mysql_query ($query); // Run the query. 

     echo ' 
     <table width="500" cellspacing="1" cellpadding="7"> 
     <tr class="top"> 
      <td align="left"><b>Date</b></td> 
      <td align="center"><b>Credit</b></td> 
      <td align="center"><b>Debit</b></td> 
      <td align="center"><b>Description</b></td> 

     </tr> 
     '; 

     $bg = '#ffffff'; // Set the background color. 

     while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
      $pointsitem = $row['order_id']; 
      $pointstype = $row['tpt_id']; 

      $bg = ($bg=='#eaeced' ? '#ffffff' : '#eaeced'); // Switch the background color. 
      //$entries = floor($row['ltd_entry_amount']/200); 
      echo '<tr bgcolor="' . $bg . '">'; 
      echo '<td align="left">' . $row['dr'] . '</td>'; 
      echo '<td align="center"><strong>' . $row['tally_points_in'] . '</strong></td> '; 
      echo '<td align="center">' . $row['total'] . '</td>'; 


      $type = '<td align="center">'; 
      if($pointstype > '0') { 
      $query = "SELECT tpt_name 
      FROM tally_point_type 
      WHERE'" . $row['tpt_id'] . "'=$pointstype"; //THIS ALSO SEEMS WRONG column name should have backticks if you're trying to escape it and maybe value should be quoted? Also these values are the same, no? 
      $result = mysql_query($query); 
      $tpt_name = mysql_fetch_assoc($result); 
      $type .='<strong>' . $tpt_name['tpt_name'] . '</strong></td></tr>'; 
      } else { 
      $type .='<strong><a href="view-ind-order.php?id=' . $pointsitem . '">Order Details</a></strong></td></tr>'; 
      } 
      echo $type; 


      } 


     echo '</table>'; 

     mysql_free_result ($result); // Free up the resources. 

     mysql_close(); // Close the database connection. 

     if ($num_pages > 1) { 

      echo '<br /><p>'; 
      $current_page = ($start/$display) + 1; 

      if ($current_page != 1) { 
       echo '<a href="view_points_2.php?s=' . ($start - $display) . '&np=' . 
       $num_pages . '&sort=' . $sort .'">Previous</a> '; 
      } 

      for ($i = 1; $i <= $num_pages; $i++) { 
       if ($i != $current_page) { 
        echo '<a href="view_points_2.php?s=' . (($display * ($i - 1))) . 
        '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> '; 
       } else { 
        echo $i . ' '; 
       } 
      } 

      if ($current_page != $num_pages) { 
       echo '<a href="view_points_2.php?s=' . ($start + $display) . '&np=' . 
       $num_pages . '&sort=' . $sort .'">Next</a> '; 
      } 

      echo '</p>'; 

     } 
     include ('./includes/footer.html'); // Include the HTML footer. 
     ?> 
+1

$ result = mysql_query($ query); $ row = mysql_fetch_array($ result); $ tpt_name = $ row [0]; –

回答

0

你需要獲取結果:

$result = mysql_query($query); 
$row = mysql_fetch_assoc($result); 
$tpt_name = $row['tpt_name']; 
+0

我試過把它放在裏面,但沒有運氣,我已經把原代碼中的完整代碼作爲參考... – AdamMc

0

你需要獲取結果。

<?php 
$pointstype = $row['tpt_id']; 
$type = '<td align="center">'; 
if($pointstype > '0') { 
    $query = "SELECT tpt_name 
      FROM tally_point_type 
      WHERE'" . $row['tpt_id'] . "'=$pointstype"; //THIS ALSO SEEMS WRONG column name should have backticks if you're trying to escape it and maybe value should be quoted? Also these values are the same, no? 
    $result = mysql_query($query); 
    $tpt_name = mysql_fetch_assoc($result); 
    $type .='<strong>' . $tpt_name['tpt_name'] . '</strong></td></tr>'; 
} else { 
    $type .='<strong><a href="view-ind-order.php?id=' . $pointsitem . '">Order Details</a></strong></td></tr>'; 
} 
echo $type; 
?> 

另見附註在您的查詢的評論,可考慮改用司機的MySQLi或PDO,我不知道,你正在使用的數據是來自,但可能是開放的SQL注入。 How can I prevent SQL injection in PHP?

以下是供將來參考的手冊鏈接http://php.net/manual/en/function.mysql-query.php。見例#2。

+0

我試圖把它放入,但沒有運氣,我已經把原代碼作爲參考發佈...... – AdamMc

+0

更新後的代碼中沒有此代碼。你可以把它與更新的代碼併發回你現在得到的?你也可以脫掉錯誤抑制嗎?也不要將用戶輸入直接放入您的查詢中,並且如果您不確定該方法是否使用'$ _REQUEST'來傳輸數據。還請包括您收到的完整錯誤/通知消息。 – chris85

+0

嘿,我已經用你的代碼更新了完整的代碼。 我收到以下錯誤: 「在第283行的腳本'/my-database-/view-ind-points.php'中發生錯誤:未定義索引:order_id」 – AdamMc