Reputation: 107
So I have a NodeMCU programmed to send float data to a hosted MySQL Database. I made it so that the value being sent is printed in the serial monitor for me to make sure that it's correct. Up until this point, everything works fine.
However, when I check the recorded values, it would only have '1.00'. For example, the supposed recorded value according to the serial monitor should be '12.65', but the database would have '1.00' instead. I tried converting the variable type to float using (float) $var
and floatval($var)
but it still had the same result. What am I missing?
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$api_key = test_input(isset($_POST["api_key"]));
if ($api_key = $api_key_value)
{
$var = test_input(isset($_POST["DB_Column"]));
$insert_var = mysqli_query($conn, "INSERT INTO SCHEMA.Table(Time, Record)
VALUES(CURRENT_TIMESTAMP, '" . $var . "')
;");
if (!$insert_var)
{
die("Query failed: " . mysqli_error($conn));
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Upvotes: 0
Views: 334
Reputation: 57121
In both cases you are actually getting the values from isset()
which returns true or false.
$var = test_input(isset($_POST["DB_Column"]));
If you want to make sure there is a value then usually you would add it to your inital test...
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["DB_Column"])...
or to give it a default value, use ??
, in here it defaults to 0...
$var = test_input( $_POST["DB_Column"] ?? 0 );
Also rather than using test_input()
, you should be using prepared statements to protect from SQL injection etc, How can I prevent SQL injection in PHP?.
Upvotes: 2