0

我将把这个问题分成几段以便于参考;

  1. 我有一个带有按钮的网站,该按钮将数据库中的当前数字与 1 相加。

  2. 我现在使用“更新”表格来添加数据库中的数字。我已经使用准备好的语句来做到这一点:

    <?php
        require_once('connectvars.php');
        $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
            if (mysqli_connect_errno()) {
                echo 'Cannot connect.' . mysqli_connect_error(); 
                exit();
                }
    
            if ($stmt = $mysqli->prepare('UPDATE `javascriptbutton` SET `clicks`=? WHERE `id`=?')) { 
    
                $stmt->bind_param('ii', $clicks, $id);
    
                $clicks = $_POST['clicks'] + 1; 
                $id = 1;
    
                $stmt->execute();
    
                $stmt->close();
    
            } else {
                echo 'Woops, something went wrong.: ' . $mysqli->error;
            }
    

    ?>

  3. 当用户在 html 中按下一个按钮时,它会调用一个包含 Ajax 的脚本,该脚本引用了上面的 php 脚本。

  4. 每次按下按钮时,这个 php 文件都必须从 id=1 的数据库列“clicks”中获取当前的“clicks”,并加上 1 并将其插入数据库。

  5. 我一直在研究和查看我的代码,我可以得出结论,它与:

    $clicks = $_POST['clicks'] + 1;

The $_POST['clicks'] should be the current number in the database, and get plussed by 1.

Question: Do I have to create a select form to get the current clicks out of the database? Or how can I plus the current clicks with 1 and put the new number back into the database till next time a person presses the button?

If there are any of the 5 steps you want to deepen, please tell me.

4

1 回答 1

0

You can just use:

$mysqli->prepare("UPDATE `javascriptbutton` SET `clicks`=`clicks`+1 WHERE `id`=?")

EDIT Full code:

    if ($mysqli->prepare("UPDATE `javascriptbutton` SET `clicks`=`clicks`+1 WHERE `id`=?")) { 

        $stmt->bind_param('i', $id);
        $id = 1;

        $stmt->execute();

        $stmt->close();

    } 
于 2013-03-30T19:56:06.570 回答