Context: I've established a connection with a MySQL database that has Stock Ticker, Open, High, Low, Close prices, interval (1d, 1wk and 1mo) and date. Now, I'm trying to make it possible for a user to select the "ticker" name and "interval" in HTML and send this input as a MySQL search query with PHP. This data would then be saved in an array ($data_array[] = $row
) so I could then use this data in a javascript variable. My main goal with this is first to be able to print the output from the MySQL query (i.e. the values of $data_array
like this: [date,open,high,low,close],[date,open,high,low,close], etc. I will use this in a javascript to print out a graph using google charts. For the time being, I've established values for the ticker
as AAPL and interval
as 1mo (1 month) to test if I can get the data from the database
Problem: Whenever I run the PHP code, I keep getting "Undefined array key "ticker"" as well as "Undefined array key "interval"". I've found this topic mysql query result into php array, but not sure if this would fix the problem I'm having. I was also getting syntax error, unexpected variable "$stmt"
, but that seems to be solved by now
I'm new to PHP so any help is very welcome (specific documentation I might've missed,etc)! I apologize in advance if this is a duplicate, but I've been searching for possible solutions and I haven't been able to come up with one
HTML:
<form name="query" method="post" action="/connection.php">
<!--<div class="ticker">-->
<label for="ticker">Ticker:</label>
<input style="background: rgba(255,255,255,0.5);border-radius: 10px; border: none; text-align: center;"
type="text" id="ticker" name="ticker" required minlength="1" maxlength="4" size="10"
value="AAPL"></input>
<!--</div>-->
<!--<div class="interval"> -->
<label for="interval">Period:</label>
<select style="background: rgba(255,255,255,0.5);border-radius: 10px; border: none; text-align: center;"
name="interval" id="interval">
<option value="1d">1 day</option>
<option value="1wk">1 week</option>
<option selected="selected" value="1mo">1 month</option>
</select>
<!--</div>-->
<input class="button1" type="submit" formmethod="post" formaction="/connection.php" name="submit"></input>
</form>
PHP:
<?php
$servername = "...";
$username = "...";
$password = "...";
$dbname = "...";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$ticker = $_POST['ticker'];
$interval = $_POST['interval'];
if (isset($_POST['submit'])) {
$stmt = $mysqli->prepare("SELECT Date, Open, High, Low, Close FROM symbols WHERE ticker='$ticker' AND interval='$interval'");
$stmt->bind_param("ss", $ticker, $interval);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc($result)) {
$data_array[] = $row;
echo "[" . $row["Date"]. "," . $row["Open"]. "," . $row["High"]. "," . $row["Low"]. "," . $row["Close"]. "],";
}
$stmt->close();
}
?>
Testing:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$ticker = $_POST['ticker'];
$interval = $_POST['interval'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$stmt = $mysqli->prepare("SELECT Date, Open, High, Low, Close FROM symbols WHERE ticker=? AND interval=?");
$stmt->bind_param("ss", $ticker, $interval);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc($result)) {
$data_array[] = $row;
echo "[" . $row["Date"]. "," . $row["Open"]. "," . $row["High"]. "," . $row["Low"]. "," . $row["Close"]. "],";
}
$stmt->close();
}
?>
<!DOCTYPE html>
<html>
<body>
<div class="container-fluid">
<div class="statistic-block block">
<form name="query" method="post" action="" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<!--<div class="ticker">-->
<label for="ticker">Ticker:</label>
<input style="background: rgba(255,255,255,0.5);border-radius: 10px; border: none; text-align: center;"
type="text" id="ticker" name="ticker" required minlength="1" maxlength="4" size="10"
value="AAPL"></input>
<!--</div>-->
<!--<div class="interval"> -->
<label for="interval">Period:</label>
<select style="background: rgba(255,255,255,0.5);border-radius: 10px; border: none; text-align: center;"
name="interval" id="interval">
<option value="1d">1 day</option>
<option value="1wk">1 week</option>
<option selected="selected" value="1mo">1 month</option>
</select>
<!--</div>-->
<input class="button1" type="submit" name="submit" id="submit" onsubmit="return false"></input>
</form>
</div>
</div>
</body>
</html>