'Why am I getting a "Insert value list does not match column list: 1136 Column count doesn't match value count" error when the data matches? [duplicate]

I am getting the aforementioned error but my count on columns and data to insert are both 19

try {
    $db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "INSERT INTO prescriptions (
        rxID, rxPrimeName, rxAltName, patient, prescriber, dpp, dppMetric, totalDailyDosage, 
        totalDailyPills, frequency, freqMetric, am, noon, pm, bed, prn, pharmacy, lastFill, pills
        )
    VALUES 
        (
        '$rxID', '$drugName1', '$drugName2', '$patient', '$prescriber', '$dpp', '$metric', '$totalDailyDosage,
        $totalDailyPills', '$quantity', '$frequency', '$am', '$noon', '$pm', '$bed', '$prn', '$pharmacy', '$lastFill', '$pills'
        )";
    $db->exec($sql);
    echo "New record created successfully";
} catch (PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}
$db = null;

Here is the exact error I'm receiving:

INSERT INTO prescriptions ( rxID, rxPrimeName, rxAltName, patient, prescriber, dpp, dppMetric, totalDailyDosage, totalDailyPills, frequency, freqMetric, am, noon, pm, bed, prn, pharmacy, lastFill, pills ) VALUES ( '1111111', 'Test1', 'Test2', 'Jordan', 'Test3', '50', 'mg', '100, 2', '1', 'BID', '1', '1', '0', '0', '0', 'KJdh', '2022-04-15', '60' ) SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1

Both sides (INSERT INTO and VALUES) have 19

When I use phpMyAdmin to insert the data, here's what it comes back with:

INSERT INTO prescriptions (keyID, rxID, rxPrimeName, rxAltName, patient, prescriber, dpp, dppMetric, totalDailyDosage, totalDailyPills, frequency, freqMetric, am, noon, pm, bed, prn, pharmacy, lastFill, pills) VALUES (NULL, '1234567', 'Test1', 'Test2', 'Jordan', 'Test3', '30', 'mg', '60', '2', '1', 'BID', '1', '1', '0', '0', '0', 'Atrium', '2022-04-15', '60');

Please forgive the blocks on the column names above - I'm using backquote around the column names

The phpMyAdmin one works perfectly yes I'm aware that the variable data is different, but all acceptable types based on the column



Solution 1:[1]

I counted 18. Looks like you are missing ' at '100, 2',

Solution 2:[2]

The problem is your values clause is missing quotes:

VALUES 
(
'$rxID', '$drugName1', '$drugName2', '$patient', '$prescriber', '$dpp', '$metric',
'$totalDailyDosage, <-- missing quote before comma
-->> missing quote --> $totalDailyPills', 
'$quantity', '$frequency', '$am', '$noon', '$pm', '$bed', '$prn', '$pharmacy', '$lastFill', '$pills'
)";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Futterkiste
Solution 2 Stu