2014-05-20 109 views
0

我有一個小問題我試圖插入數據到mysql數據庫,但當我添加數據在input box記錄顯示爲blank entry,我在哪裏出錯了。感謝團隊。mysql數據庫條目顯示空白

我的代碼如下:

這是我的索引文件

文件名:index.php文件

<?php 
// link to the page where the database is being connected 
require_once('php/db_connection.php'); 

// set up your query within a variable to make it easier to work with 
$query = "SELECT * FROM people"; //this will select everything out of our database(artificial_test1) from the 'people' table. 

$result = mysql_query($query); //this will conect to the database and select the table to run the query on and return the values from that table. 

// now we will set up a while loop to retrieve the information from the database table. 
while ($person = mysql_fetch_array($result)) { 
    echo "<h3>". $person['name'] ."</h3>"; // this line will fetch the query result that is stored in the $person variable and find the row ['name'] and display it on screen 
    echo "<p>". $person['info'] ."</p>"; 
    } 
?> 
<!-- below we will set up a form to add users to the database --> 
<h1>Create a user</h1> 
<form action="create_user.php" method="post"> 
    <label for="">Name</label><input type="text" name"name"><br /> 
    <label for="">user info</label><input type="text" name"info"> 
    <br /> 
    <input type="submit" name="submit" value="submit"> 
</form> 

這是建立用戶檔案:

文件名:create_user.php

<?php 

    include ('php/db_connection.php'); 

    $name = $_POST['name']; 
    $info = $_POST['info']; 

    if (!$_POST['submit']) { 
     echo "please fill out the form"; 
     header('Location: index.php'); 
    }else{ 
     mysql_query("INSERT INTO people (`ID`, `name`, `info`) VALUES (NULL, '$name', '$info')") or die(mysql_error()); 
     echo "User has been added!"; 
     mysql_close(); 
     header('Location: index.php'); 
    } 
?> 

這是我的數據庫連接文件:

文件名:db_connection.php

<?php 
// set up variables relating to database connection 
$db_host = 'localhost'; // this defines the type of server we are connecting to ie:. we are using localhost because we are using a local server to connect to. 
$db_user = 'root'; // the user that you are using to log into the database with. default user in our case is 'root'. 
$db_password = ''; // if there is a password on the database then that will go in this variable, but we dont have one on the local servers db so we set it to nothing. 
$database = 'artificial_test1'; // this is where we select the database that we are going to use. 

// this is where we connect to the database we use the php function mysql_connect. 
// mysql_connect function will take 3 arguments inside the parentheses and will look like 
// this ie:. mysql_connect(database host, database user, database password). 
$connect_db = mysql_connect($db_host, $db_user, $db_password); 

// now we have to select the database that we want to start using. 
// For this we will make use of the php function mysql_select_db(database_name) 
// which will take 1 argument which is the database_name we want to work with 
mysql_select_db($database); 
?> 
+1

Ohai律」 [鮑比表(HTTP: //bobby-tables.com)! – eggyal

回答

5

你忘記了輸入n中的等號ame屬性 -

<label for="">Name</label><input type="text" name="name"><br /> 
<label for="">user info</label><input type="text" name="info"> 

另外,您應該從mysql切換到mysqli,因爲mysql函數已被棄用。您還需要保護自己免受可能的SQL Injection Attacks

+0

謝謝Jay,你是男人!是的,我知道我應該切換,但在此刻我試圖理解mysql,所以當我切換病理時明白兩者之間的差異。你有什麼有用的文章或視頻教程,我可以進行我的研究? – Code

+1

我會從這裏開始 - https://wikis.oracle.com/display/mysql/Converting+to+MySQLi你會喜歡php.net上的文檔,因爲它們之間的功能可能不會共享相同的名稱。這是相當無痛的。 –

+0

即使是簡單的沙粒也會引起(*沙*)風暴。有點像「如果蝴蝶在中國掀起翅膀,它會在另一個國家感受到」。 (*或效果* *)好抓;;) –

1

在你的形式,name"name"應該是name="name"name"info"name="info"

0

存在name屬性的問題,你忘了添加等號(=)

<label for="">Name</label><input type="text" name"name"><br /> 
<label for="">user info</label><input type="text" name"info"> 

應該

<label for="">Name</label><input type="text" name="name"><br /> 
<label for="">user info</label><input type="text" name="info"/>