0

Let me start off by saying that if my question has already been answered, please point me to the answer as I have been unable to locate a viable solution to my problem.

I am trying to insert information submitted from a form into a table within a database. I am able to connect to the database just fine but when I try to run the INSERT SQL command, it spits out and error message. All that I am getting is: "Error: INSERT INTO 008181 (userID, calibration, dayName, date, time, bottomAirTemp, topAirTemp, meatTemp, cheeseTemp, walkinTemp, refrig1Temp, refrig2Temp, refrig3Temp, freeze1Temp, freeze2Temp, freeze3Temp, saladAirTemp, saladCheeseTemp, wingTemp, sauceTemp, buffPizzaTemp, hotHold, pizzaTemp, pastaTemp, topOvenTemp, topOvenSpeed, centerOvenTemp, centerOvenSpeed, bottomOvenTemp, bottomOvenSpeed, greenPepperDate, mushroomDate, onionDate, approvedProduct, expiredProduct, rawZoneProcess, fifoProcess, produceWashed, hotWater, ppmLevel, highTempWasher, lowTempWasher, chemicalsStored, illTeamMembers, handWashing, waterTemp, sinksStocked, hairRestraints, pestProvention) VALUES ('GW', 'yes', 'Sunday', 'March 12', '2:34:24 PM', '34', '34', '34', '34', '34', '34', '34', '34', '34', '34', '34', '34', '34', '34', '34', '34', 'yes', '34', '34', '', '', '', '', '', '', '', '', '', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes') 1064 ". Part of my INSERT command is being filled in by a PhP variable, however the variable is putting the correct information into the command.

The following is my php commands:

$sql = "INSERT INTO $storeNumber (userID, calibration, dayName, date, time, bottomAirTemp, topAirTemp, meatTemp, cheeseTemp, walkinTemp, refrig1Temp, refrig2Temp, refrig3Temp, freeze1Temp, freeze2Temp, freeze3Temp, saladAirTemp, saladCheeseTemp, wingTemp, sauceTemp, buffPizzaTemp, hotHold, pizzaTemp, pastaTemp, topOvenTemp, topOvenSpeed, centerOvenTemp, centerOvenSpeed, bottomOvenTemp, bottomOvenSpeed, greenPepperDate, mushroomDate, onionDate, approvedProduct, expiredProduct, rawZoneProcess, fifoProcess, produceWashed, hotWater, ppmLevel, highTempWasher, lowTempWasher, chemicalsStored, illTeamMembers, handWashing, waterTemp, sinksStocked, hairRestraints, pestProvention)
VALUES ('$userID', '$calibration', '$dayName', '$date', '$time', '$bottomAirTemp', '$topAirTemp', '$meatTemp', '$cheeseTemp', '$walkinTemp', '$refrig1Temp', '$refrig2Temp', '$refrig3Temp', '$freeze1Temp', '$freeze2Temp', '$freeze3Temp', '$saladAirTemp', '$saladCheeseTemp', '$wingTemp', '$sauceTemp', '$buffPizzaTemp', '$hotHold', '$pizzaTemp', '$pastaTemp', '$topOvenTemp', '$topOvenSpeed', '$centerOvenTemp', '$centerOvenSpeed', '$bottomOvenTemp', '$bottomOvenSpeed', '$greenPepperDate', '$mushroomDate', '$onionDate', '$approvedProduct', '$expiredProduct', '$rawZoneProcess', '$fifoProcess', '$produceWashed', '$hotWater', '$ppmLevel', '$highTempWasher', '$lowTempWasher', '$chemicalsStored', '$illTeamMembers', '$handWashing', '$waterTemp', '$sinksStocked', '$hairRestraints', '$pestPrevention')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br/>" . mysqli_errno($conn);
}

I am not sure of where my sql is going wrong as this error is only spitting out the same information which I am putting into the command.

Again, I am able to connect to the database just fine, and the database has tables set up to correspond to all possible $storeNumbers that can be selected within the submitting form. When setting up the command at first, I did not have any form of quot marks around the column names within the table. I have even tried to grab the sql code directly from phpmyadmin and only change the referring table name to the corresponding variable.

Dragonman86
  • 95
  • 1
  • 2
  • 10
  • @Fred, I am not sure how the suggested questions would answer my question as they have absolutely nothing to do with the issue that I am having. If they will answer my question, please kindly explain in what way my code went wrong so that I can better understand the answer. – Dragonman86 Mar 12 '17 at 22:13
  • you didn't quote the variables for string literals in your values; that's why I closed the question; or did I miss something? – Funk Forty Niner Mar 12 '17 at 22:14
  • No, and I was not aware of how I had messed up. After reading the answer "When to use....." I see what you are referring to and understand why my question was closed. Thank you for pointing that out, and I am updating my code right now to see if it fixes my issue. – Dragonman86 Mar 12 '17 at 22:20
  • Ok. Place single quotes around them, i.e.: `'$var'` in the values for strings. Check for errors on the query. If there are any apostrophes etc that mysql will complain about, then you will need to escape the variablles first, but better yet, use a prepared statement. That will take care of most of it. Try that, and if you have problems, update your question with the new code you used and other possible errors, but do not overwrite your original post; but as an additional edit under the original. – Funk Forty Niner Mar 12 '17 at 22:25
  • Thank you for the pointer. However I am still having the same error pop up and I am not sure why. Do the column names need to have the backticks around them? – Dragonman86 Mar 12 '17 at 22:37
  • welcome. Column (table) names only need to be ticked if the names contain spaces, hyphens or only numbers or are reserved words. If this is a matter other than mysql, then you'll need to post relevant code for it, such as php and the api/connection used. Plus, I asked you originally to not overwrite your question's code and you went and did that. `INSERT INTO $storeNumber` the variable here needs to be ticked as per the other duplicate marked. – Funk Forty Niner Mar 12 '17 at 22:41

0 Answers0