Php 如何在每次有人访问sql中的链接时将1添加到mysql数据库中的int
我已经测试了所有的东西,但是没有任何东西可以工作。这是我的代码Php 如何在每次有人访问sql中的链接时将1添加到mysql数据库中的int,php,mysql,Php,Mysql,我已经测试了所有的东西,但是没有任何东西可以工作。这是我的代码 <?php session_start(); if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { die('Invalid id'); } $conn = mysqli_connect("redacted", "redacted", "redacted", "redacted"); if (!$conn) { die("Connection failed: ".
<?php
session_start();
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { die('Invalid id'); }
$conn = mysqli_connect("redacted", "redacted", "redacted", "redacted");
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$id = (int)$_GET['id'];
"UPDATE affiliate SET clicks WHERE ID='$id' = clicks + 1";
header("Location: https://discord.gg/CjzZRBq");
?>
问题在于您只是回显$sql
(这是查询字符串),而不是将该sql命令传递给数据库。还请注意,当前脚本易受SQL注入攻击。要避免这种情况,请使用:
// Retrieve the number of existing clicks
$stmt = $conn->prepare("SELECT clicks FROM affiliate WHERE ID = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($clicks); // Store the result in the $clicks variable
$clicks++; // Increment clicks
// Update the table
$stmt2 = $conn->prepare("UPDATE affiliate SET clicks = ? WHERE ID = ?");
$stmt2->bind_param("si", $clicks, $id);
$stmt2->execute();
// Close the connection once finished
$conn->close();
希望这有帮助!:) 您实际上还没有将查询发送到数据库。您刚刚构建了一个查询字符串。甚至没有保存到变量中的字符串
$id = (int)$_GET['id'];
"UPDATE affiliate SET clicks WHERE ID='$id' = clicks + 1";
header("Location: https://discord.gg/CjzZRBq");
应该是:
$id = (int)$_GET['id'];
$qry= "UPDATE affiliate SET clicks = clicks+1 WHERE ID='$id'";
conn->query($qry);
header("Location: https://discord.gg/CjzZRBq");
你也应该向上看。强制转换为int可以降低风险,但您肯定应该使用绑定变量。尝试“更新关联集单击次数=单击次数+1,其中ID=“$ID”
@Maraboc我做了,但它不起作用。try“更新分支集点击次数=1001,其中ID='$ID'
只是想看看是否可以在database@Maraboc没有work@JonathanConnery:嗯,看来我错过了SQL本身的一个错误。请再试一次。您在第60行的/storage/ssd2/622/2278622/public_html/login/Dashboard/index.php中得到了:注意:未定义变量:sql第60行的/storage/ssd2/622/2278622/public_html/login/Dashboard/index.php警告:mysqli::query():第60行的/storage/ssd2/2278622/public_html/login/Dashboard/index.php中得到了空查询,考虑到我根本没有使用$sql
变量,您得到了:注意:未定义的变量:第60行/storage/ssd2/622/2278622/public_html/login/Dashboard/index.php中的mysqli致命错误:未捕获错误:调用成员函数prepare()在/storage/ssd2/622/2278622/public_html/login/Dashboard/index.php:60堆栈跟踪:#0{main}中抛出空值,现在已修复,将$mysqli
替换为$conn
。为什么我要更新此文件中的单击?
$id = (int)$_GET['id'];
$qry= "UPDATE affiliate SET clicks = clicks+1 WHERE ID='$id'";
conn->query($qry);
header("Location: https://discord.gg/CjzZRBq");