I'm using the following code in an attempt to insert user-provided values in <form>
into an sql table. When the INSERT query is used in PHPMyAdmin, the insertion is successful, but unsuccessful in the following php. Connection to the database is successful.
<?php
$servername = "localhost";
$username = "***";
$password = "***";
$db_name = "my_db";
$trip;
$odom;
$gals;
$ppg;
if (isset($_REQUEST['trip'])){
$trip = $_REQUEST['trip'];
$odom = $_REQUEST['odom'];
$gals = $_REQUEST['gals'];
$ppg = $_REQUEST['ppg'];
}
// Create connection
$conn = mysqli_connect($servername, $username, $db_name, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully ";
Here, I've tried using double quotes around the query to form a string; I've omitted the quotes as well, to no avail. Also, in VALUES, I've tried both single-quotations and the omission thereof as well, without success. The variables are double values, not strings.
$vals = "INSERT INTO `Log` (`Trip`, `Odometer`, `Gallons_Pumped`,
`Price_Gallon`)
VALUES ('$trip', '$odom', '$gals', '$ppg')";
The return value of $retval
is false, but no error is provided.
$retval = mysql_query($vals);
if ($retval) {
echo "New record created successfully";
} else {
echo "Error: " . $vals . "<br>" . $retval->error;
}
mysql_close($conn);
?>
Is my syntax incorrect? If not, why is the INSERT query unsuccessful?