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"; ?>