0

This used to work but I added a function to the code and now I am receiving this error and no matter what I do it won't go away:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

The code is:

<?php

if (isset($_POST['submit'])) {
    require "../config.php";

    try  {
        $connection = new PDO($dsn, $username, $password, $options);
        $new_trade = array(
            "Date" => $_POST['Date'],
            "Symbol"  => $_POST['Symbol'],
            "Buy_Price"     => $_POST['Buy_Price'],
            "Sell_Price"       => $_POST['Sell_Price'],
            "Buy_Total"  => $_POST['Buy_Total'],
            "Sell_Total" => $_POST['Sell_Total'],
            "Commission" => $_POST['Commission'],
            "Profit_Loss" => $_POST['Profit_Loss'],
            "Quantity" => $_POST['Quantity'],
            "%Profit_Loss" => $_POST['%Profit_Loss']
        );

        $sql = sprintf(
                "INSERT INTO %s (%s) values (%s)",
                "Clare_Trades",
                implode(", ", array_keys($new_trade)),
                ":" . implode(", :", array_keys($new_trade))
        );

        $statement = $connection->prepare($sql);
        $statement->execute($new_trade);
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
}
?>
<script>
     calprofit = function() 
 {
        var buyprice = document.getElementById('Buy_Total').value;
        var sellprice = document.getElementById('Sell_Total').value;
        var comp =   (sellprice - buyprice) / sellprice * 100;
        var comp =  parseFloat(comp.toFixed(2));
        document.getElementById('Profit_Loss').value = sellprice - buyprice;
        document.getElementById('%Profit_Loss').value = comp;
        document.getElementById('Commission').value = (buyprice * 0.01) + (sellprice * 0.01);

   }

</script>
<?php include "templates/header.php"; ?><h2>Add a Trade</h2>

    <form method="post">
        <label for="date">Date</label>
        <input type="datetime" name="Date" id="Date">
        <label for="symbol">Coin Symbol</label>
        <input type="text" name="Symbol" id="Symbol">
        <label for="Buy_Price">Buy Price</label>
        <input type="float" name="Buy_Price" id="Buy_Price">
        <label for="Sell_Price">Sell Price</label>
        <input type="float" name="Sell_Price" id="Sell_Price">
        <label for="Buy_Total">Buy Total</label>
        <input type="float" name="Buy_Total" id="Buy_Total">
        <label for="Sell_Total">Sell Total</label>
        <input type="float" name="Sell_Total" id="Sell_Total" onblur="calprofit()" >
        <label for="Quantity">Quantity</label>
        <input type="float" name="Quantity" id="Quantity">
        <label for="Profit_Loss">Profit/Loss</label>
        <input type="float" name="Profit_Loss" id="Profit_Loss">
        <label for="Profit_Loss_%">%_Profit_Loss</label>
        <input type="text" name="%Profit_Loss" id="%Profit_Loss">
        <label for="Commission">Commission</label>
        <input type="float" name="Commission" id="Commission">
        <br><br>
        <input type="submit" name="submit" value="Submit">
    </form>
<br><br>
    <a href="index.php">Back to home</a>

    <?php include "templates/footer.php"; ?>
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 3
    Try and remove the `%` from `%Profit_Loss`. I think that it potentially can cause issues with the placeholder. Call it something like: `Profit_Loss_Percentage` instead, or use unnamed placeholders. – M. Eriksson Jan 07 '22 at 12:15
  • `I added a function`...what was the version which used to work? What did you change? – ADyson Jan 07 '22 at 12:17
  • 1
    Here's [another question/answer](https://stackoverflow.com/questions/5809951/pdo-valid-characters-for-placeholders) confirming my suspicion about the placeholder name issue. – M. Eriksson Jan 07 '22 at 12:23
  • Hi - many thanks for the advice - I removed the '%' sign and it now works. Very strange though as it was working with this before I added the 'calprofit' function. – Jason Clarke Jan 07 '22 at 12:47
  • Are you sure you didn't add the `%Profit_Loss` parameter at the same time? Or change its name? There's no way that Javascript function could cause this error, by itself. Maybe check your source control history... – ADyson Jan 07 '22 at 12:48
  • Nominating as a duplicate of [PDO valid characters for placeholders](https://stackoverflow.com/questions/5809951/pdo-valid-characters-for-placeholders) – ADyson Jan 07 '22 at 12:48

0 Answers0