duanca3415 2017-07-18 23:45
浏览 71
已采纳

每当有人转到sql中的链接时,如何在mysql数据库中为int添加1

I have tested everything and nothing works here's my code

<?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");
?>

and after I want it to echo on the users dashboard this is what I have

<h1>Clicks</h1>
                <br />
                <br />
                You have gotten: <?php $conn = mysqli_connect("localhost", 
"id2278622_jonny", "Fencing1", "id2278622_affiliate");
 if (!$conn) {
    die("Connection failed: ".mysqli_connect_error());
}
$sql = "SELECT clicks FROM affiliate WHERE ID='$ID'";
echo "$sql";
?> Clicks!

but it just echos the sql code

  • 写回答

2条回答 默认 最新

  • dqh19413 2017-07-18 23:51
    关注

    You haven't actually sent your query to the database. You've just built a query string. A string that you didn't even save to a variable.

    $id = (int)$_GET['id'];
    "UPDATE affiliate SET clicks WHERE ID='$id' = clicks + 1";
    header("Location: https://discord.gg/CjzZRBq");
    

    Should be:

    $id = (int)$_GET['id'];
    $qry= "UPDATE affiliate SET clicks = clicks+1 WHERE ID='$id'";
    conn->query($qry);
    header("Location: https://discord.gg/CjzZRBq");
    

    You should also look up SQL Injection. Casting to an int mitigates risk, but you should definitely be using bind variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?