douping3860 2015-08-18 02:23
浏览 33

自定义使用JavaScript,PHP,MySQL计数

Hoping you can help me.

My Goal

  1. Creating a custom Like(♥) button with a counter.
  2. Store (INSERT) onclick() output value to MySQL
  3. Increment (UPDATE) stored value by one(+1)
  4. Retrieve (READ) lastest incremented value from MySQL and display on web page

What I have working

I have JavaScript outputting the value of '1' onclick.

My issue

Storing the JavaScript value on a database so it increments by 1 for every user click. I'm a bit suck with this. Any help is highly appreciated!

THE PROBLEM CODE:

HTML:

<div class="heart" id="like" onclick="onClick()">
    <span id="output"></span>
</div>

JavaScript:

// Heart Like counts
var clicks = 0;
var hasClicked = false;
    function onClick() 
    {
        if (!hasClicked) 
            {
                clicks += 1;
                document.getElementById("output").innerHTML = clicks;
                hasClicked = true; 
            };
        
    };

PHP (Connection File to DB)

<?php
//Opens connection to MySQL sever.
$severname = 'localhost';
$username = 'root';
$password = 'root';
$dbname = 'db_Portfolio';


//Creating connection to DB
$conn = mysql_connect($severname, $username, $password);

// Check connection.
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//Status of connection. 
echo "Connected to database successfully.";
?>

MySQL:

CREATE TABLE `Like_Count` (
  `Id` int(11) NOT NULL,
  `Likes` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `Likes` (`Likes`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

</div>
  • 写回答

1条回答 默认 最新

  • drl47263 2015-08-18 05:46
    关注

    The count table:

    CREATE TABLE `Like_Count` (
      `Id` int(11) NOT NULL AUTO_INCREMENT,
      `Like_id` int(11) NOT NULL ,
      `Like_count` int(11) NOT NULL ,
      PRIMARY KEY (`Id`),
      UNIQUE KEY `Likes` (`Like_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

    the user table :

    CREATE TABLE `Like_Count` (
      `Id` int(11) NOT NULL AUTO_INCREMENT,
      `User_id` int(11) NOT NULL ,
      `Like_id` int(11) NOT NULL ,
      PRIMARY KEY (`Id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

    so everytime someone clicks, check in user table to see if he has clicked. If not, update the count table with the Like_id you find in the user table to get the record in count table.

    You'd better do the click action by using JavaScript and using PHP for checking and database works.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序