简体   繁体   English

更新查询执行但不更新表

[英]Update query executing but not updating table

I am having difficulties using the UPDATE statement with a WHERE clause 我在将UPDATE语句与WHERE子句一起使用时遇到困难

I am using the following code to try to update a record in the table WHERE squadnumber is the value in the squadnumber dropdown. 我正在使用以下代码尝试更新表中的记录,其中squadnumber是squadnumber下拉列表中的值。

<img src="header.png" alt="Southside FC Header">
<h1>Player Statistics</h1> 

<?php

require("config.inc.php");

if (!empty($_POST)) {

    //initial query
    $query = "UPDATE playerstatistics SET squadnumber=':squadnumber', appearances=':appearances', subappearances=':subappearances', 
    goalsscored=':goalsscored', yellowcards=':yellowcards', redcards=':redcards' WHERE squadnumber=':squadnumber'";

    //Update query
    $query_params = array(
        ':squadnumber' => $_POST['squadnumber'],
        ':appearances' => $_POST['appearances'],
        ':subappearances' => $_POST['subappearances'],
        ':goalsscored' => $_POST['goalsscored'],
        ':yellowcards' => $_POST['yellowcards'],
        ':redcards' => $_POST['redcards']

    );


    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        header('Location: http://localhost/webservice/errorCouldNotAddData.php');
        die(json_encode($response));
    }

    $response["success"] = 1;
    header('Location: http://localhost/webservice/managerhomepage.php');
    echo json_encode($response);

} else {


?>
<br /> 

<form action="addplayerstatistic.php" method="post">

Squad Number: <br />
<select name="squadnumber">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
</select>
<br />

Number of appearances:<br /> 
<input type="number" name="appearances"/><br />

Number of appearances as a substitute:<br /> 
<input type="number" name="subappearances"/><br />

Number of Goals Scored:<br /> 
<input type="number" name="goalsscored"/><br />

Number of yellow cards:<br /> 
<input type="number" name="yellowcards"/><br />

Number of red cards:<br /> 
<input type="number" name="redcards"/><br />

<input type="submit" value="Add statistics" /> 
</form>

<a href="managerhomepage.php"><img src="backButton.png"></a>
<?php

}

?>

$response["success"] is being set to one as the query appears to have ran correctly, however when I check the database nothing has changed. 由于查询似乎已正确运行,$ response [“ success”]被设置为1,但是当我检查数据库时,没有任何改变。

I think my problem surrounds the WHERE clause not being set to the number in the dropdown so no row is updated. 我认为我的问题是未将WHERE子句设置为下拉列表中的数字,因此没有行被更新。

Any ideas? 有任何想法吗?

Thanks 谢谢

Try to update your query 尝试更新您的查询

$query = "UPDATE playerstatistics SET squadnumber=:squadnumber, appearances=:appearances, subappearances=:subappearances, 
goalsscored=:goalsscored, yellowcards=:yellowcards, redcards=:redcards WHERE squadnumber=:squadnumber";

You should not escape your parameters - PDO does that for you. 您不应该转义您的参数-PDO为您做到了。

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

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