Hi There,
Probably a bit of neiche thing but I am trying to send data from an arduino and put the data into a database called Pre-play with a table called retrieve with column values of volume,gain,treble,bass and contour.
I am trying to add them all in however no matter what I do it just adds the first declared POST value in this case the volume condition and then it just assigns a value of 0 to the other 4. I have tried it various ways and have used code checkers however they haven’t brought up any "common issues that it could be. I imagine the issue is just me. I have used PHP before I used it for some projects in the past and also use Laravel but decided to use standard PHP as I wasn’t sure how I would implement this into laravel.
Please could you have a look at my code and see what I am doing wrong/incorrect.
First way I tried:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Pre-Play";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Database Connection failed: " . $conn->connect_error);
}
if(!empty($_POST['volvalue'] && $_POST['gaivalue']))
{
$volvalue = $_POST['volvalue'];
$gaivalue = $_POST['gaivalue'];
$sql = "INSERT INTO retrieve (volume,gain)VALUES ('".$volvalue."','".$gaivalue."')";
if ($conn->query($sql) === TRUE) {
echo "OK";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
Second way I tried:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Pre-Play";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Database Connection failed: " . $conn->connect_error);
}
if(!empty($_POST['volvalue']))
{
$volvalue = $_POST['volvalue'];
$sql = "INSERT INTO retrieve (volume,gain)VALUES ('".$volvalue."')";
if ($conn->query($sql) === TRUE) {
echo "OK";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
if(!empty($_POST['gaivalue']))
{
$gaivalue = $_POST['gaivalue'];
$sql = "INSERT INTO retrieve (gain)VALUES ('".$gaivalue."')";
if ($conn->query($sql) === TRUE) {
echo "OK";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
if(!empty($_POST['trevalue']))
{
$trevalue = $_POST['trevalue'];
$sql = "INSERT INTO retrieve (treble)VALUES ('".$trevalue."')";
if ($conn->query($sql) === TRUE) {
echo "OK";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
if(!empty($_POST['basvalue']))
{
$basvalue = $_POST['basvalue'];
$sql = "INSERT INTO retrieve (bass)VALUES ('".$basvalue."')";
if ($conn->query($sql) === TRUE) {
echo "OK";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
if(!empty($_POST['convalue']))
{
$convalue = $_POST['convalue'];
$sql = "INSERT INTO retrieve (contour)VALUES ('".$value."')";
if ($conn->query($sql) === TRUE) {
echo "OK";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
If you’re able to help with this then that’d be brilliant
Kind Regards,
Ben