doujia9833 2012-12-04 22:58
浏览 44
已采纳

每次单击图像时更新mysql表中的分数(+1)(<a>)。 用php

I'm working on a website just now, and basically it has 2 images next to each other.

What I want to happen is when you click one, and not the other, it will update the score in the database +1 for the photo you clicked.

Just now both pictures seem to be getting their score in their respective tables updated by 1, but both on page load which is annoying.

I am only wanting the score for the one the user clicks to update by 1 in the db.

Is it because it is located within the tag?

Here is the code, and any help would be greatly appreciated.

<?php 

//Connect to database
mysql_connect("localhost","username","password")
    or die("Could not connect: ".mysql_error());
mysql_select_db("database")
    or die("Could not connect to database, ".mysql_error());

//Variables
$thisPage = "index.php";

$count_query_cat1= mysql_query("SELECT COUNT(*) FROM `cat1`");
$count_query_cat2= mysql_query("SELECT COUNT(*) FROM `cat2`");

$count_num_cat1 = mysql_fetch_array($count_query_cat1);
$count_num_cat2 = mysql_fetch_array($count_query_cat2);


if (isset($_GET['cat1']))
{
    $id_cat1 = (int) $_GET['cat1'];
}

if (isset($_GET['cat2']))
{
    $id_cat2 = (int) $_GET['cat2'];
}

if ($id_cat1 == 0)
{
    $url = $thisPage."?cat1=".$count_num_cat1[0]."&cat2=".$count_num_cat2[0];
    header("Location: $url");
}


$query_cat1 = mysql_query("SELECT * FROM `table1` WHERE `id` = $id_cat1");
$result_cat1 = mysql_fetch_assoc($query_cat1);

$query_cat2 = mysql_query("SELECT * FROM `table2` WHERE `id` = $id_cat2");
$result_cat2 = mysql_fetch_assoc($query_cat2);

$image_cat1 = "<img src=\"{$result_cat1['path']}\" class=\"content_image\" alt=\"{$result_cat1['description']}\" title=\"{$result_cat1['description']}\" /><br />";
$image_cat1 = "<img src=\"{$result_cat2['path']}\" class=\"content_image\" alt=\"{$result_cat2['description']}\" title=\"{$result_cat2['description']}\" /><br />";


$description_cat1 = $result_cat1['description'];
$description_cat2 = $result_cat2['description'];


//Random button
$random_cat1 = rand(1,$count_num_cat1[0]);
if ($random_cat1 == $count_num_cat1[0]){
    $random_cat1 = rand(1,$count_num_cat1[0]);
}

$random_cat2 = rand(1,$count_num_cat2[0]);
if ($random_cat2 == $count_num_cat2[0]){
    $random_cat2 = rand(1,$count_num_cat2[0]);
}

$random_btn = "{$thisPage}?cat1={$random_cat1}&cat2={$random_cat2}";

?>
<!DOCTYPE html>
<html>
<head>
    <?php echo $pageTitle; ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="screen" href="css/style.css" type="text/css" />
    <link rel="shortcut icon" href="images/favicon.ico" />
</head>
<body>
<div id="wrapper">

    <div id="navigation">
        <a href="index.php"><div id="home_icon" alth="Home Icon" title="Home"></div></a>
        <a href="<?php echo $random_btn; ?>"><div id="random_icon" alth="Random Icon" title="Random"></div></a>
        <div id="image_title">
            <p>TITLE</p>
        </div>
    </div>

    <div id="content">

    <!-- Content [Image] cat1 -->

        <a href="<?php mysql_query("UPDATE `table1` SET `score` = `score` + 1 WHERE `id` = $id_cat1"); echo $random_btn; ?>"><?php echo $image_cat1; ?></a><br/>

    <!-- Content [Image] cat2 -->

    <a href="<?php mysql_query("UPDATE `table2` SET `score` = `score` + 1 WHERE `id` = $id_cat2"); echo $random_btn; ?>"><?php echo $image_cat2; ?></a><br/>

    </div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • doutangliang7769 2012-12-04 23:31
    关注

    PHP code gets executed on page load, unless stated otherwise (by some IF statement or such). So, if you put any kind of PHP code inside your HTML tag, like here:

    <a href="<?php mysql_query("UPDATE `table2` SET `score` = `score` + 1 WHERE `id` = $id_cat2"); echo $random_btn; ?>"><?php echo $image_cat2; ?></a><br/>
    

    code you wrote inside of href attribute gets executed, no matter whether the link is clicked or not.

    Solution is as following: In href attribute, write "?update_photo=1", and in the ohter anchor write href="?update_photo=2".

    Inside the PHP tags write the following:

    if(isset($_GET['update_photo'])){
        $id = (int)$_GET['update_photo'];
        mysql_query("UPDATE `table2` SET `score` = `score` + 1 WHERE `id` = {$id}");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程