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

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?

图片转代码服务由CSDN问答提供 功能建议

当我在phpmyadmin中执行语句时,它可以正常工作,但是当我将完全相同的查询复制并粘贴到此时 php文件,它不起作用。

PHP代码:

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

我的链接变量确实有效,并且'if'语句执行。 其他SQL语句没有给我带来麻烦。

为什么php代码在PHP代码中使用时不会增加'与会者'?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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!

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题