按钮和链接无法正常工作

时间:2014-11-13 09:11:58

标签: php html

在我的网络应用程序中,我有一个图像,每个图像都有相似的按钮以及上传它的用户的名称。用户名是一个链接。但在我的代码中,链接和按钮都不起作用。两者都不可点击。 这是我的代码

     <?php 
 // Connects to your Database 
 mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
  mysql_select_db("test") or die(mysql_error()) ; 

 //Retrieves data from MySQL 
   $data1 = mysql_query("SELECT * FROM image_upload INNER JOIN user_table
ON image_upload.user_id=user_table.user_id  WHERE flag=1 ORDER BY timestamp DESC; ")     or die(mysql_error());


//Puts it into an array

 while($info = mysql_fetch_array($data1)){
  //Outputs the image and other data 
  ?>

  <div class="test" id="<?php $info['ID']?>">
  <div class="username"><a href="profile.php" class="but" title=""><?php echo     $info['user_name']?></a></div>
  <div class="imagedisplay"><img src="<?php echo     "uploads/".$info['image']?>"style="width:230px; height:auto; border:1px solid #000; border-    radius:20px;"></div>
  <div class="desc"><?php echo $info['description']?></div>
  <button type="button" id="<?php echo $info['ID']?>">Like</button>


 </div>

<?php
   }
 ?> 

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

试试这个

<?php 
 // Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
mysql_select_db("test") or die(mysql_error()) ; 

 //Retrieves data from MySQL 
$data1 = mysql_query("SELECT * FROM image_upload INNER JOIN user_table
ON image_upload.user_id=user_table.user_id  WHERE flag=1 ORDER BY timestamp DESC; ")     or die(mysql_error());


//Puts it into an array

while($info = mysql_fetch_array($data1)){
//Outputs the image and other data 
?>

  <div class="test" id="<?php echo $info['ID']?>">
  <div class="username"><a href="profile.php" class="but" title=""><?php echo $info['user_name']?></a></div>
  <div class="imagedisplay"><img src="<?php "uploads/".$info['image']?>"style="width:230px; height:auto; border:1px solid #000; border-radius:20px;"></div>
  <div class="desc"><?php echo $info['description']?></div>
  <button onclick="location.href='profile.php'" type="button" id="<?php echo $info['ID']?>">Like</button>
 </div>

<?php
}
?>

自PHP 5.5.0起,不推荐使用MySQL扩展,不建议用于编写新代码,因为将来会删除它。相反,应该使用mysqli或PDO_MySQL扩展名。

MySQLi解决方案

<?php 
 // Connects to your Database 

$con = mysqli_connect('localhost', 'root', '');
    mysqli_select_db($con, 'test') or die ('Failed to connect to MySQL: ' . mysqli_connect_error()); 


 //Retrieves data from MySQL 
$data1 = mysqli_query($con, "SELECT * FROM image_upload INNER JOIN user_table
ON image_upload.user_id=user_table.user_id  WHERE flag=1 ORDER BY timestamp DESC; ") or die(mysqli_error($con));


//Puts it into an array

while($info = mysqli_fetch_array($data1)){
//Outputs the image and other data 
?>

  <div class="test" id="<?php echo $info['ID']?>">
  <div class="username"><a href="profile.php" class="but" title=""><?php echo $info['user_name']?></a></div>
  <div class="imagedisplay"><img src="<?php "uploads/".$info['image']?>"style="width:230px; height:auto; border:1px solid #000; border-radius:20px;"></div>
  <div class="desc"><?php echo $info['description']?></div>
  <button onclick="location.href='profile.php'" type="button" id="<?php echo $info['ID']?>">Like</button>
 </div>

<?php
}
?>