php - 显示空白的mysql数据库条目

标签 php html mysql

我有一个小问题 我正在尝试将数据插入 mysql 数据库,但是当我将数据添加到 input box 时,记录显示为空白条目,我哪里出错了。感谢团队。

我的代码如下:

这是我的索引文件

文件名: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);
?>

最佳答案

您在输入的名称属性中忘记了等号 -

<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

关于php - 显示空白的mysql数据库条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23760423/

相关文章:

php - redirect() 函数不适用于 CodeIgniter 中的外部 URL

JavaScript - 获取 Div 内容的值 - 内容被错误的字符串替换

html - 绝对定位输入的宽度不符合 CSS 规则

mysql - 如何解码这个 mysql BLOB?

mysql - 为什么停止使用命令行导入sql文件

php - 如何在 PHP 中覆盖 register_argc_argv?

php - 为什么这不会输出 MySQL Artist 表中的所有数据?

php - mySQL 按问题分组

javascript - 不改变地址栏就跳转到内部链接?

mysql - 如何解决 ERROR 1248 (42000) : Every derived table must have its own alias