[英]PHP Retrieving Data from MySQL Database Error
I have two php files that are called form1.php, and demo12.php. 我有两个php文件,分别称为form1.php和demo12.php。 This is the result of the form1.php file:
这是form1.php文件的结果:
<form action="demo12.php" method="post" /> <p>Name <input type="text" name="input1" /></p> <p>Age <input type="text" name="input2" /></p> <input type="submit" value="Submit" /> </form>
Then the code of my demo12.php is: 然后我的demo12.php的代码是:
<?php
define('DB_NAME', 'form1');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
$value2 = $_POST['input2'];
$sql = "INSERT INTO user_info (name, age) VALUES ('$value', '$value2')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM name") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
echo $row["name"];
}
?>
I am able to enter a name, age, then click the submit button, which brings the user to the demo12.php page and inserts the data into the MySQL database. 我能够输入名称,年龄,然后单击提交按钮,这会将用户带到demo12.php页面,并将数据插入MySQL数据库。 The problem is, I want to retrieve and then display all from the name column onto the page.
问题是,我想检索然后从名称列中全部显示到页面上。 So I have a database named form1, them a table named user_info.
所以我有一个名为form1的数据库,它们是一个名为user_info的表。 In that the 3 columns I have are ID, name, and age.
在那三列中,我分别是ID,姓名和年龄。 To display all from the name column I have this at the near-end of my script:
要显示“名称”列中的所有内容,请在脚本的结尾处显示以下内容:
$result = mysql_query("SELECT * FROM name") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
echo $row["name"];
}
Then when the data is inserted, then when it is going to display the all of the names in the table it has an error: 然后,当插入数据时,然后要在表中显示所有名称时,就会出现错误:
Table 'form1.name' doesn't exist 表'form1.name'不存在
But it has no errors when I take that code out. 但是当我取出该代码时没有错误。 Why is it not working?
为什么不起作用?
You are inserting into the database user_info
and then trying to select form the database name
. 您要插入数据库
user_info
,然后尝试从数据库name
选择表格。 Try changing your select to "SELECT * FROM user_info"
. 尝试将您的选择更改为
"SELECT * FROM user_info"
。
You should have written: 您应该已经写过:
$result = mysql_query("SELECT * FROM user_info") or die(mysql_error());
and not 并不是
$result = mysql_query("SELECT * FROM name") or die(mysql_error());
Please replace at your last part with following 请在最后部分替换以下内容
$result = mysql_query("SELECT name FROM user_info") or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
echo $row['name'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.