簡體   English   中英

MYSQL使用php和html形式插入數據庫

[英]MYSQL insert into database using php and html form

我正在嘗試使用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();
}

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

提交表格后,它給了我這個結果:

結果圖片

在我的數據庫中,它插入了以下行:

插入行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM