0

我在 div 弹出窗口后回显甚至检索值时遇到问题,任何人都可以帮助解决问题是 $r 在 div 弹出窗口之后没有显示。或者更确切地说,提取不会迭代并且它只显示第一条记录..提前谢谢

<?php
   $con = mysql_connect('****', 'root','****')    or die('Error connecting to MySQL server.');  
mysql_select_db("dreschema", $con) or die("cannot select DB"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link href="styles2.css" rel="stylesheet" type="text/css" />
<link href="styles.css" rel="stylesheet" type="text/css" />
<link href="styleshref.css" rel="stylesheet" type="text/css" />
<script>


</script>
</head>
<body>
<div id="container1">


 <div align="center"></div>
  <div id="mainContent1">




<?php 
$text = $_GET["searchtext"];
echo "Results displayed for   ". $text;
echo '</br>';


   $query4 = "SELECT * from products WHERE ProductName LIKE '%$text%'";
$data = mysql_query($query4, $con);
if (!mysql_query($query4, $con)){
print mysql_error();
exit;
}
     while ($row = mysql_fetch_array($data)) {
echo $row['ProductName'];

if($row['Stock']== 0 OR $row['Stock']== "")
{
echo "(SOLDOUT)";
echo '<input type="hidden" name="prod" value="' . $row['Image'] . ' " " id = "prod">';
echo '<a href="preorder.php?image=' . $row['Image'] . '"><img id = "imageid" src="' . $row['Image'] . '" alt="' . $row['Image'] . '"  width="80" height="80" style="margin-left:1.5em;margin-top:1.5em;"/></a>';
echo $row['Price'] . "PhP";
?>

   <div id="blanket" style="display:none;"></div>
    <div id="popUpDiv" style="display:none;">
    <a href="#" onclick="popup('popUpDiv')"><font size =" 20">x</font></a><br>


        <a href="#" onclick="popup('popUpDiv')" ><?php echo '<iframe src="orders2.php?image=' . $row['Image'] . '"style= position:absolute;width:500px;height:500px;"></iframe>';?> </a>

    </div>  
  <a href="#" onclick="popup('popUpDiv')">Click to Open CSS Pop Up</a><br>  

<?php
}
 else 
{
echo '<input type="hidden" name="prod" value="' . $row['Image'] . ' " " id = "prod">';
echo '<a href="orders2.php?image=' . $row['Image'] . '">';
echo '<img id = "imageid" src="' . $row['Image'] . '" alt="' . $row['Image'] . '"  width="80" height="80" style="margin-left:1.5em;margin-top:1.5em;"/></a>';
echo $row['Price'] . "PhP";
$r = $row['Image'];
  echo '<br>';

?>


    <div id="blanket" style="display:none;"></div>
    <div id="popUpDiv" style="display:none;">
<?php
echo $r;
?>
    <a href="#" onclick="popup('popUpDiv')"><font size =" 20">x</font></a><br>

        <a href="#" onclick="popup('popUpDiv')" ><?php echo '<iframe src="orders2.php?image=' . $row['Image'] . '"style= position:absolute;width:500px;height:500px;"></iframe>';?> </a>

    </div>  
  <a href="#" onclick="popup('popUpDiv')">Click to Open CSS Pop Up</a><br>  



   <?php
}


}
?>

    <!-- end #mainContent --></div>
<!-- end #container --></div>
</body> 
</html>
4

1 回答 1

0

This may or may not be an answer, but it wouldn't fit in the comments section so I wanted to expand it here. Looking over the code it's hard to tell if the problem is that (a) MySQL only returned one row; (b) The PHP loop is hitting a problem; or (c) everything is working fine but your HTML markup is so messed up that the browser is simply unable to render it as you think it should.

To eliminate the first issue, please add this right after your query:

$num_rows = mysql_num_rows($data);
if (!$num_rows){die('no rows');}
echo "<p>$num_rows rows returned</p>";

This way you can see how many rows are being returned. Also, don't do this: if (!mysql_query($query4, $con)){. Do this if(!$data){.

----- ASIDE ----

Better yet switch to PDO or mysqli, as your current setup WILL be hacked and your data will get messed up). *On the MySQL front, at the very least, you should set $text = mysql_real_escape_string($_GET["searchtext"]); to avoid the simplest SQL injection attack, and for g*d's sake don't use the root user for your site. Set up MySQL users with limited permissions and use them... also, to avoid an XSS injection you should also strip_tags($_GET["searchtext"]) when echoing the text to the screen.*

----- /ASIDE ----

If the problem isn't that the query is returning a single response, look at the source code. Are you sure you're not seeing multiple loops-worth of code? You have A LOT of markup problems. I've highlighted a few:

  1. echo '<input type="hidden" name="prod" value="' . $row['Image'] . ' " " id = "prod">'; - What are all those "s doing there? There's a floating " just sitting there, which can mess up how the browser interprets the tag. Also, you often have leading or trailing spaces in attribute values... get rid of those, especially in value attributes.
  2. Same line (as well as others): You've statically coded an element ID (prod). IDs are supposed to be unique, so if the code is successfully looping, you'll end up with lots of elements with the same ID (this shouldn't cause the issue you're facing, but there's javascript included that you're not showing us, so the JS could be removing duplicate IDs).
  3. <iframe src="orders2.php?image=' . $row['Image'] . '"style= position:absolute;width:500px;height:500px;"></iframe> - The style attribute is merged into the src attribute. You need a space there or the browser may not know what to do with either attribute.
  4. Same line, the style attribute has no opening " but it has a closing " - more of the same.
  5. <a href="orders2.php?image=' . $row['Image'] . '"> - You have no opening " but you have a closing "... you can omit quotes altogether, but you can't do both.

There are more issues, as well as some general coding standards you should clean up, but you should have a look at the resulting code and make sure that it passes muster.

Lastly, make sure that the image is supposed to be displaying. In the first if statement the image is never told to display (presumably it's in the iframe) and in the else statement the image is echoed into a div that has style="display:none", so we wouldn't expect to see it. If the problem is just that the isn't displaying the image, make sure that the iframe src is correct (and you might want to urlencode the image URI when you're passing it as a _GET variable.)

If you post the resulting HTML code (and confirm that it's not just that MySQL is returning one row) we might be able to help further.

于 2013-02-11T04:10:22.347 回答