2014-02-10 52 views
0

我想將數據添加到使用phpmyadmin創建的數據庫中,方法是將信息輸入到html表單中,但每當我點擊「提交」時,它就會轉到'insert.php'窗體和我得到的錯誤是'沒有收到數據'這是我的insert.php代碼。我在這個網站上直接輸入了相同的代碼。 「http://www.w3schools.com/php/php_mysql_insert.asp」(插入數據從一個表單到數據庫中部分」我使用的XAMPP作爲我的服務器有什麼建議使用PHP將數據從html表單添加到sql數據庫

+0

你必須創建一個HTML表單和形式設置方法類型GET或POST – Jain

+0

展你的代碼。 –

+1

請勿使用該網站的教程 - http://www.w3fools.com/;去尋找一個_proper_之一。然後嘗試理解在那裏做了什麼,在手冊中看看你不知道的東西 - 只需輸入完全相同的代碼,你就不會學到太多東西。 – CBroe

回答

0

希望這將有助於

Form.html

<form action="insert.php" method="post"> 
    <input type="text" name="data"> 
    <input type="submit"> 
</form>  

insert.php

<?php 
    //checking if data has been entered 
    if(isset($_POST['data']) && !empty($_POST['data'])) 
    { 
     $data = $_POST['data']; 
    } else { 
     header('location: form.html'); 
     exit(); 
    } 

    //setting up mysql details 
    $sql_server = 'localhost'; 
    $sql_user = 'user'; 
    $sql_pwd = 'password'; 
    $sql_db = 'database'; 

    //connecting to sql database 
    $myslqi = new mysqli($sql_server, $sql_user, $sql_pwd, $sql_db) or die($mysqli->error); 

    //inserting details into table 
    $insert = $mysqli->query("INSERT INTO table (`data`) VALUE ('$data')"); 

    //closing mysqli connection 
    $mysqli->close; 
?> 
0

有些事情首先要檢查:?

有insert.php呼應的所有數據你通過get或post傳遞給它,我會下注發生的事情是表單中的錯誤,如果失敗,手動在php管理器中運行insert(在數據庫中有一個sql標籤,在那裏運行你的查詢確保你沒有一個SQL錯誤)。如果是這樣也沒事,然後發佈HTML,我們可以從那裏。

1

確保您已設置表單的動作和方法。

<form action='insert.php' method='POST'> 

//HTML inputs here 

</form> 
1
<form action="insert.php" method="post"> 
Name <input type="text" name="name"> 
Last name <input type="text" name="last"> 
<input type="submit" name="submit" value="Send"> 
</form> 

和文件insert.php應該像一個在你(W3Schools的)的鏈接。當然,您需要將變量更改爲namelast並使用MySQL連接!

相關問題