简体   繁体   English

PHP没有插入SQL列

[英]PHP not inserting into SQL columns

I'm using Ajax to collect the data such as the winningReddit, the LosingReddit, and the win and lose photos. 我正在使用Ajax来收集诸如winsReddit,LosingReddit以及胜出和丢失照片等数据。 The PHP script (below) is then supposed to send that to the MySQL tables. 然后PHP脚本(下面)将其发送到MySQL表。 The "win" and "lose" columns should increase by 1 each time. “胜利”和“失败”列每次应增加1。

For some reason this script is not saving to the database though. 出于某种原因,此脚本不会保存到数据库中。 What am I doing wrong? 我究竟做错了什么? Am I missing something? 我错过了什么吗?

<?php
if(isset ($_POST['action'])) {

include( 'connection.php');

$winnerLink = $_POST['winnerReddit'];
$loserLink = $_POST['losingReddit'];
$win = $_POST['win'];
$lose = $_POST['lose'];


mysql_query("UPDATE $winnerLink SET win = win + 1 WHERE imagelink = '$win'");
mysql_query("UPDATE $loserLink SET lose = lose + 1 WHERE imagelink = '$lose'");

}

?>

Here's the Ajax code I'm using: 这是我正在使用的Ajax代码:

    $.ajax({
        url: 'http://website.com/vote.php',
        method: 'POST',
        data: {
            action: 'save',
            win: chosenURL,
            lose: chosenURL,
            winnerReddit: $(this).attr('id'),
            losingReddit: $(this).siblings('div').attr('id')
        },
        success: function(data) {
            alert('sent');
        },
        error: function() {
            alert('nope')
        }
    });
})
})

Replace this 替换它

mysql_query("UPDATE $winnerLink SET win = win + 1 WHERE imagelink = $win");
mysql_query("UPDATE $loserLink SET lose = lose + 1 WHERE imagelink = $lose");

With this prepared statement: 准备好这份声明:

$stmt = mysqli_prepare("UPDATE ? SET win = win + 1 WHERE imagelink = ?");
$stmt->bind_param("ss", $_POST['winnerReddit'], $_POST['win']);
$stmt->execute();
$stmt->close();

$stmt = mysqli_prepare("UPDATE ? SET lose = lose + 1 WHERE imagelink = ?");
$stmt->bind_param("ss", $_POST['losingReddit'], $_POST['lose']);
$stmt->execute();
$stmt->close();

You will also need make sure you have connected to a database . 您还需要确保已连接到数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM