使用php和html表单将mysql插入数据库

798qvoo8  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(420)

我正在尝试使用php和html表单插入mysql数据库。当我按submit时,它表示已成功插入项目,但当我登录到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();
}
f3temu5u

f3temu5u1#

这似乎要么是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();
   }
   ?>

它在提交表格后给了我这个结果:

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

相关问题