duan41497 2017-08-20 12:42
浏览 255
已采纳

MySQL UPDATE语句在查询中有效,但在PHP代码中无效

When I execute the statement in phpmyadmin, it works properly, but when I copy and paste the exact same query into this php file, it doesn't work.

PHP Code:

if($_GET['vote'] == 1) {
    echo "if statement ran";
    $sql = "UPDATE raids SET attendees = attendees +1 WHERE dateposted =  '2017-08-19 16:15:46'";
    mysql_query($sql, $link);
}

My link variable does work and the 'if' statement executes. Other SQL statements haven't given me trouble.

Why isn't the php code incrementing 'attendees' when used in the PHP code?

  • 写回答

1条回答 默认 最新

  • duandiaoqian5795 2017-08-20 13:25
    关注

    As Milan Chheda said, MySQL is deprecated and is no longer secure. Use PDO or at least MySQLi instead.

    MySQLi implementation for your code:

        //MySQLi information
    
        $db_host     = "localhost";
        $db_username = "username";
        $db_password = "password";
    
        //connect to mysqli database (Host/Username/Password)
        $connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
    
        //select MySQLi dabatase table
        $db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
    
        if(isset($_GET['vote']) && $_GET['vote'] !== NULL) {
    
    $vote = $_GET['vote'];
    
    if($vote == "1") {
        echo "Vote is 1, updating the database";
        $sql = mysqli_query($connection, "UPDATE raids SET attendees = attendees + '1' WHERE dateposted =  '2017-08-19 16:15:46'");
    }
    
    }
    

    I hope this helped you. Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法