Laurcode
Laurcode

Reputation: 27

MYSQL insert into database using php and html form

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();
}

Upvotes: 0

Views: 1024

Answers (1)

NoOne
NoOne

Reputation: 104

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

Upvotes: 1

Related Questions