I'm trying to pass some values to a php file from an html file by using a <form action="addExp.php">
tag. The two variables I want to pass is the "catGory
" and the "amount
" values.
Everytime It succesfully directs me to the addExp.php
page but it doesn't there always seem to be some error displayed in the browser's console:
My html code:
<form action="addExp.php">
<div style="position: fixed;" id="Cat">
<select name="catGory" id="catSelect">
<option hidden disabled selected value=''>Select a Category</option>
<option value="Restaurant">Restaurant</option>
<option value="Health">Health</option>
<option value="Groceries">Groceries</option>
<option value="Shopping">Shopping</option>
<option value="Travelling">Travelling</option>
<option value="Education">Education</option>
<option value="Work">Work</option>
<option value="Bills/Taxes">Bills/Taxes</option>
</select>
</div>
<input type="text" onfocus="this.value=''" value="0" step="0.01" id="amount" name="amount">
</form>
My "addExp.php" code:
<?php
//phpinfo(); //Uncomment to check php info
//Declare the variables that will receive the payment infos
$inAmount = $_POST['amount'];
$trnCat = $_POST['catGory'];
//variables that define the connection to the database
$servername = "localhost";
$username = "rahim";
$password = "**********";
$dBase = "Users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dBase);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Check if the Expense table exists or no
$val = $conn->prepare('select 1 from `Expenses` LIMIT 1');
//Create Table query first
$sQuer1 = "CREATE TABLE Expenses(
Amount FLOAT(9,2),
Category CHAR,
pyDate Date,
)";
//Execute addition of record into Table
if ($val !== FALSE) //if the Table exists
{
//Prepare statement and parameters to be executed into the query
$statmnt = $conn->prepare("INSERT INTO Expenses (Amount,Category,pyDate) VALUES (?,?,?)"); //prepare SQL statement/query for execution
$statmnt->bind_param("dss", $inAmount, $trnCat, date("l d-m")); //bind the selected parameters to the SQL statement
//Execute the query statement
$statmnt -> execute();
//show that data is added
echo "Table exists, so data have been added";
} else {
//Create the table because table doesn't exist
$conn->query($sQuer1); // query() function performs a query against a database
//Prepare statement and parameters to be executed into the query
$statmnt = $conn->prepare("INSERT INTO Expenses (Amount,Category,pyDate) VALUES (?,?,?)"); //prepare SQL statement/query for execution
$statmnt->bind_param("dss", $inAmount, $trnCat, date("l d-m")); //bind the selected parameters to the SQL statement
//Execute the query statement
$statmnt -> execute();
//show that table is created
echo "The Table has been creaetd";
}
?>
Edit: I also forgot to show my apache2 error logs:
Error logs
question from:
https://stackoverflow.com/questions/65932981/i-need-help-identifying-if-ive-made-a-mistake-in-either-html-or-php-code-snippe 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…