0

I'm trying to insert into an MYSQL database using php and an html form. When I press submit it says an item inserted successfully but when I log into phpmyadmin the table is empty?

My html form code is creon.html.

<form name="bmr_calc" action="https://cs1.ucc.ie/~lmm12/Project/creon.php" method="POST" id="BMRform">
   <h1 id="info">Creon Calculator</h1>
   <table>
      <tr>
         <td colspan="2" align="center">I take one: <br>
            <input type="radio" name="creon1" value="10" required> Creon 10,000
            <input type="radio" name="creon1" value="25" required> Creon 25,000 
            <br>per <input type="text" name="creon3" required>g of fat
         </td>
      </tr>
      <br>
      <tr>
         <td>There are:</td>
         <td><input type="text" name="creon4" required>g of fat in the item I'm about to eat</td>
      </tr>
   </table>
   <td><input type="submit" value="Submit" style="float:center">
      <br>
      <img src="img/regpic.jpg" alt="reg" id="reg">
      <br>
   </td>
</form>

My php code is creon.php and it's saved on my college server;

<?php

$creon1 = $_POST['creon1'];
$creon3 = $_POST['creon3'];
$creon4 = $_POST['creon4'];

if (!empty($creon1) || !empty($creon3) || !empty($creon4)) {
    $host = "cs1.ucc.ie";
    $dbUsername = "lmm12";
    $dbPassword = "----";
    $dbname = "mscim2018_lmm12";
    //create connection
    $conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
    if (mysqli_connect_error()) {
        die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
    } else {
        $INSERT = "INSERT Into creon (creon1, creon3, creon4) values(?, ?, ?)";
        //Prepare statement

        $stmt = $conn->prepare($INSERT);
        $stmt->bind_param("sss", $creon1, $creon3, $creon4);
        $stmt->execute();
        echo "New record inserted sucessfully";

        $stmt->close();
        $conn->close();
    }
} else {
    echo "All field are required";
    die();
}
3
  • 3
    You've published your password, which might not be best practice. Commented Sep 11, 2018 at 16:19
  • 1
    You never check to make sure that the insert was successful. Check for mysqli errors after your insert execute to see if it really did insert successfully. Commented Sep 11, 2018 at 16:19
  • unrelated to this actual question, your test (!empty($creon1) || !empty($creon3) || !empty($creon4)) only checks that at least one field contains a value, whereas your error message suggests that the rule should be (!empty($creon1) && !empty($creon3) && !empty($creon4)) Commented Sep 11, 2018 at 16:22

1 Answer 1

1

This seems to be either a type issue in MySQL or a Type issue in your PHP code

To be sure, upload your data types from the table creon aka are the fields varchars, text etc.

Notice the if/else statement around bind_param you need to do that too just in case something isn't quite right, also capitalize INTO in your statement.

I ran the following:

<?php
   $creon1 = $_POST['creon1'];
   $creon3 = $_POST['creon3'];
   $creon4 = $_POST['creon4'];


   if (!empty($creon1) ||  !empty($creon3) || !empty($creon4)) {

    $host = "localhost";
       $dbUsername = "root";
       $dbPassword = "";
       $dbname = "quick";
       //create connection
       $conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
       if (mysqli_connect_error()) {
        die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
       } else {
        $test = "INSERT INTO testing (creon1, creon3, creon4) values(?, ?, ?)";
        //Prepare statement

         $stmt = $conn->prepare($test);
     if ($stmt != false)
            $stmt->bind_param("sss", $creon1, $creon3, $creon4);
     else
         print("Returns false");
         $stmt->execute();
         echo "New record inserted sucessfully";

        $stmt->close();
        $conn->close();
       }
   } else {
    echo "All field are required";
    die();
   }
   ?>

It gave me this result after submitting the form:

Picture of result

In my database it inserted the row:

Inserted row

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.