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!

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

报告相同问题?