0

I'm working on a project that involves writing PHP code into a database. I've created the appropriate database through PHP MyAdmin ( The database looks like this)

In order to write to this database, I have created a HTML and PHP file. The HTML code:

<h1> <u> Enter Food </u> </h1>

<form name="enter_food" action="enter_food_script.php" method="post">
    Choose Quantity: 
    <input type="text" name="number" size = "5">
    <select name="serving_size">
        <option value = "plate"> Plate </option>
        <option value = "bowl"> Bowl </option>
        <option value = "cup"> Cup </option>
    </select>
        <br>
    Choose Turkish Food:
    <select name="turk_food">
        <option value = "manti"> Manti </option>
        <option value = "kofta"> Kofta </option>
        <option value = "simit"> Simit </option>
    </select>
        <br>
    <br> <button type="non-turk_food"><a href="/IA/non-turkish_food.php">Enter non-Turkish food</a></button> <br>
    <br> <input type="submit" value="Submit"> <br>
    <br> <button type="homepage"><a href="/IA/Homepage.php">Return to Homepage</a></button> <br> <br>
</form>

The PHP code:

    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "turkish_calorie_tracker";
    $conn = new mysqli($servername, $username, $password, $dbname);

    if($conn->connect_error){
                die("Connection failed:" . $conn->connect_error ."; Please contact the creator of this app.");
            }

    date_default_timezone_set('Asia/Dubai'); $date = date('Y/m/s h:i:s a', time());
    $food = $_POST['turk_food'];
    $quantity = $_POST['number'] . $_POST['serving_size'];
    $consumed = "Consumed";
    $calories = 100;

    $entry = $conn->prepare("INSERT INTO data(Food/Exercise, Quantity, Calories Burned or Consumed, Number of Calories) VALUES ( ?,?,?,?)");
    $entry->bind_param("sssi", $food, $quantity, $consumed, $calories);
    $entry->execute();
    if($entry){
        echo 'Inserted';
    } else {
        echo 'Not Inserted';
    }
    $entry->close();
    header("refresh:1.5; url=/ia/enter_food.html");
    $conn->close();

Can someone please help me by saying the error in my code?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rohan Jha
  • 1
  • 2
  • 3
    If you're going to have spaces in column names, then you need to surround them with backticks. It would be easier to use more sensible column names using underscores, however. – aynber Dec 16 '19 at 13:30
  • `$entry = $conn->prepare("INSERT INTO data('Food/Exercise', Quantity, 'Calories Burned or Consumed', 'Number of Calories') VALUES ( ?,?,?,?)");` – Piyush Shukla Dec 16 '19 at 13:35
  • @PiyushShukla Those are single quotes, not backticks. Single quotes would be treated as strings, not column names. – aynber Dec 16 '19 at 13:38
  • Hi, I replaced all the spaces with underscores and also tried using apostrophe's in the INSERT into section, but sadly neither of them worked. Is there anything else to work on? – Rohan Jha Dec 16 '19 at 13:40
  • Please see the dupe. Apostrophes are not backticks. – aynber Dec 16 '19 at 14:05

0 Answers0