Hello again!
I have a problem concerning my buy.php code. The whole site is ready to launch but I keep having a problem with this particular bug, that I seem to fix every time but it keeps coming back. I think there is a problem with this shared key thing where the user id and symbol must be unique. But whenever I buy a stock it is only updating the shares of one row without creating a new one, even if it is a new user. Here is my php code.
<?php
// configuration
require("../includes/config.php");
// if user reached page via GET (as by cliccking a link or via redirect)
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
// else render form
render("buy_form.php", ["title" => "Buy"]);
}
// else if user reached page via POST (as by submitting a form via POST)
else if($_SERVER["REQUEST_METHOD"] == "POST")
{
// checks if in both fields something was typed
if (empty($_POST["shares"]) || empty($_POST["symbol"]))
{
apologize("You have to fill in both fields.");
}
// checks for non-negative integer
if (!preg_match("/^\d+$/", $_POST["shares"]) || !is_numeric($_POST["shares"]))
{
apologize("The 'Shares' field has to be a non-negative integer.");
}
// finds stock information
$symbol = strtoupper($_POST["symbol"]);
$stock = lookup($symbol);
if (!$stock)
{
apologize("Symbol does not exist.");
}
$usercash = CS50::query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);
// calculates amount to be paid and checks if it exceeds users cash
$amount = $_POST["shares"] * $stock["price"];
if ($amount > $usercash[0]["cash"])
{
apologize("Not enough money in your account.");
}
// update portofolio table
$insert = CS50::query("INSERT INTO portofolios (user_id, symbol, shares) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE shares = shares + VALUES(shares)",
$_SESSION["id"], $symbol, $_POST["shares"]);
if ($insert == 0)
{
apologize("Could not buy stocks.");
}
// updates users cash
$update = CS50::query("UPDATE users SET cash = cash - ? WHERE id = ?", $amount, $_SESSION["id"]);
if ($update == 0)
{
apologize("Could not buy stocks.");
}
// update history
$history = CS50::query("INSERT INTO history (user_id, bos, symbol, shares, price) VALUES (?, 'BUY', ?, ?, ?)", $_SESSION["id"], $stock["symbol"],
$_POST["shares"], $stock["price"]);
redirect("/");
}
?>
Below you can see the properties of my phpmyadmin database as well.
CREATE TABLE `portofolios` (
`id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`symbol` varchar(5) NOT NULL,
`shares` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`,`symbol`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `portofolios`
--
LOCK TABLES `portofolios` WRITE;
/*!40000 ALTER TABLE `portofolios` DISABLE KEYS */;
INSERT INTO `portofolios` VALUES (0,9,'TIF',81),(1,9,'GOOG',10), (2,9,'AAPL',20);
/*!40000 ALTER TABLE `portofolios` ENABLE KEYS */;
UNLOCK TABLES;
Thank you folks!
EDIT:
It seems that you have understood wrong my columns in the portofolios table. The id column is not identical to the user_id column and it is just a way of identifying the columns, that's why I have it as my primary key. This is what the documentation for this pset says. Could it be a problem, that I have not implemented AUTO INCREMENT for the id column?