2012-08-23 52 views
1

我有一个产品页面,其中8个产品图像位于正在由存储在MySQL数据库中的图像填充的列表中。这些图像都具有关联的ID,其中价格,产品名称和说明也与相同的ID相关联。从URL获取MySQL ID以显示页面上的信息

我想要做的背后的想法是;当用户点击8个产品图像中的一个时,它们将被重定向到“结帐”页面,该页面将显示相同的图像,以及也存储在数据库中该ID下的所有信息。

截至目前,我的结帐页面URL包含图像的ID(url.com/checkout.php?id=1),我希望能找到一种方法来获取所有存储在该信息下的信息要在页面上调用的URL中显示ID。

这里是我的PHP代码,在产品页面上的列表显示图像:

// Grab the data from our template table 
      $sql = "select * from templates"; 
      $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 
      while ($row = mysql_fetch_assoc($result)) 
      { 
       echo "<li>"; 
       echo "<a class=\"caption\" href=\"purchase.php?id=\"" . $row['id'] . ">"; 
       // Note that we are building our src string using the ID from the database 
       echo "<img src=\"http://URL-REMOVED.com/file_display.php?id=" . $row['id'] . "\" />"; 
       echo "<span>"; 
       echo "<big>" . $row['name'] . "</big>"; 
       echo $row['description']; 
       echo "</span>"; 
       echo "</a>"; 
       echo "</li>"; 

      } 

这是应该收集点击的产品信息的PHP代码(但没有) :

if (isset($_GET['id'])) 
     $id=$_GET['id']; 
    else 
     $id=1; 

    if (isset($_GET['action'])) 
     $action=$_GET['action']; 
    else 
     $action="empty"; 


    switch($action){ 

     case "add": 

     if($_SESSION['cart'][$id]) 
      $_SESSION['cart'][$id]++; 
     else 
      $_SESSION['cart'][$id]=1; 

     break;  

     case "remove": 

     if($_SESSION['cart'][$id]) 
     { 
      $_SESSION['cart'][$id]--; 

      if($_SESSION['cart'][$id]==0) 
    unset($_SESSION['cart'][$id]); 
      } 

      break;  

      case "empty": 
     unset($_SESSION['cart']); 

     break;  
     } 

//Display Cart 

     if(isset($_SESSION['cart'])) { 

      $total=0; 
      foreach($_SESSION['cart'] as $id => $x) { 
      $result=mysql_query("select image,name,price,description from templates WHERE id=$id"); 
      $myrow=mysql_fetch_array($result); 
      $image=$myrow['image']; 
      $name=$myrow['name']; 
      $price=$myrow['price']; 
      $description=$myrow['description']; 

     } 
    } 

这里是实际的HTML/PHP,其中的信息是应该显示:

<a class="caption" href="checkout.php"> 
      <img src="http://URL-REMOVED.com/file_display.php?id=<?php $myrow['id'] ?>"/> 
      <span> 
       <big> 
        <strong><?php $myrow['name'] ?></strong> 
       </big> 

       <div class="price"><?php $myrow['price'] ?></div> 
      </span> 
     </a> 
    </div> 
    <div id="info_form_container"> 
    <div class="product_info"> 
     <div class="control-group"> 
      <strong>Template Name:</strong> 
      <?php $myrow['name'] ?> 
     </div> 

     <div class="control-group"> 
      <strong>Template Description:</strong> 
      <?php $myrow['description'] ?> 
     </div> 

     <div class="control-group"> 
      <strong>Template Price: </strong> 
      <?php $myrow['price'] ?> 
     </div> 
    </div> 

我想我不确定这是否是最好的方法?但我绝对想要将图像存储在数据库中,并且我一定想用ID号给他们打电话...

我该如何做到这一点?我的代码在哪里错了?

+0

检查表,表中可能没有任何东西 –

+2

新的应用程序**不应该使用**'mysql_query',而应该使用新的'mysqli'或PDO接口之一。 [SQL注入漏洞](http://bobby-tables.com/)会导致非常严重的问题。在你创造一个巨大的混乱之前,你最好切换到其中的一个,你必须清理。 – tadman

+0

好的,谢谢你指出。但是这并不能回答我的问题......而且我的数据库中的表都已满。 –

回答

0

编辑:我不认为我第一次理解你的问题。尝试使用它来生成您的产品列表,更新连接信息。如果一切正常,请使用以下方法来净化你的变量和其他存储连接信息

<?php 
    $mysqli_connection = new mysqli("localhost", "username", "password", "database"); 
    $sql = "SELECT * FROM templates"; 
    $result = $mysqli_connection->query($sql); 
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) { 

     $id = $row['id']; 
     $name = $row['name']; 
     $description = $row['description']; 

     echo '<li>'; 
     echo '<a href="purchase.php?id='.$id.'" class="caption">'; 
     echo '<img src="http://URL-REMOVED.com/file_display.php?id='.$id.'" />'; 
     echo '<span>'; 
     echo '<big>'.$name.'</big>'; 
     echo $description; 
     echo '</span>'; 
     echo '</a>'; 
     echo '</li>'; 
    } 
?> 

OG答:

我觉得你在做什么就好了。据该方法我用(虽然我看起来有点整洁)。运行你的PHP函数,使用你得到的ID查询你的MySQL数据库,并开始转储你的信息到你的网站。为了帮助提高可读性并减少拼写混淆,可以将$myrow['whatever']结果指定为变量来回显,但这对我来说比任何内容都更加美观。

来修复你的MySQL的东西,并使用mysqli的,请尝试以下操作:

$sql = "SELECT * FROM templates"; 
$result = $mysqli_connection->query($sql); 
while ($row = $result->fetch_array(MYSQLI_ASSOC)) { 
    echo "<li>"; 
    echo "<a class=\"caption\" href=\"purchase.php?id=\"" . $row['id'] . ">"; 
    // Note that we are building our src string using the ID from the database 
    echo "<img src=\"http://URL-REMOVED.com/file_display.php?id=" . $row['id'] . "\" />"; 
    echo "<span>"; 
    echo "<big>" . $row['name'] . "</big>"; 
    echo $row['description']; 
    echo "</span>"; 
    echo "</a>"; 
    echo "</li>"; 
} 

并尝试使用以净化你的信息:

$my_stuff = mysqli_connection->real_escape_string($row['that_stuff']); 

而且,你知道你可以使用单引号('')围绕你的回声语句,如果你想的话,对吧?可以更容易地转义所有的双引号。

因此,在充分,这可能是一个粗略的例子我会做什么,但我想它分解成各种功能,也许创造一些全局变量(如连接本身):

<?php 


    $mysqli_connection = new mysqli("localhost", "username", "password", "database"); 
    $sql = "SELECT * FROM templates WHERE id=$id"; 
    $result = $mysqli_connection->query($sql); 
    // I'm assuming there is only one entry, so no while loop for me 
    $row = $result->fetch_array(MYSQLI_ASSOC); 

    $your_title = $mysqli_connection->real_escape_string($row['title']); 
    $path_to_image = $mysqli_connection->real_escape_string($row['image']); 
    $description = $mysqli_connection->real_escape_string($row['description']); 
    $price = $mysqli_connection->real_escape_string($row['price']); 

?> 

<html> 
    <head></head> 
    <body> 
     <h3><?php echo $your_title; ?></h3> 
     <img src="<?php echo $path_to_image; ?>" /> 
     <ul> 
      <li>Description: <?php echo $description; ?></li> 
      <li>Price: <?php echo $price; ?></li> 
      <!-- etc..--> 
+0

谢谢,但是,这不是我遇到的问题。我无法将所选ID的信息打到下一页... –

+0

@TyBailey我编辑了我的答案。我认为你有问题,因为你需要在HTML呈现时回显值 – burmat

+0

我试过你的方法,它似乎也不工作。我很确定问题是ID没有被带到URL。它只是在它的末尾添加了?id =没有ID#... –