MYSQL使用php和html形式插入数据库

时间:2018-09-11 16:14:52

标签: php html mysql forms phpmyadmin

我正在尝试使用php和html表单插入MYSQL数据库。当我按提交时,它说一个项已成功插入,但是当我登录phpmyadmin时,表为空?

我的html表单代码为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>

我的php代码为creon.php,并保存在我的大学服务器中;

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

1 个答案:

答案 0 :(得分:0)

这似乎是MySQL中的类型问题或PHP代码中的类型问题

请确保从表 creon 上传数据类型,也就是字段varchars,text等。

请注意,bind_param周围的if / else语句也需要这样做,以防万一某些不正确的情况,也可以在语句中大写INTO。

我运行了以下内容:

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

提交表单后,它给了我这个结果:

Picture of result

在我的数据库中插入了以下行:

Inserted row